Click here to Skip to main content
15,900,973 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can i access the id of specific label in listview in code behind to get its value
Posted
Comments
Karthik_Mahalingam 30-Jan-14 10:33am    
can u pls add more info.
post the code..
m-e-h-d-h-i 30-Jan-14 10:56am    
I want to use value of label to use in session property and use it in another page

Try like this..

C#
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";
           }
       }
 
Share this answer
 
Comments
m-e-h-d-h-i 30-Jan-14 10:54am    
system Evnt Arg does not have contain definition for 'Item'
Karthik_Mahalingam 30-Jan-14 10:56am    
can u post your code.

<asp:ListView ID="ListView1" runat="server" OnItemDataBound="ListView1_ItemDataBound">
m-e-h-d-h-i 30-Jan-14 10:58am    
can i use this code?
Session["ID"] = (Label)ListView1.FindControl("ID")
Karthik_Mahalingam 30-Jan-14 11:01am    
no. it wont give you proper result.
how many rows does your list view has ?
Karthik_Mahalingam 30-Jan-14 11:01am    
do u have team viewer ?
You have a list view on .aspx page for example:
HTML
<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

C#
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>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900