Click here to Skip to main content
13,001,454 members (60,242 online)
Click here to Skip to main content
Add your own
alternative version


14 bookmarked
Posted 29 May 2012

How to post back to the server using JavaScript in ASP.NET

, 29 May 2012
Rate this:
Please Sign up or sign in to vote.
This is an alternative for "How to post back to the server using JavaScript in ASP.NET"


The code in the original article works great and causes postback from JavaScript. However, it doesn't let you pass a variable argument back from JavaScript. I made some slight modifications to the code to allow it to pass a single string variable from JavaScript to codebehind.


Basically, as Scott Norberg noted in his original article,, were trying to avoid using __doPostback to cause a postback from javascript in .Net, and instead use documented .Net framework class "PostBackOptions" to cause the postback. This extension to his code allows for passing a variable from JavaScript also.

Using the code

Here are my changes (my code is in VB). First step is to write a small EventArg class:

Public Class JavaScriptPostbackEventArgs
    Inherits EventArgs

    Public args As String = ""

    Public Sub New(args As String)
        Me.args = args
    End Sub
End Class

Next, change the postback event's type to use this new class:

Public Event Postback As EventHandler(Of JavaScriptPostbackEventArgs)

Now the next task is to get the JavaScript to pass a single string variable. This is done by changing the Render method:

The first step is to add an argument to the option:

options.Argument = "arg"

Next, also add it as a variable in the javascript function call:

writer.Write([String].Format("{0} = function(arg) {1}", functionName__1, "{"))

When GetPostBackEventReference renders this, it renders arg is a string value, not a variable. The following will remove the single quotes off of arg and use the variable:

writer.Write(Me.Page.ClientScript.GetPostBackEventReference(options).Replace("'arg'", "arg"))
Here is the full Render method:
Protected Overrides Sub Render(writer As HtmlTextWriter)
    Dim options As New PostBackOptions(PostbackButton)
    options.PerformValidation = Me.CausesValidation
    options.ValidationGroup = Me.ValidationGroup
    options.Argument = "arg"

    Dim functionName__1 As [String] = If(JavaScriptNamespaces.Equals([String].Empty), FunctionName, [String].Concat(JavaScriptNamespaces, ".", FunctionName))
    Dim arg As String = "e"
    writer.Write("<script type='text/javascript'>")
    AddNamespaces(JavaScriptNamespaces, writer)
    writer.Write([String].Format("{0} = function(arg) {1}", functionName__1, "{"))
    writer.Write(Me.Page.ClientScript.GetPostBackEventReference(options).Replace("'arg'", "arg"))
End Sub
Now finally the raise postback event will get the EventArgument from the request:
Private Sub PostbackButton_Click(sender As Object, e As EventArgs)
    RaiseEvent Postback(sender, New JavaScriptPostbackEventArgs(HttpContext.Current.Request("__EVENTARGUMENT")))
End Sub

That's all the changes.

Example of how to use

Here is an example on how to use it from the client side. Add the control the page markup:

<uc:JavascriptPostBack runat="Server" ID="ucPostback" FunctionName="javaPostback" />

A simple javascript example to call the function is: You can use this inside another function.


And finally the code behind that handles the event:

Private Sub ucJavascriptpostback_Postback(sender As Object, e As JavaScriptPostbackEventArgs) Handles ucNodeClickPostback.Postback    

End Sub

In the above example, e.args will have the value 'test'.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Razi Syed
Software Developer (Senior)
United States United States
My area of expertise is developing management database systems for business so that they can efficiently store and lookup existing customer's information and related data, and be able to generate various reports. My goal is to always deliver innovative design and a user friendly interface.

You may also be interested in...


Comments and Discussions

QuestionHow to implement the post back from a pop up page to master Pin
abiFarrah13-Jan-13 13:57
memberabiFarrah13-Jan-13 13:57 
SuggestionOh, 'bout your picture, Razi ... Pin
Gary Noter1-Jun-12 6:49
memberGary Noter1-Jun-12 6:49 
GeneralDitch that method, use jQuery's .ajax calll Pin
Gary Noter31-May-12 12:52
memberGary Noter31-May-12 12:52 
GeneralRe: Ditch that method, use jQuery's .ajax calll Pin
Razi Syed31-May-12 14:05
memberRazi Syed31-May-12 14:05 
GeneralRe: Ditch that method, use jQuery's .ajax calll Pin
HaBiX31-May-12 21:14
memberHaBiX31-May-12 21:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170624.1 | Last Updated 29 May 2012
Article Copyright 2012 by Razi Syed
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid