Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript: Multiple parameters in __doPostBack

First of all, the only post (calling-multiple-dopostback-from-javascript) I found about this didn't help my problem, so I don't belive this post is a duplicate.

I have this JavaScript function in my ASPX webpage that includes a __doPostBack function:

function OpenSubTable(bolID, controlID) {
  // code
  __doPostBack('UpdatePanelSearch', bolID);
  // more code
}

Works perfectly and I can get the value of bolID into my code behind like this:

protected void UpdatePanelSearch_Load(object sender, EventArgs e)
{
  var bolID = Request["__EVENTARGUMENT"];
  // code
}

The problem is, that I have to pass 2 different values through the postback. Are there any simple solutions to this? Obviously something like this doesn't work:

function OpenSubTable(bolID, controlID) {
  // code
  __doPostBack('UpdatePanelSearch', bolID, controlID); // not that simple, i'm afraid :(
  // more code
}

Any help would be most welcome.

Regards, Gunnar

like image 591
Gunnar Avatar asked Jan 23 '13 15:01

Gunnar


People also ask

What is the use of __ Dopostback in JavaScript?

Doing or Raising Postback using __doPostBack() function from Javascript in Asp.Net. Postback is a mechanism where the page contents are posted to the server due to an occurrence of an event in a page control. For example, a server button click or a Selected Index changed event when AutoPostBack value is set to true.

What is Dopostback?

Understanding the JavaScript __doPostBack Function. This method is used to submit (post back) a form to the server and allows ASP.NET framework to call appropriate event handlers attached to the control that raised the post back.

What is __ Eventargument?

The __EVENTARGUMENT is any relevant event arguments regarding the control performing the postback. For most controls, there are no specialized event arguments, and since event arguments are different for every control, null is passed to represent a default argument should be created during the event sequence.


1 Answers

You could pass the two values as one JSON string:

function OpenSubTable(bolID, controlID) {
  __doPostBack('UpdatePanelSearch', JSON.stringify({ bolID: bolID, controlID: controlID}));
}

And then parse it on the server:

protected void UpdatePanelSearch_Load(object sender, EventArgs e)
{
  SomeDTO deserializedArgs = 
      JsonConvert.DeserializeObject<SomeDTO>(Request["__EVENTARGUMENT"]);
  var bolID = deserializedArgs.bolID;
  var controlID = deserializedArgs.controlID;
}

public class SomeDTO
{
    public string bolID { get; set; }
    public string controlID { get; set; }
}

If you're using .Net >=4.0, I believe you can deserialize to a generic touple and avoid having to create SomeDTO. Edit: More information about deserializing to dynamic types.

like image 161
jbabey Avatar answered Oct 20 '22 03:10

jbabey