1

I am developing an Asp .net web form with a hidden field that should have some value, the aim of this hidden field is to pass values from client script to the server so that the client script places the value in this field and trigger a postback:

<asp:HiddenField ID="MyHiddenField" ClientIDMode="Static" runat="server" />
<asp:CheckBox ID="dummy" ClientIDMode="Static" AutoPostBack="true" runat="server" Style="display: none" />

The aim of the checkbox is to help triggering post back:

if ($('#MyHiddenField').val() == '') {
            $('#MyHiddenField').val(SomeValue);
            //do post back:
            $('#dummy').click();
}

The server stores the values brought from the hidden field in the Session and pass it to the hidden field if it exists:

if (MyHiddenField.Value != "")
    {
        if (Session["c"] == null)
            Session.Add("c", MyHiddenField.Value);
    }
    else if (Session["c"] != null)
        MyHiddenField.Value = Session["c"].ToString();

Now I am getting this server error at random requests after the postback

Invalid postback or callback argument

The stack trace show that the hiddenfield is the problem:

[ArgumentException: Invalid postback or callback argument. Event validation is enabled using in configuration or <%@ Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.] System.Web.UI.ClientScriptManager.ValidateEvent(String uniqueId, String argument) +144 System.Web.UI.Control.ValidateEvent(String uniqueID, String eventArgument) +108 System.Web.UI.WebControls.HiddenField.LoadPostData(String postDataKey, NameValueCollection postCollection) +36 System.Web.UI.WebControls.HiddenField.System.Web.UI.IPostBackDataHandler.LoadPostData(String postDataKey, NameValueCollection postCollection) +13 System.Web.UI.Page.ProcessPostData(NameValueCollection postData, Boolean fBeforeLoad) +303 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1960

How can a hiddenfield causes this bad callback arguments? and Why am I getting this exception?

2
  • Which control trigger the post back? It is not a good practice other control to trigger CheckBox and cause the post back.
    – Win
    Sep 2, 2015 at 19:19
  • It is triggered automatically and directly after the page loads
    – ammcom
    Sep 2, 2015 at 19:33

3 Answers 3

0

Instead of calling some dummy control. you can call doPostBack function

if ($('#MyHiddenField').val() == '') {
        $('#MyHiddenField').val(SomeValue);
        //do post back:
        __doPostBack();

}

Add below code in server side page load method

ClientScript.GetPostBackEventReference(this, string.Empty);
3
  • I tried it but it does not work, the same exception arise as it checks for _dopostback arguments, the first argument should be a valid control otherwise an exception is thrown and the second argument should be a valid event, eventually, I have to add a server control so why not just use it to post back and don't take a risk of callling javascript directly
    – ammcom
    Sep 2, 2015 at 19:19
  • make sure you are using "__doPostBack()" Two underscores
    – Viru
    Sep 2, 2015 at 19:23
  • I will try, anyway it is not my problem as it is not how to do a postback, whatever the postback method is, I get the exception explained
    – ammcom
    Sep 2, 2015 at 19:26
0

So I would have posted this as a comment but apparently I dont have enough reputation to do so. So I guess I'll just put it here. First I would try to clean up some of your code by changing your if logic. The error you are getting is saying that what is on/in the web browser is different then what the server has and it then says that this is invalid and passes you the runtime error. Hopefully by changing the if logic it might resolve the issue.

The reason being is that if your hidden value is not null its possible the session still is, so nothing will happen but if you change it like below nothing will happen when your hiddenfield is null, otherwise the session will be updated.

if (MyHiddenField.Value != "")
 {
    if (Session["c"] == null)
       Session.Add("c", MyHiddenField.Value);
 }
 else if (Session["c"] != null)
   MyHiddenField.Value = Session["c"].ToString();

if(Session["c"] == null)
   Session.Add("c",MyHiddenField.Value);
else if(MyHiddenField.Value != "")
 {
   MyHiddenField.Value = Session["c"].ToString();
 }
2
  • The change you suggested makes the logic different from what I intend
    – ammcom
    Sep 2, 2015 at 19:20
  • As I know, it is posible that the webpage contains things different from server, the values may change at client side and the server gets the changes in the next postback, I can't get your idea
    – ammcom
    Sep 2, 2015 at 19:24
0

Actually I figured out the problem and it may be useful for someone else, the problem is that I was triggering postback before the page is totally loaded, waiting for the page to load fixes the issue

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.