Click here to Skip to main content
12,403,391 members (73,148 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# ASP.NET C#4.0
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:

<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)
protected void Page_Load(object sender, EventArgs e)
{
  ((Label)grdXML.FindControl("UserID")).Text = "Sample text";
}

[edit]Code block fixed[/edit]
Posted 13-Apr-13 2:23am
Updated 7-Jul-16 0:33am
Nelek96.6K
v3
Comments
Navaid Farooqui 13-Apr-13 8:57am
   
this code should be in gridview rowcreated event or in rowcommand
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Access the controls at row level and find the needed control using FindControl. You will have access to rows in RowDataBound event method.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Following example explains the to set value to Item Template controls-----

//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....
  Permalink  
v3
Comments
kalsa 7-Jul-16 6:32am
   
Good one
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

You can also use this code.

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";
                }
               
            }            
 
        }
 
    }
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

Write the code in grdXML_ItemDataBound event



If TypeOf e.Item Is GridDataItem Then
 

 Dim objCommentLabel As Label = selectedItem("TemplateCOlumnID).FindControl("UserID")
 
objCommentLabel.text ="Insert some message"
 

End IF
  Permalink  
v2

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160721.1 | Last Updated 7 Jul 2016
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100