Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
910 views
in Technique[技术] by (71.8m points)

asp.net - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...