回发后的动态隐藏字段
假设您将数据保存到动态隐藏字段中,该隐藏字段是在处理某些回发事件期间动态创建的。
回发时从该字段检索它的最佳方法是什么(除了在请求中搜索该隐藏字段的键然后检索相应的值,如下面的代码所示)?
protected void Button2_Click(object sender, EventArgs e)
{
bool found = false;
for (int i=0; i<this.Request.Form.Keys.Count; i++)
{
string item = this.Request.Form.Keys[i];
if ( item=="Hidden1")
{
Literal6.Text = Request.Form.GetValues(i)[0];
found = true;
}
}
if (found==false)
{
Literal6.Text = "Hidden1 is not found";
}
}
suppose you save data into a dynamic hidden field ,which is created dynamically during the handling of some postback event.
what is the best way to retrieve it from this field upon a postback, (besides searching the request for the key of this hidden field and then retrieving the corresponding value as in the code below)?
protected void Button2_Click(object sender, EventArgs e)
{
bool found = false;
for (int i=0; i<this.Request.Form.Keys.Count; i++)
{
string item = this.Request.Form.Keys[i];
if ( item=="Hidden1")
{
Literal6.Text = Request.Form.GetValues(i)[0];
found = true;
}
}
if (found==false)
{
Literal6.Text = "Hidden1 is not found";
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你可以这样做:
但你也可以使用 findControl 方法。也就是说,如果一个元素有一个注册的 id...忘了说,因为 findcontrol 获取控件 ID,而 GetValues 通过名称确定控件。 (这在你的例子中是不可能的;)
you could do like this:
but you can also use the findControl method. That is, if an element has an registered id... forgot to say that, since findcontrol takes the control ID, and GetValues determines the control by name. (which is not likely in your example ;)