Developing Matt

My Technical Journal

Code before to Code behind (javascript to aspx.vb)

leave a comment »

I’m using a 3rd party control with limited events. The events that they do have are javascript events, which leads me to the following: How do I call a procedure in my aspx code behind page from javascript? The problem is that javascript/vbscript/html is client side, and the code behind is server side, so … in rudimentary terms …. I need to find a way to travel to the back end of my aspx page to call my specific event. (avoiding ambiguous references about the time-space continuum and the matter-anti matter discussion). They are telling me that ajax is a perfect vehicle for this, but I’m still trying to learn hopscotch. So here’s how to do it. In your control, make sure to include a call to your javascript with whatever event you are trying to capture:

<form id=”Form1″ method=”post” runat=”server”>
<input type=”button” value=”Call Back” onclick=”JavaScript:callBack();” />

In your script section, do a postback and pass in a name that you will capture.

<script language=”JavaScript”>
function callBack()

Finally, in your code behind page add a few lines to your Page_load event

ClientScript.GetPostBackEventReference(Me, String.Empty)
If IsPostBack Then
If Request(“__EVENTTARGET”).Trim() = “imcallingback” Then
End If
End If

From link: “The __doPostBack function takes two arguments, eventTarget and eventArgument. The eventTarget contains the ID of the control that causes the postback and the eventArgument contains any additional data associated with the control. Note that the two hidden fields, “__EVENTTARGET” and “__EVENTARGUMENT,” are automatically declared. The value of the eventTarget and eventArgument are stored in the hidden fields. ”

This means that you can send another argument in your __postback call like

__doPostBack(‘imcallingback’, ‘anotherarguemnt’);

And you can actually receive this second argument in your code-behind like



Written by matt

August 29, 2007 at 10:56 am

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: