简体   繁体   中英

How to get Textbox control in C#

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM