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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ASP.NET Newbie Problem with Calendar Control

Status
Not open for further replies.

tmunson99

MIS
Jun 18, 2004
62
US
I have created a calendar control so I can reuse it on various web forms. I can select a date from the calendar at run time and it appears on my web form. However, what I cannot figure out is how to capture that date in my current form. The text box in my control is txtCalendarDate, however my web form does not see this text box. It's simply a control. The code for my calendar control is:

Public MustInherit Class CalendarControl
Inherits System.Web.UI.UserControl
Protected WithEvents txtCalendarDate As System.Web.UI.WebControls.TextBox
Protected WithEvents Calendar1 As System.Web.UI.WebControls.Calendar

...

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
End Sub

Private Sub Calendar1_SelectionChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Calendar1.SelectionChanged

txtCalendarDate.Text = Calendar1.SelectedDate.ToShortDateString()
Dim div As System.Web.UI.Control = Page.FindControl("divCalendar")

If TypeOf div Is HtmlGenericControl Then
CType(div, HtmlGenericControl).Style.Add("display", "none")
End If

End Sub

End Class

In other words, how do I pass this txtCalendarDate to the form I have the control on?

 
Try setting the Textbox to Public rather than Protected.

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
I'm not sure where you mean to change that. I made the following change in the calendar control itself:

Public MustInherit Class CalendarControl
Inherits System.Web.UI.UserControl
Public txtCalendarDate As System.Web.UI.WebControls.TextBox
Protected WithEvents Calendar1 As System.Web.UI.WebControls.Calendar

It still doesn't work. When I am in my webform, I can add and see the calendar control, the controls works, when the date is selected it appears in the control's text box. However, I want to pass that date to the database along with other data entered in the other text boxes and I don't know how to refer to it. I'm in a webform that is using the control. I'm probably making this way more complicated that it is.
 
OL to explain I've created a simple calendar user control (MyCalendar.ascx) and a simple web page (WebForm1.aspx).

MyCalendar.aspx:
Code:
<%@ Control Language="vb" AutoEventWireup="false" Codebehind="MyCalendar.ascx.vb" Inherits="WebApplication1.MyCalendar" TargetSchema="[URL unfurl="true"]http://schemas.microsoft.com/intellisense/ie5"[/URL] %>
<asp:Calendar id="Calendar1" runat="server"></asp:Calendar>
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>

MyCalendar.ascx.vb:
Code:
Public Class MyCalendar
    Inherits System.Web.UI.UserControl

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents Calendar1 As System.Web.UI.WebControls.Calendar
    Public WithEvents TextBox1 As System.Web.UI.WebControls.TextBox

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
    End Sub

    Private Sub Calendar1_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Calendar1.SelectionChanged
        TextBox1.Text = Calendar1.SelectedDate
    End Sub
End Class

WebForm1.aspx
Code:
<%@ Register TagPrefix="uc1" TagName="MyCalendar" Src="MyCalendar.ascx" %>
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="WebApplication1.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
	<HEAD>
		<title>WebForm1</title>
		<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
		<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
		<meta name="vs_defaultClientScript" content="JavaScript">
		<meta name="vs_targetSchema" content="[URL unfurl="true"]http://schemas.microsoft.com/intellisense/ie5">[/URL]
	</HEAD>
	<body>
		<form id="Form1" method="post" runat="server">
			<asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
			<uc1:MyCalendar id="MyCalendar1" runat="server"></uc1:MyCalendar>
		</form>
	</body>
</HTML>

WebForm1.aspx.vb:
Code:
Public Class WebForm1
    Inherits System.Web.UI.Page
    Protected WithEvents MyCalendar1 As MyCalendar
#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents Button1 As System.Web.UI.WebControls.Button

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object


    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        MyCalendar1.TextBox1.Text = "Test"
    End Sub
End Class

Now you will see in the user control I have set the TextBox to Public e.g.
Code:
    Public WithEvents TextBox1 As System.Web.UI.WebControls.TextBox

As I have now placed my user control on WbForm1.aspx and added my WithEvents line (with the same name as the id of my user control) in the line:
Code:
Protected WithEvents MyCalendar1 As MyCalendar
it now means that I can access the Public Textbox on the User Control and set it to "Test". e.g. the code that says:
Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        MyCalendar1.TextBox1.Text = "Test"
    End Sub

I hope all this made sense and you should be able to work through my example to see where you are going wrong in yours.

Please post if you have any questions.





----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
It works!

I made the declaration of the textbox in the calendar control public as suggested. What I was missing though was declaring the control in the webform:

I need this line of code:

Protected WithEvents MyCalendar1 As MyCalendar

I would have thought merely placing the control on the form would add at least that code. So my web form didn't know about the calendar control. Your samples were superb! Thanks for taking the time.

Teresa


 
i'm a newbie at this, and i'm a bit confused at the calendar.aspx and the calendar.ascx.vb.

its my understanding that you create the calendar.aspx as a new webform, but how do you create the calendar.ascx.vb? is it another separate webform?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top