[英]create hidden textbox, change it value in javascript and get new value. c#
I'm create a web custom control with hidden field: 我正在创建一个带隐藏字段的Web自定义控件:
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
hidden = new HtmlInputHidden();
hidden.ClientIDMode = System.Web.UI.ClientIDMode.Static;
hidden.ID = this.ID + "_hidden";
this.Controls.Add(hidden);
}
I change it value in javascript on the page: 我在页面上的javascript中更改了它的值:
$(textbox).text("some text");
Then try to get this value: 然后尝试获取此值:
string str = Request.Form[hidden.Name];
I get a null.... Also I tryed: 我得到了一个零....我也试过:
string str = Request.Form[hidden.ClientID]
and still get null. 仍然得到null。
Thanks. 谢谢。
So when accessing it from JavaScript you need to use this syntax: 因此,当从JavaScript访问它时,您需要使用以下语法:
$('#field_hidden').val("some text");
further, with the edit, I just noticed that you're not giving it a name as karaxuna stated. 此外,通过编辑,我只是注意到你没有给它一个名字,因为karaxuna说。 And finally, you'd need to make sure it's inside the
form
tag for it to be part of the Request.Form
. 最后,您需要确保它位于
form
标记内, form
使其成为Request.Form
一部分。
One other way of accessing its value though, even if it's built dynamically inside the Load
, is to grab its value with the Value
property. 另一种访问其值的方法是,即使它是在
Load
内部动态构建的,也是使用Value
属性获取其值。 However, make sure you do that after the Load
, in something like PreRender
, because ASP.NET needs to have a chance to bind its value with ViewState
. 但是,请确保在
Load
之后执行此操作,例如PreRender
,因为ASP.NET需要有机会将其值与ViewState
绑定。
如果您想通过Request.Form获取它,请为其命名
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.