JavaScript: Multiple parameters in __doPostBack

南楼画角 提交于 2019-11-30 01:34:43

问题


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


回答1:


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.




回答2:


Consider placing your data in server side hidden fields and then reading that data after your postback.

<asp:HiddenField id="Data1HiddenField" runat="server" />
<asp:HiddenField id="Data2HiddenField" runat="server" />

Your client script should include the ClientID values to handle server side naming container modifications. Using the <%= expression %> syntax (Expression Builder) requires that your script (or at least this part of the script) be maintain within your .aspx file. If you maintain your JavaScript in external files, you can "register" a simple function that gets called by your main JavaScript to move the data and compose that function server side along with the required ClientIDs. See ClientScriptManager.RegisterClientScriptBlock().

var data1 = "data value 1";
var data2 = "data value 2";
$("#<%= Data1HiddenField.ClientID %>").val(data1);
$("#<%= Data2HiddenField.ClientID %>").val(data2);

Your server side code then looks like this:

string data1 = Data1HiddenField.Value;
string data2 = Data2HiddenField.Value;

There are certainly other techniques to passing multiple data values but I have found this to be both simple and easy to maintain. You can pass all kinds of data and encode it using JSON if needed.




回答3:


I have used multiple parameters before by building and splitting a string.

eg

string args = String.Format("{0};{1}", bolID, ControlID);

You can then pass this in to the arguments for the postback, and when checking for the postback arguments just split the string based on your speration character (in this case ';')



来源:https://stackoverflow.com/questions/14482939/javascript-multiple-parameters-in-dopostback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!