[thelist] error using .NET calendar to set value of textbox

Tab Alleman talleman at Lumpsum.com
Mon Oct 1 13:36:28 CDT 2007


Ahhh, I somehow glossed over the fact that your controls are in a template field.  In that case you do need to use FindControl.  I would handle this in the parent's (Gridview or whatever you're using) RowDataBound event handler:

		Dim txtHireDate as TextBox
		Dim calHireDate as Calendar

		If e.Row.RowType = DataControlRowType.DataRow Then
			txtHireDate = DirectCast(e.Row.Cells(x).Controls(y), TextBox)
			calHireDate = DirectCast(e.Row.Cells(x).Controls(y), Calendar)
		End If

(You'd need to replace the x's and y's with the appropriate values.  If you're not sure, you can step through the code and then look at the values of possible e.Row.Cells(x).Controls(y) permutations for textboxes and calendars.)

Actually, if your calendar is also in a template field, I'm surprised the event handler is even firing the way you've got it coded.  I would think you'd need to have your code in the parent's RowCommand handler.   So I may actually be out of my depth here... but I hope I've helped in some way.



> -----Original Message-----
> From: thelist-bounces at lists.evolt.org
> [mailto:thelist-bounces at lists.evolt.org]On Behalf Of Joel D Canfield
> Sent: Monday, October 01, 2007 11:21 AM
> To: thelist at lists.evolt.org
> Subject: Re: [thelist] error using .NET calendar to set value 
> of textbox
> 
> 
> > You shouldn't have to do a FindControl.  txtHireDate.Text = 
> > ... should do it.
> 
> It doesn't; more below
> 
> > If you're using ASP 1.1 with the code-behind model
> 
> 2.0
> 
> > Protected WithEvents txtHireDate As 
> System.Web.UI.WebControls.TextBox
> 
> all there as expected, including the calendar declaration
> 
> Here's the entire contents of the vb codebehind:
> 
> 	Partial Class updateemployee
> 		Inherits System.Web.UI.Page
> 		Protected DeptDDL As DropDownList
> 		Protected WithEvents txtHireDate As TextBox
> 		Protected WithEvents calHireDate As Calendar
> 		Sub Page_Load()
> 			DeptDDL.Items.Insert(0, New
> ListItem("Information Technology", 9))
> 		End Sub
> 		Sub calHireDate_SelectionChanged(ByVal sender As Object,
> ByVal e As EventArgs)
> 			txtHireDate.Text =
> calHireDate.SelectedDate.ToString("d")
> 		End Sub
> 	End Class
> 
> and the entire template field:
> 
>   <asp:TemplateField HeaderText="HireDate" SortExpression="HireDate">
>     <ItemTemplate>
>       <asp:Label 
>         ID="HireDateLabel"
>         Runat="Server"
>         Text='<%# Eval("HireDate", "{0:d}") %>' />
>     </ItemTemplate>
>     <EditItemTemplate>
>       <asp:Label 
>         id="lblHireDate"
>         Text="Hire Date:"
>         AssociatedControlID="txtHireDate"
>         Runat="server" />
>       <asp:TextBox
>         id="txtHireDate"
>         Text='<%# Eval("HireDate", "{0:d}") %>'
>         Runat="server" />
>       <img src="images/calendar.gif" onclick="displayCalendar()"
> width="16" height="17" />
>       <div id="datePicker">
>         <asp:Calendar
>           id="calHireDate"
>           OnSelectionChanged="calHireDate_SelectionChanged" 
>           Runat="server"></asp:Calendar>
>         </div>
>     </EditItemTemplate>
>     <ItemStyle CssClass="datacell" />
>   </asp:TemplateField>
> 
> when I select a date in the popup calendar, I get the error "Object
> reference not set to an instance of an object" referencing this line:
> 
>     Line 10:  txtHireDate.Text = 
> calHireDate.SelectedDate.ToString("d")
> 
> I don't even know which object it's referring to, 'txtHireDate' or
> 'calHireDate'
> 
> the continued assistance continues to be appreciated :)
> 
> joel
> -- 
> 
> * * Please support the community that supports you.  * *
> http://evolt.org/help_support_evolt/
> 
> For unsubscribe and other options, including the Tip Harvester 
> and archives of thelist go to: http://lists.evolt.org 
> Workers of the Web, evolt ! 
> 



More information about the thelist mailing list