设置的AutoPostBack闪光之前会话变量在ASP:按钮 [英] Setting a session variable before AutoPostBack fires in an asp:Button
问题描述
我怎么会去从ASP的设置点击会话变量:一个的AutoPostBack事件触发之前按钮
How would I go about setting a session variable from the click of an ASP:Button before an AutoPostBack event fires.
下面是我的权利,但我不完全知道我在做这个权利:
Here is what I have right now, but I'm not exactly sure I'm doing this right:
<asp:Button ID="CommitBTN" runat="server" PostBackUrl="~/MMR_Home.aspx"
onclick="CommitBTN_Click" UseSubmitBehavior="true"
OnClientClick='<% string temp1 = "true"; Session["ClickedFlag"] = temp1; %>' Text="Commit Changes to Database" />
这会是执行这个动作的正确方法还是我在准备这完全错了?
Would this be the correct way of performing this action or am I going at it completely wrong?
编辑:
我的按钮标签更改为此:
Changed my button tag to this:
<asp:Button ID="CommitBTN" runat="server" PostBackUrl="~/MMR_Home.aspx"
onclick="CommitBTN_Click" OnClientClick="document.getElementById('<%= Hidden.ClientID
%>').value='1'" UseSubmitBehavior="true" Text="Commit Changes to Database" />
我收到这是我的错误:
I receive this as my error:
微软JScript运行时错误:无法设置属性价值的价值:对象为null或undefined
Microsoft JScript runtime error: Unable to set value of the property 'value': object is null or undefined
推荐答案
使用这样的:
里面aspx文件:
<form runat="server">
<asp:Button ID="CommitBTN" runat="server" Text="Button" onclick="CommitBTN_Click" OnClientClick="document.getElementById('HiddenField').value='Ram'"/>
<asp:HiddenField ID="HiddenField" runat="server" />
</form>
或
<script type="text/javascript">
function setMyHiddenField(myValue) {
document.getElementById('HiddenField').value = myValue;
}
</script>
<form runat="server">
<asp:Button ID="CommitBTN" runat="server" Text="Button" onclick="CommitBTN_Click" OnClientClick="setMyHiddenField('Ram')"/>
<asp:HiddenField ID="HiddenField" runat="server" />
=============================================== ===================
==================================================================
aspx.cs文件中
protected void CommitBTN_Click(object sender, EventArgs e)
{
Session["ClickedFlag"] = HiddenField.Value;
Response.Write(Session["ClickedFlag"]);
}
这是很容易replase拉姆与你的价值。 ;)
It is easy to replase "Ram" with your value. ;)
您可以更改拉姆TEMP1容易:
you can change Ram to temp1 easy:
setMyHiddenField('temp1目录')
或者你可以调用这个函数在你的其他控件的事件befor CommitBTN pressed
Or you can call this function on your another control events befor CommitBTN pressed
这篇关于设置的AutoPostBack闪光之前会话变量在ASP:按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!