0

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?

EDIT:

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:

Microsoft JScript runtime error: Unable to set value of the property 'value': object is null or undefined

se_brandon
  • 228
  • 10
  • 25

2 Answers2

1
  1. Use a Hidden Field control.

  2. Update the Hidden Field to 1 on Button Client Click.

  3. Update the Session Value in the Page Load' event. The Value will be 1 then update the Session variable and set theHidden Fieldvalue to 0 underneath theSession Variable` Update.

  4. Reason for the Usage of Page Load event is that on clicking the Button as per the page life cycle the page events like PreInit, Init, InitComplete, PreLoad, Load executes before the execution of Button Control.

Page events execution takes place like below..

  1. Preinit
  2. Init
  3. InitComplete
  4. PreLoad
  5. Load
  6. Control Event
  7. Load Complete
  8. Pre Render

Hope this will help you...

1

Use this:

Inside aspx file:

<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>

Or

<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" />

==================================================================

Inside aspx.cs file

protected void CommitBTN_Click(object sender, EventArgs e)
    {
        Session["ClickedFlag"] = HiddenField.Value;
        Response.Write(Session["ClickedFlag"]);
    }

It is easy to replase "Ram" with your value. ;)

you can change Ram to temp1 easy:

setMyHiddenField('temp1')

Or you can call this function on your another control events befor CommitBTN pressed

Ramin Bateni
  • 16,499
  • 9
  • 69
  • 98
  • Both of these answers I will try. I would have never though to use a hidden field. I will let you guys know how it goes. – se_brandon Jul 25 '12 at 19:04
  • Microsoft JScript runtime error: Unable to set value of the property 'value': object is null or undefined – se_brandon Jul 25 '12 at 19:22
  • I guess I really don't need to set a session variable, I just need to know that the asp:button was clicked. The previous answers to this question are close. All I want to be able to do in the Page_Load is say "if (button.clicked == true) then set a flag. – se_brandon Jul 25 '12 at 19:42
  • I added a new js function to my codes.use it any where of your page that you want (in this case remove OnClientClick event). – Ramin Bateni Jul 25 '12 at 19:53