最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

asp.net - JavaScript: Multiple parameters in __doPostBack - Stack Overflow

programmeradmin1浏览0评论

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

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

Share Improve this question edited May 23, 2017 at 11:59 CommunityBot 11 silver badge asked Jan 23, 2013 at 15:14 GunnarGunnar 2,7254 gold badges22 silver badges23 bronze badges 3
  • @jbabey probably has the easiest answer, but it's also not hard to roll your own "doPostBack" function. All it needs to do is set the value of a couple hidden input fields and then submit the form. Very easy, even for a beginner. – JDB Commented Jan 23, 2013 at 15:29
  • @Cyborgx37 yes, hidden inputs is the option i would pursue if (for some weird reason) JSON is out of the question. In general, you don't want to muddie up your HTML with hidden inputs unless you absolutely have to. – jbabey Commented Jan 23, 2013 at 15:32
  • @jbabey You can use jQuery to create the fields in the DOM as needed. Keeps your HTML clean. Alternatively you can put values in the URL, but I generally frown on that approach for a POST request (which is what WebForms generally uses). Your approach is still probably the one I'd recommend. – JDB Commented Jan 23, 2013 at 16:46
Add a comment  | 

3 Answers 3

Reset to default 15

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

发布评论

评论列表(0)

  1. 暂无评论