Search code examples
jqueryasp.netdatepickerweb-controls

How to use jquery Datepicker in web control in asp.net?


i am creating a web-control and i want to use jquery date-picker on this control, i have tried the below things: 1). apply all the required jquery code in web-control(but it does not work for me.) 2). apply all the required jquery code in page where i am calling that web-control(but it does not work for me.)

Please help me,

Actually what is my purpose to do it is: i want to create a generic module for search, to which i will just pass the name of table it would be work automatically, what i have completed up to now is:

1). bind all the columns in a dropdown list now what i want to do is: if the column type is DateTime then it will show a textbox with jquery calendar.

Please help me to resolve this problem, My code is:

<table>
<tr>
    <td>
        <asp:DropDownList ID="drpColumnName" Width="150px" runat="server" AutoPostBack="true"
            OnSelectedIndexChanged="drpColumnName_SelectedIndexChanged">
        </asp:DropDownList>
    </td>
    <td>
        <asp:DropDownList ID="drpOperation" Width="150px" runat="server">
        </asp:DropDownList>
    </td>
    <td>
        <asp:DropDownList ID="drpCondition" Width="150px" runat="server">
            <asp:ListItem Text="Or" Selected="True" Value="0"></asp:ListItem>
            <asp:ListItem Text="And" Value="1"></asp:ListItem>
        </asp:DropDownList>
    </td>
    <td>
        <asp:TextBox ID="txtSearchText" runat="server" Width="150px"></asp:TextBox>
        <asp:TextBox ID="txtDateSearch" CssClass="myClass" runat="server" Width="150px"></asp:TextBox>
    </td>
    <td>
        <asp:Button ID="btnAddConditionToSearch" runat="server" Text="AddToSearch" OnClick="btnAddConditionToSearch_Click" />
    </td>
</tr>
<tr>
    <td colspan="5">
        &nbsp;
    </td>
</tr>
</table>

Page's code is :

  <form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
    <Search:ctrlSearch ID="cntrlSearch" runat="server" />
</div>
</form>

Jquery code is:

     <script type="text/javascript">
$(document).ready(function () {


    var prm = Sys.WebForms.PageRequestManager.getInstance();
    prm.add_initializeRequest(InitializeRequest);
    prm.add_endRequest(EndRequest);
    // Place here the first init of the DatePicker   

    $('.myclass').datepicker();


});

function InitializeRequest(sender, args) { }
function EndRequest(sender, args) {
    // after update occur on UpdatePanel re-init the DatePicker 

    $('.myclass').datepicker();


}

Please help me,


Solution

  • Locate your date picker by a class selector.This is much easier than trying to locate ids.In case you have your controls sandwiched in an update panel you have two initialize the datepicker twice.Do this

      <script type="text/javascript">
      $(document).ready(function () {
    
    
            var prm = Sys.WebForms.PageRequestManager.getInstance();
            prm.add_initializeRequest(InitializeRequest);
            prm.add_endRequest(EndRequest);
            // Place here the first init of the DatePicker   
    
            $('.myclass').datepicker();
    
    
        });
    
        function InitializeRequest(sender, args) { }
        function EndRequest(sender, args) {
            // after update occur on UpdatePanel re-init the DatePicker 
    
            $('.myclass').datepicker();
    
    
                 }
    </script>
    

    Markup`

      <asp:TextBox ID="txtDateSearch" CssClass="myClass" runat="server" Width="150px"></asp:TextBox>
    

    See demo files here