5,699,997 members and growing! (21,677 online)
Email Password   helpLost your password?
Web Development » ASP.NET » Howto License: The Code Project Open License (CPOL)

Calling ASP.NET Server Side Events using JavaScript

By saanj

Calling ASP.NET Server Side Events using JavaScript
C# (C# 1.0, C# 2.0, C# 3.0, C#), Javascript, ASP.NET

Posted: 12 Apr 2008
Updated: 12 Apr 2008
Views: 12,683
Bookmarked: 9 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
11 votes for this Article.
Popularity: 3.12 Rating: 3.00 out of 5
3 votes, 27.3%
1
2 votes, 18.2%
2
1 vote, 9.1%
3
0 votes, 0.0%
4
5 votes, 45.5%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

Many times we want to execute ASP.NET Server Side Events using JavaScript. What about calling a TextChanged Event on the onchange javascript event of a textbox control. It can be done quite easily without giving pain to our brain too much.

Background

In one of our ASP.NET Web Form which is a part of our exisiting application, I was trying to implement and open an AJAX Modal Popup Extender Dialogue box on the click of an image button control. But, there is a TextBox in the form in which a JavaScript attribute was bound for the OnPropertyChange Event. The TextBox was actually a date picker control. If user selects a date then certain kind of bussiness logics get fired. But, just because of that ModalPopupControl never worked with this form. Because, whenever the Image Button was clicked to open the Modal Dialog Box, the OnPropertyChange Event was fired and the form.submit method was called which did not let the Modal Dialog Box to open. So, the situation was like that I had to avoid the form.submit method but keeping in mind that the Bussiness Logic remained the same.

Using the code

To overcome from this situation, I used the following code:

(i) I used the GetPostbackEventReference method of the Page object to register the ASP.NET Server Control which can create PostBack using Client Side callback.

      Page.GetPostBackEventReference(txt_sssn_dt);        

(ii) Then, I used the __dPostBack Event to fire the TextChanged Event of the datepicker control (txt_sssn_dt). This code had been placed inside the Date Picker JavaScript function and it would be fired everytime the user selects the date from the date picker.

      __doPostBack("txt_sssn_dt", "TextChanged");     

(iii) It was also necessary to write a simple JavaScript function to handle the situation when user just wanted to type the date directly into the date picker (txt_sssn_dt) . I wrote a JavaScript function called DoPostBack().

     function DoPostBack()
     {        
           __doPostBack("txt_sssn_dt", "TextChanged");
     }  

(iv) I called this function on the OnChange event of the date picker control. I got the solution.

     if(!IsPostback)
     {
      /...Implementation.../ 
         /.................../
      txt_sssn_dt.Attributes.Add("OnChange", "javascript:return DoPostBack()")
     }

It's a very simple demonstration to show how to call a Server Side ASP.NET Events from Client Side. Please share your ideas if there is any other or better way to do this. I am available at sanjaysantra@hotmail.com.

Points of Interest

.NET Framework, C#, ASP.NET, SQL Programming, SSRS, XML, Silverlight.

License

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

About the Author

saanj


A Software Engineer working in Gurgaon, India.

Hometown is in Kolkata, India.

Working in ASP.NET, C#.NET, AJAX, XML, SQL Server, SQL Server Reporting Services, JavaScript, MOSS 2007.

Enjoys Cricket, National & World Music.

Favourite band include Silkroute, Euphoria, Oasis, Linkin Park, MLTR
Occupation: Software Developer
Location: India India

Other popular ASP.NET articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 1 of 1 (Total in Forum: 1) (Refresh)FirstPrevNext
General[Message Removed]memberMojtaba Vali21:33 12 Apr '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 12 Apr 2008
Editor:
Copyright 2008 by saanj
Everything else Copyright © CodeProject, 1999-2008
Web16 | Advertise on the Code Project