[Solved] jQuery DateTimePicker selected value is null in code behind in ASP.Net

Last Reply 2 months ago By dharmendr

Posted 2 months ago

Mostly for gridview, we just use row.findcontrol and get its text value

Also in my case, my textbox is in a regular form not inside a gridview

Then why textbox.text returns null ?

Refer

DateTimePicker in ASP.Net GridView using jQuery DateTimePicker Plugin

You are viewing reply posted by: dharmendr 2 months ago.
Posted 2 months ago
mcp111 says:
Then why textbox.text returns null?

 The value is assigned at client side to TextBox. So you are getting null.

To get the value Request.Form collection is used.

Refer below code.

HTML

<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script src="Scripts/jquery.dynDateTime.min.js" type="text/javascript"></script>
<script src="Scripts/calendar-en.min.js" type="text/javascript"></script>
<link href="Styles/calendar-blue.css" rel="stylesheet" type="text/css" />
<link href="Styles/calendar-blue.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
    $(document).ready(function () {
        $(".Calender").dynDateTime({
            showsTime: true,
            ifFormat: "%Y/%m/%d %H:%M",
            daFormat: "%l;%M %p, %e %m,  %Y",
            align: "BR",
            electric: false,
            singleClick: false,
            displayArea: ".siblings('.dtcDisplayArea')",
            button: ".next()"
        });
    });
</script>
<div>
    <asp:TextBox ID="txtDOB" runat="server" ReadOnly="true" class="Calender" />
    <img src="calender.png" />
    <br />
    <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />
</div>

C#

protected void btnSave_Click(object sender, EventArgs e)
{
    DateTime dob = Convert.ToDateTime(Request.Form["txtDOB"]);
    //Save the date to Database here.
}

VB.Net

Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs)
    DateTime.Parse(Request.Form("txtDOB"))
    'Save the date to Database here.
End Sub