Quantcast

Postbacks with Parameters in JavaScript

Get the WebProNews Newsletter:
[ Business]

Whenever you use a Button or LinkButton it is because you want to be able to do a postback when it is clicked.

The same could be the case for CheckBox or DropDownList etc. but then you need to set the AutoPostback property to true.

It all works very much the same way from a user’s point of view – click or select and the page performs a postback.

However, in some cases you want to be able to do a postback from a custom JavaScript function that emulates the click of an e.g. LinkButton.

That is very simple to do so, but did you know that you also can send custom information via such a postback?

Example

The following LinkButton calls the server-side event handler OnSaveClick.

<asp:LinkButton runat="Server" ID="btnSave" Text="Save" OnClick="OnSaveClick" />

This is pretty much standard and no tricks have been used so far. Now we need the JavaScript method that forces the LinkButton to do a postback that calls the server-side method OnSaveClick.

<script type="text/javascript">

function SaveWithParameter(parameter)

{

  __doPostBack('btnSave ', parameter)

}

</script>

Notice that the function takes a parameter that it sends to the __doPostBack function. All we need to do now is to call the SaveWithParamter function from JavaScript.

SaveWithParameter("Hello world!");

Now the page performs a postback and we can now access the “Hello world!” string that we sent as a parameter from within the OnSaveClick event handler.

protected void OnSaveClick(object sender, EventArgs e)

{

  string parameter = Request["__EVENTARGUMENT"];

}

What we just did was to perform a postback from a custom JavaScript function and send a parameter to the server-side event handler. It sounds a lot harder than it is, right?

In ASP.NET 2.0 you have to set the EnableEventValidation=”false” attribute in the page declaration or in web.config to make it work.

Comments

Tag:

Reddit | Furl

Bookmark WebProNews:

Mads Kristensen currently works as a Senior Developer at Traceworks located
in Copenhagen, Denmark. Mads graduated from Copenhagen Technical Academy with a multimedia degree in
2003, but has been a professional developer since 2000. His main focus is on ASP.NET but is responsible for Winforms, Windows- and
web services in his daily work as well. A true .NET developer with great passion for the simple solution.

http://www.madskristensen.dk/

Postbacks with Parameters in JavaScript
About Mads Kristensen
Mads Kristensen currently works as a Senior Developer at Traceworks located in Copenhagen, Denmark. Mads graduated from Copenhagen Technical Academy with a multimedia degree in 2003, but has been a professional developer since 2000. His main focus is on ASP.NET but is responsible for Winforms, Windows- and web services in his daily work as well. A true .NET developer with great passion for the simple solution.

http://www.madskristensen.dk/ WebProNews Writer
Top Rated White Papers and Resources
  • Guest

    Thanks, that’s exactly what I was searching for. It’s very clear when implementing an IPostBackHandler within your custom control class or even the page class – this parameter is passed to the handler, but it’s not obvious in the load event of a control.

  • Gaurang Daslania

    Hi..Thanks for the help..now my question is does this support for multiple parameters?

  • Oliver

    EnableEventValidation=”false” introduces a security risk…

  • Ba Nyar Thein

    Ohhh… That is really perfect post. I have been looking for this kind of things. Thanks a lot man..

  • Join for Access to Our Exclusive Web Tools
  • Sidebar Top
  • Sidebar Middle
  • Sign Up For The Free Newsletter
  • Sidebar Bottom