简体   繁体   English

ASP.NET隐藏字段数据仅在回发中可用? 为什么?

[英]ASP.NET Hidden fields data only available in a postback? why?

According to MSDN hidden fields section , 根据MSDN隐藏字段部分

In order for hidden-field values to be available during page processing, you must submit the page using an HTTP POST command. 为了使隐藏字段值在页面处理期间可用,您必须使用HTTP POST命令提交页面。 If you use hidden fields and a page is processed in response to a link or an HTTP GET command, the hidden fields will not be available. 如果您使用隐藏字段,并且响应链接或HTTP GET命令处理了页面,则隐藏字段将不可用。

If I add a HiddenField control at design time and set a value in it at design time or in the Init event in ASP.NET, why would I not be able to read/process the value when a page is first requested? 如果在设计时添加HiddenField控件并在设计时或在ASP.NET中的Init事件中为其设置值,为什么在第一次请求页面时无法读取/处理该值?

How have you defined your hidden field? 您如何定义隐藏字段?

You need to make hidden field as runat="server" like this: 您需要像这样将隐藏字段设置为runat =“ server”:

<input id="something" type="hidden" value="something that is hidden" runat="server" />

Then you will be able to access this field on server even if it is Get request. 这样,即使它是“获取请求”,您也可以访问服务器上的该字段。

protected void Page_Load(object sender, EventArgs e)
{
     Response.Write(string.Format("{0} came from hidden field", something.Value));
}

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

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