Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

object reference problem, textbox, datagrid, clientID

Status
Not open for further replies.

tigerjade

Programmer
Mar 17, 2004
237
US
It's me again. :/ I'm trying to get the ClientID of an object on the ItemDataBound event to attach to another object. When I run the code, I get the System.NullReferenceException error: Object reference not set to an instance of the object.

.aspx code:
Code:
<asp:TemplateColumn HeaderText="Return Date">
						<ItemTemplate>
							<asp:Label ID="dateLabel" Runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "detReturnDateTime", "{0:D}")%>' />
						</ItemTemplate>
						<EditItemTemplate>
							<asp:TextBox ID="updateDateText" Runat="server" Font-Size="8pt" Text='<%# DataBinder.Eval(Container.DataItem, "detReturnDateTime", "{0:D}")%>' ReadOnly="True"></asp:TextBox><asp:HyperLink id="lbtnCalendar" runat="server" ImageUrl="images/calendar.gif"></asp:HyperLink>
						</EditItemTemplate>
					</asp:TemplateColumn>

.aspx.cs code:
Code:
protected void UserDetailsDataGrid_ItemDataBound(object sender, DataGridItemEventArgs e)
		{
			if (e.Item.ItemType == ListItemType.Item) 
			{
				TextBox udt = (TextBox)e.Item.FindControl("updateDateText");
				HyperLink calHL = e.Item.FindControl("lbtnCalendar") as HyperLink;
				calHL.NavigateUrl = "javascript:openWin('PopupCalendar.aspx?field=Form1" + udt.ClientID + "','newWin','width=250,height=290,toolbars=no,scrollbars=no,resizable=yes');";
			}
		}

What am I doing wrong this time?

Thanks!

tigerjade

"Always code as if the person who ends up maintaining your code will be a violent psychopath who knows where you live." -- Martin Golding


 
As usual, I get to my wits' end, post here, and then find an answer. I think my big screwup was in determining the index; I found this code:

Code:
		protected void UserDetailsDataGrid_ItemDataBound(object sender, DataGridItemEventArgs e)
		{
			if (e.Item.ItemType == ListItemType.EditItem) 
			{
				Int32 iLastCellIndex = e.Item.Cells.Count-1;
				String sTextBoxName = e.Item.Cells[iLastCellIndex].FindControl("updateDateText").ClientID;
				TextBox udt = e.Item.FindControl("updateDateText") as TextBox;
				HyperLink calHL = e.Item.FindControl("lbtnCalendar") as HyperLink;
				calHL.NavigateUrl = "javascript:openWin('PopupCalendar.aspx?field=Form1." + sTextBoxName + "','newWin','width=250,height=290,toolbars=no,scrollbars=no,resizable=yes');";
			}
		}

And it worked like a charm!

tigerjade

"Always code as if the person who ends up maintaining your code will be a violent psychopath who knows where you live." -- Martin Golding


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top