如何从代码隐藏中更改列表视图中标签的值?

发布于 2024-12-21 12:16:19 字数 2212 浏览 2 评论 0原文

实际上我正在使用 asp.net 和 c# 开发模板。
我在我的 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>

我在后面的 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 中的 label.text,我的意思是通过将数据库的数据从代码隐藏到这些标签。
感谢您的考虑。

Actually i'm developing template using asp.net and c#.
i'm using listview at my ascx page and my ItemTemplate is as below:

<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:

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:

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.
appreciate your consideration.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜妞爱困 2024-12-28 12:16:19

您应该处理 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 事件)。

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:

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文