获取c#.net中的隐藏字段值 [英] Getting the hidden field value in c#.net
问题描述
你好编码员,
这里我给出了一个关于我的要求的小例子
我是使用一个java脚本函数,比如
我的aspx页面代码:
Hi coders,
Here i am giving the small example about my requirement
I am using one java script function like
My aspx page Code:
<script type="text/Jscript">
function getvalue() {
document.getElementById("hdnResultValue").value = 123;
}
</script>
<asp:HiddenField ID="hdnResultValue" Value="0" runat="server" />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</form>
我的aspx页面cs代码:
My aspx page cs Code:
protected void Button1_Click(object sender, EventArgs e)
{
Page.ClientScript.RegisterStartupScript(this.GetType(),null, "getvalue();", true);
string codeBehindValue = hdnResultValue.Value;
}
这时当我第一次点击我的按钮时,我没有得到hdnResultValue值(这里我得到0) ,如果我第二次点击,我得到了hdnResultValue值(这里我得到123)
这可能是由于回发而发生的。
我的问题是如何在第一次按下我的按钮时获取hdnResultValue值。
Here when I hit my button first time I am not getting the hdnResultValue value(Here I am getting 0), If I hit second time I am getting the hdnResultValue value(here I am getting 123)
This might be happening due to post back.
my problem is how can I can get the hdnResultValue value when hit my button first time.
推荐答案
Asp:按钮的onclick是绑定到服务器端处理程序。
如果你想运行一些js,你需要一个客户端处理程序。这样的事情...
Asp:Button's onclick is binding to server side handler.
If you want to run some js you need a client side handler. Something like this...
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="getvalue();" />
尝试使用Request.Form获取隐藏字段的当前值。
Do try the Request.Form to get the hidden field's current value.
Request.Form["Hiddenfieldname"].ToString();
例如,如果你有隐藏的字段,比如
For Example if you have a hidden field like
<asp:HiddenField name="hdnResultValue" Value="0" runat="server" />
比你可以得到价值如
than you can get the value like
Request.Form["hdnResultValue"].ToString();
希望它会有所帮助。
Hope it will help.
请参阅
您正在使用客户端和服务器端方法
为此您必须调用客户端在服务器事件中的客户端事件和服务器端
为此我在下面显示了修改后的代码
See
You are using both client and server side method
For this You have to call client side in client event and server side in server event
For this I have showing modifited code in following below
<div>
<asp:HiddenField ID="hdnResultValue" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" OnClientClick="getvalue();"/>
</div>
服务器按钮点击事件应该用于测试目的是
Server button click event should for test purpose is that
protected void Button1_Click(object sender, EventArgs e)
{
// Page.ClientScript.RegisterStartupScript(this.GetType(), null, "getvalue();", true);
string codeBehindValue = hdnResultValue.Value;
Response.Write(codeBehindValue);
}
这篇关于获取c#.net中的隐藏字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!