C# 如何访问列表视图的标签控件?
我想访问C# 如何访问列表视图的标签控件?,c#,asp.net,C#,Asp.net,我想访问DataList控件内的一个标签。如何在代码隐藏文件(C#)中访问该文件?我正在使用VisualStudio2010 我想访问“productnamelabel”的文本属性 我的代码是: <asp:DataList ID="DataList1" runat="server" DataKeyField="id" DataSourceID="SqlDataSource1"> <ItemTemplate> productName:
DataList
控件内的一个标签。如何在代码隐藏文件(C#)中访问该文件?我正在使用VisualStudio2010
我想访问“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";
编辑:要访问数据列表
中的每个标签
,您需要遍历由数据列表
生成的所有数据列表项
。然后,您可以浏览每个控件
集合并访问标签
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";
}
}
}
注意:我不知道这是否是最好的方法,这正是我想到的。下面的代码显示了一个aspx文件的内容,其中包含两个label控件和两个SqlDataSource控件。每个SqlDataSource控件都将其DataSource模式设置为可选值—DataSet和DataReader,并且它们都定义了一个OnSelecting事件,在该事件中指定了EmployeeID参数的值:
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;
}
下面的代码段显示了aspx.cs文件内容,其中的参数值在Selecting事件处理程序中设置。在Page_Load方法中,访问每个Sql数据源控件返回的数据,并将值委托给标签。访问方法取决于数据源模式,但对于SqlDataSource和AccessDataSource都是相同的:
[C#]
受保护的无效页面加载(对象发送方、事件参数e)
{
DataView dvSql=(DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
foreach(dvSql中的DataRowView drvSql)
{
Label1.Text=drvSql[“FirstName”].ToString();
}
OleDbDataReader rdrSql=(OleDbDataReader)SqlDataSource2.Select(DataSourceSelectArguments.Empty);
while(rdrSql.Read())
{
Label2.Text=rdrSql[“LastName”].ToString();
}
rdrSql.Close();
}
受保护的无效SqlDataSource1_选择(对象发送方,SqlDataSourceSelectingEventArgs e)
{
e、 Command.Parameters[“EmployeeID”]。值=2;
}
受保护的无效SqlDataSource2_选择(对象发送方,SqlDataSourceSelectingEventArgs e)
{
e、 Command.Parameters[“EmployeeID”]。值=2;
}
实际上我的问题还没有解决。我的数据列表创建了许多标签,我想访问标签的文本值,但不知道how@prakash:我已经更新了我的答案。我不完全确定我是否理解你的要求,但我希望这能有所帮助。如果没有,请告诉我。将控件投射到标签
并捕获异常,就像把孩子扔进游泳池看他们是否会游泳。你可以问控件它是否是一个标签(if(c是标签){…}
@SWeko:我不知道,很酷!我会更新答案=)。还有…哈哈,这个比喻。