Accessing ItemTemplate server control properties in a gridview

Please support our ASP.NET advertiser: $4.95 a Month - ASP.NET Web Hosting – Click Here!
Reply

Join Date: Mar 2008
Posts: 8
Reputation: lagyossarian is an unknown quantity at this point 
Solved Threads: 0
lagyossarian lagyossarian is offline Offline
Newbie Poster

Accessing ItemTemplate server control properties in a gridview

 
0
  #1
Mar 28th, 2008
I have a gridview that has a couple of Label server controls nested inside an ItemTemplate. I want to assign their Text value when each row is databound (I'm using Profile properties so I have to get the user and their profile to get the values).

  1. <asp:GridView ID="grdUsers" runat="server" AllowPaging="True" AutoGenerateColumns="False" DataKeyNames="UserName,UserID" DataMember="DefaultView" DataSourceID="dsUsers" EmptyDataText="There are no users">
  2. <Columns>
  3. <asp:HyperLinkField DataTextField="UserName" DataNavigateUrlFields="UserName" DataNavigateUrlFormatString="~/AddUser.aspx?UserName={0}" HeaderText="Login Name" />
  4. <asp:TemplateField>
  5. <ItemTemplate>
  6. <asp:Label ID="lblLastName" runat="server"></asp:Label>
  7. <asp:Label ID="lblFirstName" runat="server"></asp:Label>
  8. </ItemTemplate>
  9. </asp:TemplateField>
  10. <asp:CheckBoxField DataField="AdminRole" HeaderText="Admin" />
  11. <asp:CheckBoxField DataField="AdminUsersRole" HeaderText="AdminUser" />
  12. <asp:CheckBoxField DataField="ChangeProfileRole" HeaderText="Prof" />
  13. <asp:CheckBoxField DataField="CreateAccountRole" HeaderText="Add" />
  14. <asp:CheckBoxField DataField="DeleteAccountRole" HeaderText="Del" />
  15. <asp:CheckBoxField DataField="EmulationRole" HeaderText="Emul" />
  16. <asp:CheckBoxField DataField="ViewStatsRole" HeaderText="Stat" />
  17. <asp:CheckBoxField DataField="VoidCheckRole" HeaderText="Void" />
  18. </Columns>
  19. </asp:GridView>



I'm using the gridview's RowDataBound event to do the label.Text assignment

  1. protected void grdUsers_RowDataBound(object sender, GridViewRowEventArgs e)
  2. {
  3. if (e.Row.RowType == DataControlRowType.DataRow)
  4. {
  5. memUser = Membership.GetUser(Convert.ToString(DataBinder.Eval(e.Row.DataItem, "UserName")));
  6. userProfile = Profile.GetProfile(memUser.UserName);
  7.  
  8. Label lastName = (Label)grdUsers.FindControl("lblLastName");
  9. Label firstName = (Label)grdUsers.FindControl("lblFirstName");
  10. lastName.Text = userProfile.LastName;
  11. firstName.Text = userProfile.FirstName;
  12. }
  13. }

It's failing and giving me an error message stating that lastName is null. What am I doing wrong here? Any and all assistance is much appreciated. Thanks in advance.
Last edited by peter_budo; Mar 30th, 2008 at 6:29 am. Reason: Keep It Organized - please use [code] tags
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 20
Reputation: ravichandra is an unknown quantity at this point 
Solved Threads: 5
ravichandra ravichandra is offline Offline
Newbie Poster

Re: Accessing ItemTemplate server control properties in a gridview

 
0
  #2
Mar 29th, 2008
You need to bind the labels text to some field value like Text='<%#Bind(ColumnName) %>'

Since you are not binding i suppose you are getting the error for null values in
lblLastName,lblFirstName
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 19
Reputation: postmaster is an unknown quantity at this point 
Solved Threads: 1
postmaster postmaster is offline Offline
Newbie Poster

Re: Accessing ItemTemplate server control properties in a gridview

 
0
  #3
Apr 1st, 2008
Another thought:
Do you allow NULL value in your database for LastName field?
If yes then you need to handle tat NULL in your code.

Postmaster
<snipped>
Last edited by peter_budo; Apr 1st, 2008 at 7:16 am. Reason: Keep It Spam-Free - Do not spam, advertise, plug your website, or engage in any other type of self promotion.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the ASP.NET Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC