I am working with the C#, ASP.NET framework [very new to this environment]. This is what I want to achieve:
- Pass data from jQuery Datepicker textbox to controller
- Parse the date, database query from the selected range
- Asynchronously, send queried rows back to page to update content
Here is the HTML:
<form id="date" runat="server">
<asp:Label AssociatedControlId="from_date" Text="From:" runat="server" />
<asp:TextBox ID="from_date" Text="" runat="server" />
<asp:Label AssociatedControlId="to_date" Text="To:" runat="server" />
<asp:TextBox ID="to_date" Text="" runat="server" />
</form>
I have this snippet on the client side to handle the change event:
var dates = $('#from_date, #to_date').datepicker({
if ( this.id == "to_date" )
$('#to_date').change();
});
To call in the controller:
protected void to_date_UpdateHandler( object sender, EventArgs e ) {
/* from here, I would query within the ranges in the "from_date"
and "to_date" textboxes */
}
Obviously, this will cause a page refresh, but I want to pass the data along asynchronously. How should I go about achieving this? Thank you.
It’s a little unclear from your question which particular jQuery ‘datepicker’ plugin you are using, so I will proceed to use the jQuery UI date picker for this example.
First off, there are some things you should be aware of when working with jQuery and ASP.NET WebFroms. Specifically, up until very recently, when server controls are rendered, their IDs get mangled by .NET. It is usually a good idea to stick to CSS classes when doing lots of client side scripting, but if you must use IDs, you can select controls like so:
Secondly, you will need to communicate with the server via
WebMethodsor by configuring an ASPX page to return XML or JSON. ASP.NET MVC really makes this easy, but it’s possible in WebForms and definitely worth your time (I despise UpdatePanels).Now to some code.
ASPX:
ASPX.CS