Click here to Skip to main content
15,883,901 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,

How to set or assign the value to the ItemTemplate Lable control in the gridview from code behind?

I have tried but no solution. It's very urgent.

Awaiting your prompt response. Pls find the below my code



HTML Source:

XML
<asp:GridView ID="grdXML" AutoGenerateColumns="False"  runat="server">
                     <Columns>
                     <asp:TemplateField HeaderText="">
                        <ItemTemplate>
                            <asp:Label ID="UserID" runat="server" Text="" />
                        </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="">
                        <ItemTemplate>
                            <asp:Label ID="Uname" runat="server" Text="" />
                        </ItemTemplate>
                        </asp:TemplateField>
                     </Columns>
                     </asp:GridView>



Code Behind (C#.Net)
C#
protected void Page_Load(object sender, EventArgs e)
{
  ((Label)grdXML.FindControl("UserID")).Text = "Sample text";
}


[edit]Code block fixed[/edit]
Posted
Updated 7-Jul-16 0:33am
v3
Comments
Navaid Farooqui 13-Apr-13 8:57am    
this code should be in gridview rowcreated event or in rowcommand

Access the controls at row level and find the needed control using FindControl. You will have access to rows in RowDataBound event method.
 
Share this answer
 
Following example explains the to set value to Item Template controls-----

C#
//Add onRowDataBound event to your grid and write the following code server side

protected void gridView1_RowDataBound(object sender,GridViewRowEventArgs e)
{
 
//make sure to have UserID as itemtemplate in your grid
 
Label lblUserID= (Label)e.Row.FindControl("UserID");
 
 lblUserID.Text="Sample Text";
}


this will definitely work....
 
Share this answer
 
v3
Comments
kalsa 7-Jul-16 6:32am    
Good one
You can also use this code.

C#
protected void AspGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {    
      if(e.Row.RowType == DataControlRowType.DataRow)
        {
            DataRowView v = (DataRowView)e.Row.DataItem;           
            
            if (e.Row.Cells.Count > 0 && e.Row.Cells[0] != null && e.Row.Cells[0].Controls.Count > 0)
            {
                Label link = e.Row.Cells[0].Controls[0] as Label;
                if (link != null)
                {                    
                        link.Text = "Edit";
                }
               
            }            

        }

    }
 
Share this answer
 
Write the code in grdXML_ItemDataBound event



C#
If TypeOf e.Item Is GridDataItem Then


 Dim objCommentLabel As Label = selectedItem("TemplateCOlumnID).FindControl("UserID")

objCommentLabel.text ="Insert some message"


End IF
 
Share this answer
 
v2

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