Search code examples
asp.netwebformspostbackdopostback

Webforms postback with multiple arguments


I wish to call the __doPostBack(eventTarget, eventArgument) function. In the code file I would then read the arguments as usual like this: Dim param As String = Request("__EVENTARGUMENT"). However this doesn't work for multiple arguments.

Someone here suggested that you could use a json or csv-list. Usually I would just choose a delimiter that sepeates my arguments. However in my case now the argument may contain any character, so I can't just decide on any delimiter, so I figured I'd use a JSON.

I tried this.

Markup:

<button type="button" id="btn_postbackButton" onclick="__doPostBack('btn_postbackButton', {one: 'someText', two: 'moreText'});">TestButton</button>

Code-File

If (IsPostBack) Then
    Dim tgt As Object = Request("__EVENTTARGET")
    Dim param As Object = Request("__EVENTARGUMENT") ' what datatype to use?
    ' [...]
    ' how to get arguments from json?
End If

Now how do I get my arguments back in the code? What datatype do I use for the parameters? Calling param.ToString() only returns [object Object]

What's more, the arguments are generated in the code file in my actual application, and there may be different ones and some may be missing, using a simple array doesn't work for me as you can't determine after the fact which array position coresponds to which argument. And regardless it still gives me [object Object].


Solution

  • When your button is clicked, the page sends the following HTML form fields:

    __EVENTTARGET: btn_postbackButton
    __EVENTARGUMENT: [object Object]
    

    To get your json on the server side as a string, use the following button markup:

    <button type="button" id="btn_postbackButton" onclick="__doPostBack('btn_postbackButton', '{one: \'someText\', two: \'moreText\'}');">TestButton</button>