Search code examples
jqueryasp.netsqlparameter

Get values from jscript datepicker in form


I'm trying to use a jscript Datepicker UI calendar when I try to pass the text properties to an sqlparameter in asp.net .cs code. behind I can see the cldStartProj text box object but I can't see the cldEstProjEnd or cldEndProj textboxes.

I am getting an error saying:

"the name 'cldEstEndDate' does not exist in the current context"

How can I get the values from my aspx form from theses datepicker boxes?

<script>
    $(function () {
        $("#cldStartProj").datepicker();
    });
    $(function () {
        $("#cldEndProj").datepicker();
    });
    $(function () {
        $("#cldEstProjEnd").datepicker();
    });
</script>

<p><input type="datetime" id="cldStartProj"></p>
<p><input type="datetime" id="cldEstProjEnd"></p>
<p><input type="datetime" id="cldEndProj"></p>

cmdAdd.Parameters.AddWithValue("@ProjStartDate", cldStartProj.Text);
cmdAdd.Parameters.AddWithValue("@ProjEstEndDate", cldEstProjEnd.Text);
cmdAdd.Parameters.AddWithValue("@ProjEstEndDate", cldEndProj.Text);

Solution

  • You can use asp:TextBox for the datepicker. Then you can access it on the .cs code.

    <asp:TextBox CssClass="datePicker" ID="cldStartProj" runat="server" />
    <asp:TextBox CssClass="datePicker" ID="cldEstProjEnd" runat="server"  />
    <asp:TextBox CssClass="datePicker" ID="cldEndProj" runat="server" />
    

    Then in the script,

    $(function () {
        $(".datePicker").datepicker();
    });
    

    Use class for binding the datepicker. Id will be change after rendering as it has the attribute runat=server.