468,720 Members | 1,803 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,720 developers. It's quick & easy.

Displaying calendar month according to Datalist choice

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 !

Feb 26 '07 #1
6 3035
On Feb 26, 3:00 pm, "Zeba" <coolz...@gmail.comwrote:
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("yy yy")).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 ;

}

Feb 26 '07 #2
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 !

Feb 27 '07 #3
On Feb 27, 11:05 am, "Zeba" <coolz...@gmail.comwrote:
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...

Feb 27 '07 #4
Okayy...thanks..! That makes things clear..
On Feb 27, 4:12 pm, "Alexey Smirnov" <alexey.smir...@gmail.comwrote:
On Feb 27, 11:05 am, "Zeba" <coolz...@gmail.comwrote:
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
Feb 27 '07 #5
On Feb 27, 12:26 pm, "Zeba" <coolz...@gmail.comwrote:
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.

Feb 27 '07 #6
"Alexey Smirnov" <al************@gmail.comwrote in message
news:11*********************@h3g2000cwc.googlegrou ps.com...
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...
Feb 27 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Hitit | last post: by
11 posts views Thread by samuelberthelot | last post: by
1 post views Thread by ajmera.puneet | last post: by
19 posts views Thread by skyblue | last post: by
7 posts views Thread by William (Tamarside) | last post: by
1 post views Thread by CARIGAR | last post: by
9 posts views Thread by bryonone | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.