Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: 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 3:23am
Edited 15-Apr-13 1:51am
Nelek63.4K
v3
Comments
Navaid Farooqui at 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
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  

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

  Print Answers RSS
0 Kornfeld Eliyahu Peter 275
1 Maciej Los 204
2 Sergey Alexandrovich Kryukov 200
3 King Fisher 185
4 OriginalGriff 183
0 OriginalGriff 6,223
1 DamithSL 4,724
2 Maciej Los 4,196
3 Kornfeld Eliyahu Peter 3,894
4 Sergey Alexandrovich Kryukov 3,496


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 15 Apr 2013
Copyright © CodeProject, 1999-2014
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