Sending data from javascript to a .Net object

Recently I came across an issue where I had checkboxes outputted via XSLT (which only outputs HTML) but needed to have the values of the selected checkboxes sent to a C# object.  After banging my head against the wall and trying various schemes I remembered that JavaScript can be used to perform a postback.  Now if there was only some way to pass data when performing the postback.  Turns out there is.

In JavaScript you can use the call  “__doPostBack(variablename, value);” to perform the postback and send the variable along with it.  For instance,



performs a postback, sending the variable “ShareBlog” with the value “Rocks”.

In your code (C# in my case) you can read in this variable and use it as you will



if (Page.IsPostBack)


   string eventTarget = Page.Request[“__EVENTTARGET”];  

   string eventArgument = Page.Request[“__EVENTARGUMENT”];  

   if (eventTarget != String.Empty && eventTarget == “ShareBlog”)  

   {     //do what you will with the eventArgument   }



Note that you would replace “Shareblog” in the second if statement with the name of your variable you used in the “__doPostBack” statement.

Leave a Reply