| | |
Accessing ItemTemplate server control properties in a gridview
Please support our ASP.NET advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2008
Posts: 8
Reputation:
Solved Threads: 0
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).
I'm using the gridview's RowDataBound event to do the label.Text assignment
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.
asp Syntax (Toggle Plain Text)
<asp:GridView ID="grdUsers" runat="server" AllowPaging="True" AutoGenerateColumns="False" DataKeyNames="UserName,UserID" DataMember="DefaultView" DataSourceID="dsUsers" EmptyDataText="There are no users"> <Columns> <asp:HyperLinkField DataTextField="UserName" DataNavigateUrlFields="UserName" DataNavigateUrlFormatString="~/AddUser.aspx?UserName={0}" HeaderText="Login Name" /> <asp:TemplateField> <ItemTemplate> <asp:Label ID="lblLastName" runat="server"></asp:Label> <asp:Label ID="lblFirstName" runat="server"></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:CheckBoxField DataField="AdminRole" HeaderText="Admin" /> <asp:CheckBoxField DataField="AdminUsersRole" HeaderText="AdminUser" /> <asp:CheckBoxField DataField="ChangeProfileRole" HeaderText="Prof" /> <asp:CheckBoxField DataField="CreateAccountRole" HeaderText="Add" /> <asp:CheckBoxField DataField="DeleteAccountRole" HeaderText="Del" /> <asp:CheckBoxField DataField="EmulationRole" HeaderText="Emul" /> <asp:CheckBoxField DataField="ViewStatsRole" HeaderText="Stat" /> <asp:CheckBoxField DataField="VoidCheckRole" HeaderText="Void" /> </Columns> </asp:GridView>
I'm using the gridview's RowDataBound event to do the label.Text assignment
asp Syntax (Toggle Plain Text)
protected void grdUsers_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { memUser = Membership.GetUser(Convert.ToString(DataBinder.Eval(e.Row.DataItem, "UserName"))); userProfile = Profile.GetProfile(memUser.UserName); Label lastName = (Label)grdUsers.FindControl("lblLastName"); Label firstName = (Label)grdUsers.FindControl("lblFirstName"); lastName.Text = userProfile.LastName; firstName.Text = userProfile.FirstName; } }
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
•
•
Join Date: May 2006
Posts: 19
Reputation:
Solved Threads: 1
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>
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.
![]() |
Other Threads in the ASP.NET Forum
- Previous Thread: Master Page Links Problem
- Next Thread: Upload image without using fileupload
| Thread Tools | Search this Thread |
.net 2.0 3.5 ajax alltypeofvideos appliances asp asp.net beginner box browser businesslogiclayer button c# cac checkbox class commonfunctions compatible content contenttype control countryselector courier dataaccesslayer database datagrid datagridview datalist deployment development dgv dialog dropdownlist dropdownmenu dynamic dynamically edit embeddingactivexcontrol fileuploader fill findcontrol flash flv formview gridview gudi iis javascript list listbox login menu microsoft mouse mssql nameisnotdeclared news novell numerical opera order panelmasterpagebuttoncontrols problem radio ratings redirect registration relationaldatabases reportemail schoolproject search security serializesmo.table sessionvariables silverlight smoobjects software sql sql-server sqlserver2005 ssl tracking treeview validatedate validation vb.net videos vista visual-studio visualstudio vs2008 web webapplications webarchitecture webdevelopment webprogramming webservice wizard xsl





