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

4

1 回答 1

5

您首先需要使用以下属性使您的<input>控件成为服务器端标记:runat="Server"

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

然后,您可以使用 C# 代码修改该值:

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

如果您想使用 jQuery 在客户端修改值,我首先建议将 ID 设为静态:

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

然后你可以这样做:

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

页面加载后的任何地方。

于 2013-08-05T15:23:34.807 回答