Return Value from Javascript to Server Side in ASP.NET

No.of Views4273
Bookmarked1 times
Downloads 
Votes1
By  kirtan007   On  04 Jul 2010 22:07:04
Tag : ASP.NET , Miscellaneous
This article will teach you how to retrieve JavaScript functions Return Value to Code Behind
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

Some time it's necessary to send a JavaScript function's return value to server-side. For example, we are able to show a Confirm Dialog using Page.RegisterClientScriptBlock() but after we are cannot determine which button a user have pressed on the JavaScript Confirm Dialog. This article will teach you about how to get values in JavaScript from a client-side to server-side such as find out what button a user has clicked on a ConfirmDialog.

Prerequisites  

Basic Knowledge of JavaScript,C#.

Technology

asp.net c# 2.0/3.5

Implementation

Suppose we want to show a confirm dialog when user selects some item in a Dropdownlist and we want to know which Button user have pressed on the Confirm Dialog. Means we want to know if he has pressed "Ok" button or "Cancel" button in our code on server-side.

To get JavaScript values in the code-behind, we need Hidden Input Control, whose runat attribute we will set to  runat="Server" so that it can be accessible in code-behind. It will work as an intermediately to pass the value from client-side to server-side.

Below, I have highlighted two parts - One is Script in <head> part of the code it contain Function ConfirmIt() .

That will be executed when a user select an item in the DropDownList. In the DropDownList, we defined onChange="ConfirmIt()" that will execute the JavaScript and will set value of Hidden Input Control to 1 if user selects the OK button and will set value to 0 when user selects Cancel button on the Confirm Dialog().

 Screen for Demostration

Image Loading

Figure-01

Image Loading

Figure-02

HTML

<html><head runat="server"><title></title><script type="text/javascript">function ConfirmIt() {var x = confirm("Do you Want to notify changes to User ??");var control = '<%=inpHide.ClientID%>';if (x == true) {document.getElementById(control).value = "1";}else {document.getElementById(control).value = "0";}}</script></head><body><form id="form1" runat="server"><div><input id="inpHide" type="hidden" runat="server" /><asp:DropDownList ID="DropDownList1" runat="server"            onselectedindexchanged="DropDownList1_SelectedIndexChanged" onChange="ConfirmIt()"            AutoPostBack="True"><asp:ListItem>Item1</asp:ListItem><asp:ListItem>Item2</asp:ListItem><asp:ListItem>Item3</asp:ListItem></asp:DropDownList><asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></div></form></body></html>

Now let's look at the code where we will retrieve the ConfirmDialog's return value at server-side.

Here is my code for the SelectedIndexChanged event of the DropDownList control. As you can see from this code, I simply use the Parse method and get the value of inpHide. If the value of this is 1, I know the OK button was pressed and if the value is 0, I know the Cancel button was pressed.

Code Behind

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {int DiagResult = int.Parse(inpHide.Value);if (DiagResult == 1)
        {//Do SomthingResponse.Write("You Have Selected Ok");
        }else if (DiagResult == 0)
        {//Do somthingResponse.Write("You have Selected Cancel");
        }
 
    }

That's pretty simple and self explanatory code.i hope you like my article.

Sample Project Source

Download source files -2 kb

 
Sign Up to vote for this article
 
About Author
 
kirtan007
Occupation-
Company-
Member Type-Senior
Location-Not Provided
Joined date-02 Jul 2009
Home Page-http://kirtan.uni.cc
Blog Page-
He completed his Bachelor of Computer Application from Gujarat University 2009 .He is doing Master of Computer Application from Gujarat Technological University right now .. His area of Interests are Web Hacking , C# .net Windows form ,asp.net , WPF ,Silverlight ,SQL Server and Some PHP.
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top