You're Browsing As A Guest! Register Now... |
||
|
Become a ASP.NET Expert!
Join 416,729 ASP.NET Programmers for FREE! Get instant access to thousands
of ASP.NET experts, tutorials, code snippets, and more! There are 2,940 people online right now.Registration is fast and FREE... Join Now!
|
||
Page 1 of 1
date time picker
#2
Re: date time picker
Posted 22 May 2009 - 04:13 AM
#4
Re: date time picker
Posted 29 May 2009 - 01:36 PM
Ok, so what if i have 3 text box with 3 buttons associated with them but only one calendar control.
I click the button it makes the calendar visible, at which point the user selects a date and its supposed to go to the text box.
Catch is...i dont have a calendar for each text box...just 1 calendar to feed 3 text boxes. I created a page-level variable...
which upon one of the buttons click does...
Then the user causes said event to happen...
But the catch is for some reason the variable calTextBox is not carrying the object reference of the output text box. Keep getting a NullReferenceException.
I tried doing...
except the data goes no where but everything in the calendar event completes.
Everything above is on the same form and is in the same view of a multiview.
I click the button it makes the calendar visible, at which point the user selects a date and its supposed to go to the text box.
Catch is...i dont have a calendar for each text box...just 1 calendar to feed 3 text boxes. I created a page-level variable...
Dim calTextBox As TextBox
which upon one of the buttons click does...
calTextBox = txtNewProjectStart clndrNewProject.Visible = True
Then the user causes said event to happen...
Protected Sub clndrNewProject_Selectionchanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles clndrNewProject.Selectionchanged calTextBox.Text = clndrNewProject.SelectedDate.ToString clndrNewProject.Visible = False End Sub
But the catch is for some reason the variable calTextBox is not carrying the object reference of the output text box. Keep getting a NullReferenceException.
I tried doing...
Dim calTextBox As New Web.UI.WebControls.TextBox
except the data goes no where but everything in the calendar event completes.
Everything above is on the same form and is in the same view of a multiview.
#5
Re: date time picker
Posted 29 May 2009 - 07:55 PM
First off keep in mind that every time the page posts back everything is re-instantiated including the page itself. This means that your TextBox control is being created each time the page posts back. And since you are not using the New keyword you are getting the NullReferenceException.
I would suggest an alternative to the method you are attempting to use. Since you are only using one calendar control to populate three different textboxes, you need to know which button that is associated to each textbox initiated the Click event. Store the name of the button in a Session variable and then check that value in the Selectionchanged event of the calendar control.
To illustrate this, here is an example:
Here is the code for the page itself. 3 textboxes, 3 buttons, and 1 calendar control.
Each button calls the same event. First create a button in the event and then cast the sender object to the correct type. The sender object will tell us which button called the event. Once we have the correct button, store the ID in a Session variable and then show the calendar.
Now in the Selectionchanged event of the calendar get the value stored in the Session variable and use it to determine the logical path to follow and put the date in the correct textbox. Lastly, hide the calendar control again until the next button is clicked.
I hope that helps explain a fairly simple method of accomplishing the task you desire.
I would suggest an alternative to the method you are attempting to use. Since you are only using one calendar control to populate three different textboxes, you need to know which button that is associated to each textbox initiated the Click event. Store the name of the button in a Session variable and then check that value in the Selectionchanged event of the calendar control.
To illustrate this, here is an example:
Here is the code for the page itself. 3 textboxes, 3 buttons, and 1 calendar control.
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtDate1" runat="server"></asp:TextBox>
<asp:Button ID="btnDate1" runat="server" Text="Date1" /><br />
<asp:TextBox ID="txtDate2" runat="server"></asp:TextBox>
<asp:Button ID="btnDate2" runat="server" Text="Date2" /><br />
<asp:TextBox ID="txtDate3" runat="server"></asp:TextBox>
<asp:Button ID="btnDate3" runat="server" Text="Date3" /><br />
<asp:Calendar ID="Calendar1" runat="server" Visible="false"></asp:Calendar>
</div>
</form>
</body>
</html>Each button calls the same event. First create a button in the event and then cast the sender object to the correct type. The sender object will tell us which button called the event. Once we have the correct button, store the ID in a Session variable and then show the calendar.
Protected Sub btnDate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDate1.Click, btnDate2.Click, btnDate3.Click
'create a new button
Dim clickedButton As Button
'cast the sender to the correct type, a Button
clickedButton = CType(sender, Button)
'store the Button ID in a Session variable
Session("Buttonclicked") = clickedButton.ID
'show the calendar
Calendar1.Visible = True
End Sub
Now in the Selectionchanged event of the calendar get the value stored in the Session variable and use it to determine the logical path to follow and put the date in the correct textbox. Lastly, hide the calendar control again until the next button is clicked.
Protected Sub Calendar1_Selectionchanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Calendar1.Selectionchanged
'verify that a Session variable is valid before attempting to use it
If Session("Buttonclicked") <> Nothing Then
'now check the session variable for the ID of the button
'this will indicate which textbox to put the date into
If Session("Buttonclicked").ToString().Equals("btnDate1") Then
txtDate1.Text = Calendar1.SelectedDate.ToString()
ElseIf Session("Buttonclicked").ToString().Equals("btnDate2") Then
txtDate2.Text = Calendar1.SelectedDate.ToString()
Else
txtDate3.Text = Calendar1.SelectedDate.ToString()
End If
End If
'hide the calendar
Calendar1.Visible = False
End Sub
I hope that helps explain a fairly simple method of accomplishing the task you desire.
#6
Re: date time picker
Posted 01 June 2009 - 07:10 AM
Yeah i keep forgetting about the postback event. As well, forgot about the session variables. Looks like ill be creating some variables in the session_start event in the app.config.
I have alot more togo, but thanx for bringing that old strategy back, havent used session variables since i started playing with PHP and then moved to ASP.
I have alot more togo, but thanx for bringing that old strategy back, havent used session variables since i started playing with PHP and then moved to ASP.
Page 1 of 1


Ask A New Question
Reply





MultiQuote








|