Blue Theme Orange Theme Green Theme Red Theme
 
Nevron Gauge for SharePoint
Home | Forums | ASP.NET 2.0 Tutorials | Web Services | How Do I...? | Class Browser | WPF Quick Starts | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
6 Months Free & No Setup Fees ASP.NET Hosting!
Search :       Advanced Search »
Home » ASP.NET » Usage of doPostBack in a real environment

Usage of doPostBack in a real environment

Many atricles describes about "__doPostBack". But here I am trying to say some real practical ways to use it and also mentioned some precautions waant to take before use it. Believe me this article derived from my practical experience and will continue only such items from my side, as only those thing will help a developer.

Author Rank :
Page Views : 15518
Downloads : 0
Rating :
 Rate it
Level : Intermediate
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
Nevron Gauge for SharePoint
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

1. When to use __doPostBack

You write a C# code on one of the event handler (eg:-button click) and also want to invoke the same event from client side using JavaScript (eg:-on blur event of a HTML control). Then from JavaScript side write a function which calls "__doPostBack" and that function want to be call on any HTML event of your preference.

2. Arguments in __doPostBack

There are 2 arguments want to pass with "__doPostBack". First one meant for the control's name (eg:-name of the button control whose click event want to call) whose event want to call from client side. Second one is optional (forget it for avoid complication and just pass "" as value for second argument).

3. One Simple example

I have decided to build a tool which only includes 2 HTML textboxes (T1&T2). I made their property to "runat=server" by right clicking on these controls using Visual Studio. I will enter an amount in Rupees to T1 and after focus lost from T1, the corresponding Dollar amount want to be displayed in T2. So the code written in the "T1_ServerChange" event is as below. Please recall that 2 textboxes mentioned here are HTML controls (with "runat=server" ) not web controls.

private void T1_ServerChange(object sender, System.EventArgs e)

{

          //Just assume 1Dollar = 45 Rupees

          T2.Value = (Int16.Parse(T1.Value) * 45).ToString();

}

Run the program and enter 1 in T1 and press tab. You will see nothing is happening. You try your own any way to fire a lost focus event. But nothing is happening. So the problem is obvious that the posting to the server side (PostBack) is not occurring. So you want to manually fire a postback to work your code.

So this is one particular situation, in which "_doPostBack" has it's power. The situation is a webpage with all controls as HTML controls and you want to invoke a code in an event.

So now we are going to manually invoke the postback. Follow the below steps on HTML side

  1. Write a JavaScript function as follows

    <script language="javascript">
    function enablePostBack()
    {
            //T1 is the first argument(name of our control) I mentioned earlier and give the
           // value of second argument as "" that's all
          
    __doPostBack("T1","");
    }
    </script>

  2. Call the above function on the blur event of T1 like onblur = "enablePostBack();"

  3. Just paste a linkbutton control on the page(Purpose of this will discuss later)

Now run the program and you will see our tool is working well. The only thing we extra done is manually fired a postback using "__doPostBack";. So I think you got the situations in which we want to use "__doPostBack".

4. Please take some precautions.

Before using "__doPostBack" you want to confirm about certain things. After all "__doPostBack" fires a "postback". Just do the things as below:

Just remove the pasted linkbutton from the above mentioned program and run it. You will try to enter a value in T1 and press tab, then a script  error will generate with message as "object expected". Yes expected objected not got. That's the problem and the object is "__doPostBack". ULTIMATELY A POSTBACK WILL ONLY FIRE, IF THERE IS A CONTROL IN THE PAGE WITH A PROPERTY "AUTOPOSTBACK=TRUE". So just paste a web "TextBox" control and make it's "autopostback" property to "True". Then ur program will run without any problem. There is another method also to avoid the above mentioned script error. Just paste a web "linkbutton" control on the page and the program will be perfectly OK. Now you got why I paste a "linkbutton" in our program. But pasting controls to avoid this error is not professional. Do you want to handle it in a professional manner? Just remove the "linkbutton" from the page and add the following to the HTML part.

  1. Add 2 hidden controls as below

    <input type =hidden name ="__EVENTTARGET" value ="">
    <
    input type =hidden name ="__EVENTARGUMENT" value ="">

  2. Include a JavaScript function as below

    function __doPostBack(eventTarget, eventArgument)
    {
         document.Form1.__EVENTTARGET.value = eventTarget;
         document.Form1.__EVENTARGUMENT.value = eventArgument;
         document.Form1.submit();
    }

  3. Now you have only 2 controls (T1&T2) in your page and not extra controls or linkbutton for activating the "postback". Run the program and you will see it works. So using the above javascript function and hidden controls along with "__doPostBack" is ofcourse the proffessional way of activating the client side postback.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Jaish Mathews

An application architect. I have been in IT field since 2000 AD

Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Team Foundation Server Hosting
Become a Sponsor
 Comments

 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.