Displaying calendar month according to Datalist choice

Z

Zeba

Hi,

I have a page with a calendar and two datalist items - one containing
month values and the other, year values. Depending on the month/year
value chosen ( in text/string format ) I should be able to display
the correct month of the calendar.

I thought it would be possible to use the OnSelectedIndexChanged
attribute of the dropdown list to call a javascript function that sets
the month value of the calendar...Something like :

<asp:DropDownList ID="MonthList" runat="server" AutoPostBack="True"
DataSourceID="AuctionMonthDataSource" OnSelectedIndexChanged=getcal()</asp:DropDownList>


But how do I get access to the SelectedDate tag from inside getcal() ?
And do I have to manually convert the text values of the month into
numeric values ?

Thanks !
 
G

Guest

Hi,

I have a page with a calendar and two datalist items - one containing
month values and the other, year values. Depending on the month/year
value chosen ( in text/string format ) I should be able to display
the correct month of the calendar.

I thought it would be possible to use the OnSelectedIndexChanged
attribute of the dropdown list to call a javascript function that sets
the month value of the calendar...Something like :

<asp:DropDownList ID="MonthList" runat="server" AutoPostBack="True"
DataSourceID="AuctionMonthDataSource" OnSelectedIndexChanged=getcal()

</asp:DropDownList>

But how do I get access to the SelectedDate tag from inside getcal() ?
And do I have to manually convert the text values of the month into
numeric values ?

Thanks !


<asp:DropDownList id="ddlMonth" style="Z-INDEX: 101; LEFT: 64px;
POSITION: absolute; TOP: 24px" runat="server"

AutoPostBack="True"></asp:DropDownList>

<asp:Label id="Label1" style="Z-INDEX: 103; LEFT: 8px; POSITION:
absolute; TOP: 24px" runat="server">Month</asp:Label>

<asp:DropDownList id="ddlYear" style="Z-INDEX: 102; LEFT: 200px;
POSITION: absolute; TOP: 24px" runat="server"

AutoPostBack="True"></asp:DropDownList>

<asp:Label id="Label2" style="Z-INDEX: 104; LEFT: 152px; POSITION:
absolute; TOP: 24px" runat="server">Year</asp:Label>

<asp:Calendar id="Calendar1" style="Z-INDEX: 105; LEFT: 16px;
POSITION: absolute; TOP: 72px" runat="server"></asp:Calendar>




VB.NET



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

If Not Page.IsPostBack Then

'Populate month in the dropdownlist

Dim strMonth As String = ""

Dim i As Integer

For i = 1 To 12

If i.ToString().Length < 2 Then

strMonth = "0" + i.ToString()

ddlMonth.Items.Add(New ListItem(strMonth, strMonth))

Else

ddlMonth.Items.Add(New ListItem(strMonth, strMonth))

End If

Next




ddlMonth.Items.FindByValue(DateTime.Now.ToString("MM")).Selected =
True

'Populate year in the dropdownlist

Dim j As Integer

For j = 1900 To 2050

ddlYear.Items.Add(New ListItem(j.ToString(),
j.ToString()))

Next


ddlYear.Items.FindByText(DateTime.Now.ToString("yyyy")).Selected =
True

End If

End Sub 'Page_Load





Private Sub ddlMonth_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
ddlMonth.SelectedIndexChanged

SetCalendarDate()

End Sub 'ddlMonth_SelectedIndexChanged



Private Sub ddlYear_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
ddlYear.SelectedIndexChanged

SetCalendarDate()

End Sub 'ddlYear_SelectedIndexChanged



Sub SetCalendarDate()

Dim dtNewDate As DateTime

dtNewDate =
DateTime.Parse((Int16.Parse(ddlMonth.SelectedItem.Text) & "/1/" &
Int16.Parse(ddlYear.SelectedItem.Text)))

Calendar1.TodaysDate = dtNewDate

End Sub 'SetCalendarDate




C#



private void Page_Load(object sender, System.EventArgs e)

{

// Put user code to initialize the page here

if (!Page.IsPostBack )

{

//Populate month in the dropdownlist

string strMonth="";

for(int i = 1 ;i<=12;i++)

{

if (i.ToString().Length <2 )

{

strMonth ="0" + i.ToString ();

ddlMonth.Items.Add (new
ListItem(strMonth,strMonth )) ;

}

else

{

ddlMonth.Items.Add (new
ListItem(strMonth,strMonth )) ;

}

}

ddlMonth.Items.FindByValue ( DateTime.Now.ToString
("MM")).Selected =true;

//Populate year in the dropdownlist

for(int j = 1900 ;j<=2050;j++)

{

ddlYear.Items.Add (new ListItem(j.ToString(),j.ToString
() )) ;

}

ddlYear.Items.FindByText (DateTime.Now.ToString
("yyyy")).Selected =true;

}

}



private void ddlMonth_SelectedIndexChanged(object sender,
System.EventArgs e)

{

SetCalendarDate();

}

private void ddlYear_SelectedIndexChanged(object sender,
System.EventArgs e)

{

SetCalendarDate();

}

void SetCalendarDate()

{

DateTime dtNewDate;

dtNewDate =DateTime.Parse
(Int16.Parse(ddlMonth.SelectedItem.Text) + "/1/" +
Int16.Parse( ddlYear.SelectedItem.Text));

Calendar1.TodaysDate=dtNewDate ;

}
 
Z

Zeba

Thanks Alexey,
My basic idea itself was flawed i guess...Is it actually possible to
call a javascript function from a server control for such a purpose ?
Btw, why did you use "private" void ddlYear_SelectedIndexChanged( ) ?
The .aspx page inherits from the .cs file right ? It gave me an error
that it was inaccessible due to protection level, so I changed it to
protected.
-Thanks !
 
G

Guest

Thanks Alexey,
My basic idea itself was flawed i guess...Is it actually possible to
call a javascript function from a server control for such a purpose ?

The OnSelectedIndexChanged event executes on the server, when page is
posted back to the server and that is the getcal() event cannot be on
the client side. The Calendar is also a server control and to set the
selected date you need to execute a code on the server side.

If you really need to avoid the postback, you should not use the
server controls (at least for calendar). Use a javascript calendar
instead...
 
Z

Zeba

Okayy...thanks..! That makes things clear..


The OnSelectedIndexChanged event executes on the server, when page is
posted back to the server and that is the getcal() event cannot be on
the client side. The Calendar is also a server control and to set the
selected date you need to execute a code on the server side.

If you really need to avoid the postback, you should not use the
server controls (at least for calendar). Use a javascript calendar
instead
 
G

Guest

Okayy...thanks..! That makes things clear..

Unfortunately I don't have any good example for this. You can google
for "javascript calendar asp.net" to look for most suitable code. It's
really depend on the need.
 
M

Mark Rae

Unfortunately I don't have any good example for this. You can google
for "javascript calendar asp.net" to look for most suitable code. It's
really depend on the need.

I have one which is free to anyone who contacts me privately and asks for
it...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top