Question

I've a jQuery date picker on my asp.net form. Everything seems to be working fine, except when I submit the form, the date chosen is lost from the textbox and the .net code gives me an error because the date is missing.

Anyone know why this might be happening?

It starts working when I remove the readonly=true from the textbox, but I need it to be readonly to prevent the user from typing in the date incorrectly.


Some code/script as requested:

HTML:

<form id="form1" runat="server">
    <asp:TextBox id="txtDate" Runat="server" readonly="true" />
    <asp:Button ID="btnSendRequest" text="Send Request" Runat="server" OnClick="btnSendRequest_Click" />
</form>

jQuery:

$("#txtDate").datepicker({
    dateFormat: "dd/mm/yy"
});

.NET:

Sub btnSendRequest_Click(ByVal Sender As Object, ByVal E As EventArgs)
    objSQLCommand = New SqlCommand("insert query here", objConnection)

    'parameters here

    Try
        objSQLCommand.Connection.Open()
        objSQLCommand.ExecuteNonQuery()
        objSQLCommand.Connection.Close()
    Catch Ex As Exception
        ' some error here
    End Try
End Sub
Was it helpful?

Solution

.Net won't get the value if the readonly flag is set. You'll need to remove that for it to work. I'm not sure if enabled = false will give you the same issue, but personally I'd stop user interaction through jquery seeing as you're using it anyway.

Or you could add an attribute of readonly in codebehind, this sometimes works and allows .net to still read the value:

txtYourTextBox.Attributes.Add("readonly", "readonly");

OTHER TIPS

the first problem is this

$("#<%=txtDate.ClientID%>").datepicker({
   dateFormat: "dd/mm/yy"
});

add client id to txtDate

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top