JavaScript: Multiple parameters in __doPostBack

拟墨画扇 提交于 2019-11-30 18:25:16
jbabey

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.

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.

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 ';')

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