简体   繁体   English

如何从codebehind更改listview中的label的值?

[英]how to change the values of label in listview from codebehind?

Actually i'm developing template using asp.net and c#. 实际上,我正在使用asp.net和c#开发模板。
i'm using listview at my ascx page and my ItemTemplate is as below: 我在我的ascx页面上使用listview,我的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>

and i retrieve the data from the database in ascx code behind as bellow: 我以下面的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;
}

and also i bind the data in ascx code behind as follow: 而且我将数据绑定在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();
}

my question is how i can delete the <%# Eval("EmpID") %> and all the other label text like this in ItemTemplate and change the label.text in ItemTemplate from the code behind, i mean pass the data of the database to these label from code behind. 我的问题是我如何才能删除<%# Eval("EmpID") %>以及在ItemTemplate中像这样的所有其他标签文本,并从后面的代码中更改ItemTemplate中的label.text,我的意思是传递数据库的数据这些标签从后面的代码。
appreciate your consideration. 感谢您的考虑。

You should handle the ItemDataBound event of the ListView what is fired for every item after you've bound the ListView to it's DataSource: 您应该处理ListView的ItemDataBound事件,然后将ListView绑定到其DataSource,然后为每个项目触发该事件:

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();
    }
}

This event is not triggered on every postback but only on databinding(unlike the ListView's ItemCreated event). 并非每次回发都触发此事件,而仅在数据绑定时触发此事件(与ListView的ItemCreated事件不同)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM