如何在代码隐藏的Listview中访问特定标签的ID [英] How Can I Access The Id Of Specific Label In Listview In Code Behind

查看:62
本文介绍了如何在代码隐藏的Listview中访问特定标签的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在后面的代码中访问listview中特定标签的id以获取其值

解决方案

尝试这样..



 受保护  void  ListView1_ItemDataBound(< span class =code-keyword> object  sender,ListViewItemEventArgs e)
{
if (e.Item.ItemType = = ListViewItemType.DataItem)
{
Label lbl = e.Item.FindControl( yourLabelName as 标签;
lbl.Text = Something;
}
}


您在.aspx页面上有一个列表视图,例如:

 <   asp:listview     id   =  lstEmployee    runat   =  server    xmlns:asp   = #unknown >  
onitemdatabound =lstEmployee_ItemDataBound >
< itemtemplate >
< tr id = Tr1 runat = 服务器 >
< td id = Td1 runat < span class =code-keyword> = server >
<% -
数据绑定内容。 - %>
< asp:label id = NameLabel runat = server >
Text ='<% #Eval( 名称)%>' />
< / asp:label > < / td >
< / tr >
< / itemtemplate >
< / asp:listview >





和页面加载绑定列表视图和DataBound事件后面的代码获取标签的id



 使用系统; 
使用 System.Collections.Generic;
使用 System.Web.UI.WebControls;

命名空间 WebApplication1
{
public partial class WebForm1:System.Web.UI.Page
{
protected void Page_Load( object sender,EventArgs e)
{
List< employee> empSource = new 列表< employee>()
{
员工()
{
Name = Sandeep
},
new Employee()
{
Name = Raviender
}
};
lstEmployee.DataSource = empSource;
lstEmployee.DataBind();
}

protected void lstEmployee_ItemDataBound( object sender,ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem )
{
Label lblName =(Label)e.Item.FindControl( NameLabel< /跨度>);
if (lblName.Text == Sandeep
{
lblName.Text = Sandeep Singh Shekhawat< /跨度>;
}
}
}
}
public class 员工
{
public string 名称{获得; set ; }
}

} < / 员工 > < / employee >


How can i access the id of specific label in listview in code behind to get its value

解决方案

Try like this..

protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
       {
           if (e.Item.ItemType == ListViewItemType.DataItem)
           {
              Label lbl =  e.Item.FindControl("yourLabelName") as Label;
              lbl.Text = "Something";
           }
       }


You have a list view on .aspx page for example:

<asp:listview id="lstEmployee" runat="server" xmlns:asp="#unknown">
            onitemdatabound="lstEmployee_ItemDataBound" >   
   <itemtemplate>
     <tr id="Tr1" runat="server">
      <td id="Td1" runat="server">
        <%-- Data-bound content. --%>
        <asp:label id="NameLabel" runat="server">
          Text='<%#Eval("Name") %>' />
      </asp:label></td>
    </tr>
   </itemtemplate>
   </asp:listview>



and code behind on page load bind list view and on DataBound event get id of label

using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            List<employee> empSource = new List<employee>()
                                        {
                                            new Employee()
                                            {
                                                Name ="Sandeep"
                                            },
                                            new Employee()
                                            {
                                                Name ="Raviender"
                                            }
                                        };
            lstEmployee.DataSource = empSource;
            lstEmployee.DataBind();
        }

        protected void lstEmployee_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                Label lblName = (Label)e.Item.FindControl("NameLabel");
                if (lblName.Text == "Sandeep")
                {
                    lblName.Text = "Sandeep Singh Shekhawat";
                }
            }
        }        
    }
    public class Employee
    {
        public string Name { get; set; }
    }
   
}</employee></employee>


这篇关于如何在代码隐藏的Listview中访问特定标签的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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