Erik L
Erik L

Reputation: 126

How do you click a calendar and copy the date to a textbox without postback, in asp.net with javascript?

I have a asp.net page that is used for searching rows of data. I want to be able to search a specific date-interval, therefore I have added two calendars (from & to) and a textbox for each one.

When a date is selected in the calendar it is displayed in the matching textbox, although this causes a postback, and that is my problem.

I have never used javascript before, so I thought I would give that a go, but I almost exclusivly see examples using HTML, which I am not using at all. I only use ASP.NET web controls such as <asp:Calendar.../>.

So this code that follows is just off the top of my head, I know the javascript is totally wrong, but I hope you understand what it is that I want to achieve when you look at it.

<script type="text/javascript">
    function Calendar1_SelectionChanged() {
        FromDate.Text = Calendar1.SelectedDate.ToString();
    }
</script>    

<asp:Calendar ID="Calendar1" runat="server" 
    onselectionchanged="Calendar1_SelectionChanged"></asp:Calendar>

<asp:TextBox ID="FromDate" runat="server"></asp:TextBox>

All answers are greatly appreciated! I have been trying to find an answer to this by googling, but no luck..

EDIT: For this to work on the page, I need to keep the textboxes. Or any other way where the date can be edited manually (in DateTime format)

Upvotes: 0

Views: 5790

Answers (2)

Corey Ogburn
Corey Ogburn

Reputation: 24717

The problem here is that when the page is actually served to the browser, it's altered from what exactly you have posted there. The onselectionchanged event is not a javascript event, it does the postback to call the method Calendar1_SelectionChanged back at the server. Your javascript code, although the function appears to have the same name as the onselectionchanged event, is not being run.

My recommendation is to use a jQuery datepicker.

I'm also not sure your javascript is exactly what it should be to work as you expect it to. It looks like you're using a few C# properties/methods...

Upvotes: 4

ryoung
ryoung

Reputation: 846

I would look into the jQuery calendars instead of the asp ones personally. This would definitely avoid any postbacks. http://jqueryui.com/demos/datepicker/

Upvotes: 0

Related Questions