2011-09-04 75 views
2

我想訪問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> 

回答

1

您必須使用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

1
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; 
}