如何访问列表视图的标签控件?

问题描述:

我想访问DataList控件中的其中一个标签。我如何在我的代码后面访问文件(C#)?我使用Visual Studio 2010如何访问列表视图的标签控件?

我想访问 “productnamelabel”

我的代码的文本属性是:

<asp:DataList ID="DataList1" runat="server" DataKeyField="id" DataSourceID="SqlDataSource1"> 
     <ItemTemplate> 
      productName: 
      <asp:LinkButton ID="LinkButton1" runat="server" Text='<%# Eval("productName") %>'></asp:LinkButton> 
      <asp:Label ID="productNameLabel" runat="server" Text='<%# Eval("productName") %>' /> 
      <br /> 
      brand: 
      <asp:Label ID="brandLabel" runat="server" Text='<%# Eval("brand") %>' /> 
      <br /> 
      <asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("image") %>' /> 
      <br /> 
      catagory: 
      <asp:Label ID="catagoryLabel" runat="server" Text='<%# Eval("catagory") %>' /> 
      <br /> 
      price: 
      <asp:Label ID="priceLabel" runat="server" Text='<%# Eval("price") %>' /> 
      <br /> 
      <br /> 
     </ItemTemplate> 
    </asp:DataList> 
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:shopingConnectionString1 %>" 
     SelectCommand="SELECT [id], [productName], [brand], [image], [catagory], [price] FROM [product] WHERE ([productName] = @productName)"> 
     <SelectParameters> 
      <asp:QueryStringParameter Name="productName" QueryStringField="pName" Type="String" /> 
     </SelectParameters> 
    </asp:SqlDataSource> 

您必须使用FindControl方法。像这样:

Label lbl = (Label)DataList1.FindControl("productNameLabel"); 
lbl.text = "stuff"; 

编辑:要到LabelDataList,则需要通过所有已通过DataList产生的DataListItem S的迭代。然后你可以通过每个Control收集并访问Labels

foreach (DataListItem i in DataList1.Items) // Iterates through each of your Items 
{ 
    foreach (Control c in i.Controls) // Iterates through all the Controls in each Item 
    { 
     if (c is Label) // Make sure the control is a Label control 
     { 
      Label temp = (Label)c; 
      temp.Text = "junk"; 
     } 
    } 
} 

注:我不知道这是否是做到这一点的最好方式,这正是浮现在脑海。

+0

其实我的问题不解决yet.my DataList控件创建多个标签,我想访问标签的文本价值,但不知道如何 –

+0

@prakash:我已经更新了我的答案。我并不完全确定自己明白你的要求,但我希望这会有所帮助。让我知道如果它不。 – jadarnel27

+1

将控件投射到“标签”并捕捉异常,就像将孩子扔进游泳池看看他们是否可以游泳一样。你可以问控件它是否是一个标签('if(c是标签){...}'。 – SWeko

The code below shows the contents of an aspx file, which contains two label controls, and two SqlDataSource controls. Each SqlDataSource control has its DataSource mode set to alternative values - DataSet and DataReader, and both of them have an OnSelecting event defined in which the value of the EmployeeID parameter is assigned: 

<asp:Label ID="Label1" runat="server" /> <asp:Label ID="Label2" runat="server" /> 

<asp:SqlDataSource 
    ID="SqlDataSource1" 
    runat="server" 
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
    ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" 
    DatasourceMode="DataSet" 
    SelectCommand="SELECT [LastName], [FirstName] FROM [Employees] WHERE ([EmployeeID] = ?)" 
    OnSelecting="SqlDataSource1_Selecting"> 
    <SelectParameters> 
     <asp:Parameter Name="EmployeeID" Type="Int32" /> 
    </SelectParameters> 
</asp:SqlDataSource> 

<asp:SqlDataSource 
    ID="SqlDataSource2" 
    runat="server" 
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
    ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" 
    DatasourceMode="DataReader" 
    SelectCommand="SELECT [LastName], [FirstName] FROM [Employees] WHERE ([EmployeeID] = ?)" 
    OnSelecting="SqlDataSource2_Selecting"> 
    <SelectParameters> 
     <asp:Parameter Name="EmployeeID" Type="Int32" /> 
    </SelectParameters> 
</asp:SqlDataSource> 

The following code snippet shows the aspx.cs file contents, where the parameter values are set in the Selecting event handler. In the Page_Load method, the data returned by each of the Sql DataSource controls is accessed and a value consigned to a label. The method of access depends on the DataSource mode, but is identical for both SqlDataSource and AccessDataSource: 

[C#] 
protected void Page_Load(object sender, EventArgs e) 
{ 

    DataView dvSql = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty); 
    foreach (DataRowView drvSql in dvSql) 
    { 
     Label1.Text = drvSql["FirstName"].ToString(); 
    } 

    OleDbDataReader rdrSql = (OleDbDataReader)SqlDataSource2.Select(DataSourceSelectArguments.Empty); 
    while (rdrSql.Read()) 
    { 
     Label2.Text = rdrSql["LastName"].ToString(); 

    } 
    rdrSql.Close(); 
} 



protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e) 
{ 
    e.Command.Parameters["EmployeeID"].Value = 2; 
} 

protected void SqlDataSource2_Selecting(object sender, SqlDataSourceSelectingEventArgs e) 
{ 
    e.Command.Parameters["EmployeeID"].Value = 2; 
}