| | |
Accessing ItemTemplate server control properties in a gridview
Please support our ASP.NET advertiser: $4.95 a Month - ASP.NET Web Hosting – Click Here!
![]() |
•
•
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 activexcontrol advice ajax alltypeofvideos asp asp.net bc30451 beginner bottomasp.net browser businesslogiclayer c# cac checkbox class commonfunctions compatible content contenttype countryselector courier css dataaccesslayer database datagrid datagridview datagridviewcheckbox datalist deployment development dgv dropdownlist dropdownmenu dynamic dynamically edit embeddingactivexcontrol fileuploader fill findcontrol flash flv formatdecimal formview gridview gudi iframe iis javascript listbox menu microsoft mouse mssql multistepregistration nameisnotdeclared news opera panelmasterpagebuttoncontrols problem redirect registration relationaldatabases reportemail schoolproject security serializesmo.table sessionvariables silverlight smartcard smoobjects software sql sql-server sqlserver2005 ssl textbox tracking treeview unauthorized validatedate validation vb.net video videos virtualdirectory vista visual-studio visualstudio web webapplications webarchitecture webdevelopemnt webdevelopment webprogramming webservice youareanotmemberofthedebuggerusers





