Search code examples
jqueryasp.netdatetimepicker

How to create a jquery datepicker in code behind in ASP.NET?


I want to use a datetimepicker in aspx page using cs codes.But i have to create it dynamically in cs.I usually use jquery but another solutions can be accepted.


Solution

  • You can Create the Script dynamically that attaches the jquery script to the control:

    string csname1 = "BindDatePickerScript";
    Type cstype = this.GetType();
    
    // Get a ClientScriptManager reference from the Page class.
    ClientScriptManager cs = Page.ClientScript;
    // Check to see if the startup script is already registered.
    if (!cs.IsStartupScriptRegistered(cstype, csname1))
    {
    System.Text.StringBuilder cstext1 = new System.Text.StringBuilder();
    cstext1.Append("<script type='text/javascript'>");
    cstext1.Append("Your Script");
    cstext1.Append("});");
    cstext1.Append("</");
    cstext1.Append("script>");
    
    cs.RegisterStartupScript(cstype, csname1, cstext1.ToString());
    }
    

    This Script gets added at the bottom of the HTML rendered page.