1

I have a HTML textbox

<script>
    $(function () {
        $("#datepicker").datepicker({
            changeMonth: true,
            changeYear: true
        });
    });
</script>

<input type="text" size="10" name="datepicker" id="datepicker" />

Now i need to set the value of textbox to "abcd";

I tried datepicker.Text = "abcd"; Error is "the name datepicker does not exist in current context"...

I tried finding the Control and assigning the value but still could not do it.

Is there any other way to do it?? Thanks

1
  • 4
    You need to learn the difference between client-side code and server-side code. Commented Aug 5, 2013 at 15:23

1 Answer 1

5

You'll first need to make your <input> control a server side tag, using the runat="Server" attribute:

<input runat="server" type="text" size="10" name="datepicker" id="datepicker" />

Then, you can modify the value using C# code:

protected void Page_Load(object sender, EventArgs e)
{
   datepicker.Text = "New Value"; // Initial value for input field
}

If you want to modify the value on the client side, using jQuery, I'd first suggest making the ID static:

<input runat="server" ClientIDMode="Static" type="text" size="10" name="datepicker" id="datepicker" />

Then you can do:

$("#datepicker").val('New Value');

Anywhere after the page has been loaded.

Sign up to request clarification or add additional context in comments.

3 Comments

Not a problem, glad I could help!
if you didnt want to use the ClientIDMode="Static" option you could use the <%=datepicker.ClientID %> in js
the value "abcd" is coming from database. Then how can i set value to "datepicker" in .aspx.cs file?? datepicker.Text is not working and if i add runat="server" then jquery doesnt work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.