如何从codebehind更改listview中的label的值? [英] how to change the values of label in listview from codebehind?

查看:105
本文介绍了如何从codebehind更改listview中的label的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上我正在使用asp.net和c#开发模板.
我在我的ascx页面上使用listview,而我的ItemTemplate如下:

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>



我以下面的ascx代码从数据库中检索数据:



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



而且我也将数据绑定在ascx代码后面,如下所示:



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



我的问题是我如何删除<%#Eval("EmpID")%>以及在ItemTemplate中像这样的所有其他标签文本,并从后面的代码中更改ItemTemplate中的label.text,我的意思是从后面的代码将数据库的数据传递给这些标签.
感谢您的考虑.



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.

推荐答案

ON ItemDataBound 该事件将在列表视图的每一行运行.

您可以修改每一行的数据,并可以访问每一行的控件.

检查此示例:

ON ItemDataBound this event will run for each row of the listview.

And you can modify the data for each row and can access controls of each row.

check this exapmle:

If (e.Item.ItemType = ListViewItemType.DataItem) Then
               Dim divCatHeader = e.Item.FindControl("divCatHeader")

               Dim lblWeekNbrWeekYr As Label = CType(e.Item.FindControl("lblWeekNbrWeekYr"), Label)
               If catg_cnt = 3 Then
                   divCatHeader.Visible = True
                   catg_cnt = 1
               Else
                   catg_cnt = catg_cnt + 1
               End If

               If (sales_unit = "sales") Then
                   lblWeekNbrWeekYr.text = "Week " & week_nbr & " FYE " & week_yr & " Sales"
               Else
                   lblWeekNbrWeekYr.Text = "Week " & week_nbr & " FYE " & week_yr & " Units"
               End If
           End If


尝试此博客
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx [
try this blog
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx[^]
--NDK


感谢所有人的答复,我将listview的itemDataBound事件用作波纹管:
thanks all for the reply, i have used the itemDataBound event of the listview as bellow:
<asp:listview id="lvProduct" runat="server" onitemdatabound="lvProduct_ItemDataBound" >
</asp:listview>


然后在后面的代码中,我从数据库中获取了数据并将其传递给listview中的标签.


then at the code behind i have get the data from database and pass it to the label in listview.

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


这篇关于如何从codebehind更改listview中的label的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆