Vivek P
Vivek P

Reputation: 3050

How to get a post back parameter value in code-behind

I am triggering a postback event in aspx page as below:

__doPostBack('AddNewEmployeeType', "empl", "sick");

Code behind:

string val = Request.Params.Get("__EVENTTARGET");

By the above code i was able to get only one first value, but my intention is to get all three parameter values. How can I achieve this?

Upvotes: 2

Views: 13378

Answers (3)

Igor Kroeber
Igor Kroeber

Reputation: 17

Alternatives

Postbacks are not supposed to be handled like that. If a control is raising postbacks, and if it's an User Control, it should implement IPostBackDataHandler. and you should assign an EventHandler to it, even if it's a custom one.


References

https://msdn.microsoft.com/pt-br/library/system.web.ui.ipostbackdatahandler(v=vs.110).aspx

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460108

Use the __EVENTARGUMENT:

string parameter = Request["__EVENTARGUMENT"]; 
string val = Request.Params.Get("__EVENTTARGET"); // AddNewEmployeeType

Here's a tutorial: Understanding the JavaScript __doPostBack Function

If you need to pass multiple parameters back to codebehind you need to split it by a delimiter yourself. You could for example use the pipe |:

__doPostBack('AddNewEmployeeType', "empl|sick");

and in codebehind:

string parameter = Request["__EVENTARGUMENT"]; 
string[] allParams = parameter.Split('|');

Upvotes: 6

matk
matk

Reputation: 1518

Your call to __doPostBack is technically incorrect. If you check the source of an ASP.NET page, you'll see the function:

function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}

Note it has only 2 parameters. If you need to pass more values, you need to stuff them into the eventArgument parameter.

How you do this is up to you - use a comma-separated list, or JSON, or whatever suits you, and parse this value (by accessing Request["__EVENTARGUMENT"]) on the server.

Upvotes: 0

Related Questions