简体   繁体   English

如何在C#中获取文本框控件

[英]How to get Textbox control in C#

I have a HTML textbox 我有一个HTML文本框

<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"; 现在我需要将文本框的值设置为“ abcd”;

I tried datepicker.Text = "abcd"; 我尝试过datepicker.Text = "abcd"; Error is "the name datepicker does not exist in current context"... 错误为"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: 首先,您需要使用runat="Server"属性使<input>控制服务器端标签:

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

Then, you can modify the value using C# code: 然后,您可以使用C#代码修改值:

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: 如果要使用jQuery在客户端修改值,我首先建议将ID设为静态:

<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. 页面加载后的任何位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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