Search code examples
c#asp.netcode-behindjquery-ui-datepicker

Jquery datepicker unable to capture value of SUBMIT control in code behind


I am using the jQuery DatePicker control. The control displays properly but I am unable to get the selected value in my code behind page. I attempt to capture the user-selected value using: DateTime StartDt = Convert.ToDateTime(Request.Form["datepicker"]); The user has several controls to select and then hits submit.

After submitting, when I inspect that value of StartDt it is: 01/01/0001 00:00:00AM

ASPX page:

<%@ Page Title="Fox Cities Restaurant Guide-Food Events" Language="C#" MasterPageFile="~/main_master.Master" AutoEventWireup="true" CodeBehind="Events.aspx.cs" Inherits="FCGuide.Events" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <link href="Styles/jquery-ui-1.8.21.custom.css" rel="stylesheet" type="text/css" />
 <script type="text/javascript">
     $(document).ready(function () {
         $("#datepicker").datepicker();
     });
  </script>
    <style type="text/css">
        #datepicker
        {
            width: 110px;
            height: 19px;
        }
    </style>
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <div id="defaultEvent">
        <div id="EventNav">
            <div id="EventType" class="EventNavDivs">
            <span class="EventTitle">Event Type</span> <br />
                <asp:DropDownList ID="ddlEventType" runat="server">
                    <asp:ListItem>Any Event</asp:ListItem>
                    <asp:ListItem>Art Shows</asp:ListItem>
                    <asp:ListItem>Farmers' Market</asp:ListItem>
                    <asp:ListItem>Festival</asp:ListItem>
                    <asp:ListItem>Restaurant Opening</asp:ListItem>
                </asp:DropDownList>
            </div>
            <div id="EventDate" class="EventNavDivs">
                <span class="EventTitle">Event Date</span> <br />
                <%--<asp:TextBox ID="datepicker" runat="server"></asp:TextBox>--%>
                <input id="datepicker" type="text" size="50px" maxlength="12" />
            </div>
            <div id="EventLocation" class="EventNavDivs">
                <span class="EventTitle">Location</span> <br />
                <asp:DropDownList ID="ddlEventCity" runat="server">
                </asp:DropDownList>
            </div>
            <div id="EventBtn" class="EventNavBtn">
                <br /><asp:Button ID="btnFindEvents" runat="server" Text="Submit" 
                    onclick="btnFindEvents_Click" />
            </div>
        </div>
        </div>
    </div>
</asp:Content>

Code Behind page:

protected void btnFindEvents_Click(object sender, EventArgs e)
{
    string EventType = ddlEventType.SelectedValue;
    DateTime StartDt = Convert.ToDateTime(Request.Form["datepicker"]);
    string City = ddlEventCity.SelectedValue;

        BLgetEvents obj = new BLgetEvents();
        var EventList = obj.getAllEvents(StartDt);

        ListEvents.DataSource = EventList;
        ListEvents.DataBind();
    }

Solution

  • change your .aspx code to this:

    <input id="datepicker" type="text" size="50px" maxlength="12" name="DatePickername" />
    

    then in your codebehind, do:

     string dt = Request.Form["DatePickername"];