问题描述
首先,我要对我的问题的标题不明确道歉。我不知道如何简洁地描述在标题我的问题。
First I'll apologize for the unclear title of my question. I wasn't sure how to succinctly describe my problem in a title.
我有一个隐藏字段在我的.aspx
I have a hidden field in my .aspx
<input type="hidden" name="hid1" value="0" />
我想要的页面加载事件中设置此字段的值,如果它不是一个回发。
I want to set the value of this field during the page load event, and if it is not a postback.
protected void Page_Load(object sender, EventArgs e) {
if (!Page.IsPostBack) {
// This doesn't work!
Request.Form["hid1"] = "1";
}
if (Page.IsPostBack) {
// This DOES work!
Request.Form["hid1"] = "1";
}
}
问题是,该请求不包含页面加载事件中表单数组中的隐藏字段时,它不是一个回发(即 - 第一次的页面被击中)。随后点击页面(也就是 - 回传)。结果包含隐藏字段的表单阵列中的
The problem is that the Request doesn't contain the hidden field in the Form array during the page load event when it's not a postback (ie - the first time the page is hit). Subsequent hits to the page (ie - postbacks) result in the Form array containing the hidden field.
我敢肯定,它与页面的生命周期中的事,但我真正需要知道的是如何我的页面加载事件过程中设置的隐藏字段,当它不是一个回发?
I'm sure that it has to do with the lifecycle of the page, but what I really need to know is how do I set the hidden field during the page load event and when it is not a postback?
编辑:
我真的,真的不希望纳入=服务器属性!
I really, really don't want to incorporate the runat="server" attribute!
推荐答案
您可以在您的网页类中定义一个属性,然后在code修改属性值:
You could define a property in your page class and then modify the property value in your code:
protected string HiddenFieldValue { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
HiddenFieldValue = "postback";
else
HiddenFieldValue = "not postback";
}
然后,这样它的值设置为属性值这样定义隐藏的表单字段:
Then define the hidden form field like this so that it's value is set to the property value:
<input type='hidden' id='hidden1' value='<%=HiddenFieldValue %>' />
如果你只是想设置的值形成一个回发或不回发期间的财产,你可以添加条件,以及:
If you only want to set the value form the property during a postback or non-postback you could add the condition as well:
<input type='hidden' id='hidden1' value='<% if(IsPostBack) { %> <%=HiddenFieldValue%> <% } %>' />
这篇关于隐藏字段是空!的IsPostBack和的IsPostBack不为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!