Reputation: 232
I want to access one of the labels inside the DataList
control. How can I access that in my code behind file (C#)? I'm using Visual Studio 2010
I want to access text property of "productnamelabel"
My code is:
<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>
Upvotes: 3
Views: 3284
Reputation: 232
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;
}
Upvotes: 1
Reputation: 11433
You have to use the FindControl
method. Like so:
Label lbl = (Label)DataList1.FindControl("productNameLabel");
lbl.text = "stuff";
EDIT: To get to every Label
in your DataList
, you need to iterate through all of the DataListItem
s that have been generated by the DataList
. Then you can go through each Control
collection and access the 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";
}
}
}
NOTE: I don't know if this is the best way to do this, it's just what came to mind.
Upvotes: 1