DWolf
DWolf

Reputation: 725

.NET hidden field value not found after JQuery sets it

I am having a bit of a hard time trying to get a confirm box working with asp. My issue right now, is that in my confirm javascript method I am updating a hidden field's value to either 1 or 0, and in the ASP function, I am checking whether or not the value is 1 or 0. Currently the hiddenfield value is "".

Front End Code:

<asp:HiddenField ID="txtconfirmmessageValue" runat="server" />
/* redacted code*/
<asp:LinkButton ID="EndSessionLinkButton" CssClass="SessionDashboardButton" runat="server" OnClientClick="return confirmAll();" OnClick="EndSession" Text="CANCEL" ></asp:LinkButton>

Javascript code:

 function confirmAll() {
        if (confirm("You are about to end session. Are you sure you want to do this.")) {
            $('#<%=txtconfirmmessageValue.ClientID %>').val(1);
            return true;
        }
        else {
            $('#<%=txtconfirmmessageValue.ClientID %>').val(0);
            return false;
        }
    }

Behind Code ASP.NET

protected void EndSession(object sender, EventArgs e)
{
    string value = txtconfirmmessageValue.Value;
    while (string.IsNullOrEmpty(value))
    {
        value = txtconfirmmessageValue.Value;
    }

    if (!Utility.ToBool(txtconfirmmessageValue.Value))
    {
        return;
    }
    /* redacted */
}

The value of txtconfirmmessageValue is always "" and is never set, even tho I added alerts to the javascript method that alerted the value of the asp:hiddenfield after the event triggered.

NOTE:: I also added the while loop in there to by pass the post back of asp happening before the confirm box event was finished.. I am not sure how to postpone the postback until the confirm box is completed. Any help would be appreciated.

Upvotes: 0

Views: 414

Answers (5)

YUu
YUu

Reputation: 79

maybe you can try my method fellowing

Implements IPostBackEventHandler

  ScriptManager.RegisterStartupScript(this, this.GetType(), "confirm",
  "if(confirm('check my tickets!'))" + this.ClientScript.GetPostBackEventReference(this,  "true") + "; else " +
  this.ClientScript.GetPostBackEventReference(this, "false"), true);

  public void RaisePostBackEvent(string eventArgument)
{
string res;
switch (eventArgument)
{
case "true":
res = "check!";  
break;
case "false":
res = "uncheck。";   
break;
default:
throw new NotSupportedException();
}
ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('" + res + "');", true);
}

Upvotes: 0

Arun Raj
Arun Raj

Reputation: 969

Use document.getElementById instead of Jquery selector. it will work.

document.getElementById("<%=txtconfirmmessageValue.ClientID %>").value=1;

Upvotes: -1

moarboilerplate
moarboilerplate

Reputation: 1643

Since other page events fire when you click on a button, like Page_Load, make sure those events aren't firing and overwriting the value for your control.

The page is being posted back before jQuery is setting the value, so you need to prevent the postback from occurring until the user responds to the confirm.

In order to maintain the same behavior on your page, making sure the server-side method is still getting called, you'll want to do a postback on behalf of the button by using GetPostBackEventReference.

Upvotes: 1

Enrique Zavaleta
Enrique Zavaleta

Reputation: 2098

If I put the parameter as a string instead of a int it works

 function confirmAll() {
        if (confirm("You are about to end session. Are you sure you want to do this.")) {
            $('#<%=txtconfirmmessageValue.ClientID %>').val("1");
        }
        else {
            $('#<%=txtconfirmmessageValue.ClientID %>').val("0");
        }
    }

Upvotes: 0

Vijay
Vijay

Reputation: 3023

Since you are setting the hidden field value from javascript, you can get it through Request.Form[] in codebehind.

Request.Form[txtconfirmmessageValue.UniqueId]

codebehind:

protected void EndSession(object sender, EventArgs e)
{
    string value = Request.Form[txtconfirmmessageValue.UniqueId];

    if (!Utility.ToBool(value))
    {
        return;
    }
}

Upvotes: 1

Related Questions