如何从代码隐藏改变列表视图中标签的值
本文关键字:视图 标签 列表 改变 代码 隐藏 | 更新日期: 2023-09-27 18:17:43
实际上我正在使用asp.net和c#开发模板。
我使用listview在我的ascx页面和我的ItemTemplate如下:
<ItemTemplate>
<tr style="background-color:#FFF8DC;color: #000000;">
<td>
<asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="Delete" CausesValidation="false" OnClientClick="return confirm('Are you sure you want to delete this Product Details?');" />
<asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" CausesValidation="True" />
</td>
<td>
<asp:Label ID="EmpIDLabel" runat="server" Text='<%# Eval("EmpID") %>' />
</td>
<td>
<asp:Label ID="EmpNameLabel" runat="server" Text='<%# Eval("EmpName") %>' />
</td>
<td>
<asp:Label ID="DepartmentLabel" runat="server" Text='<%# Eval("Department") %>' />
</td>
<td>
<asp:Label ID="AgeLabel" runat="server" Text='<%# Eval("Age") %>' />
</td>
<td>
<asp:Label ID="AddressLabel" runat="server" Text='<%# Eval("Address") %>' />
</td>
</tr>
</ItemTemplate>
和我检索数据库中的数据在ascx代码后面如下所示:
public DataTable GetEmployee(string query)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
SqlDataAdapter ada = new SqlDataAdapter(query, con);
DataTable dtEmp = new DataTable();
ada.Fill(dtEmp);
return dtEmp;
}
,我还在ascx代码中绑定数据,如下所示:
private void BindLVP(string SortExpression)
{
string UpdateQuery = "Select * from Employee" + SortExpression;
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
hid_UpdateQTP.Value = UpdateQuery;
lvProduct.Items.Clear();
lvProduct.DataSource = GetEmployee(UpdateQuery);
lvProduct.DataBind();
}
我的问题是如何在ItemTemplate中删除<%# Eval("EmpID") %>
和所有其他标签文本并更改标签。文本在ItemTemplate从后面的代码,我的意思是传递数据库的数据到这些标签从后面的代码。
你应该处理ListView的ItemDataBound事件,当你将ListView绑定到它的数据源后,它会为每个项目触发:
protected void LVP_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
Label EmpIDLabel = (Label)e.Item.FindControl("EmpIDLabel");
System.Data.DataRowView rowView = e.Item.DataItem as System.Data.DataRowView;
EmpIDLabel.Text = rowView["EmpID"].ToString();
}
}
这个事件不是在每次回发时触发,而是只在数据绑定时触发(不像ListView的ItemCreated事件)