1. When to use __doPostBack
You write 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 anyway 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 code in an event.
So now we are going to manually invoke the postback. Follow the below steps on HTML side
- 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>
- Call the above function on the blur event of T1 like onblur = "enablePostBack();"
- 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.
- Add 2 hidden controls as below
- <input type =hidden name ="__EVENTTARGET" value ="">
- <input type =hidden name ="__EVENTARGUMENT" value ="">
- Include a JavaScript function as below
- function __doPostBack(eventTarget, eventArgument)
- {
- document.Form1.__EVENTTARGET.value = eventTarget;
- document.Form1.__EVENTARGUMENT.value = eventArgument;
- document.Form1.submit();
- }
- 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 professional way of activating the client side postback.

Shalin BhavsarPosted Mar 3, 2015, 4:56 AM
HI,I am working on "Android app",I called web paged from server into "Webview". When my URL load at moment I am finding page an Element using "javascript" and Display/Hide them.But when AutoPostback event fired,My URL not call in Webview,How to handle it?if any one know then plz tell me.
RomaneditedPosted Mar 12, 2011, 1:52 PMEdited Mar 12, 2011, 1:57 PM
Hi, There can be some problems when calling __doPostBack from inside a page which is on a master page or from within a ascx control. You need to use element.UniqueID instead of element.ClientID. See more details on my article: http://extremedev.blogspot.com/2011/03/aspnet-dopostback-not-working.html ----------------------------------------------------------- Development Solutions: http://extremedev.blogspot.com
visakaPosted Feb 24, 2007, 7:24 AM
I am validating a phoneno in textbox.If the phone no is invalid i will display a messagebox specifying error.On clicking ok on the message box i need to focus the cursor in the phoneno field.I have done this.But a problem occurs that when the user enters an invalid number(Previously tried numbers) by pressing the down arrow key and clicks the mouse on next textbox that invalid number will be accepted without the message box display.If i enter the same number again it will be accepted?What is the solution? my code is as follows (javascript) function FormValidate() { if(document.WO_form.txt_Phoneno.value.search(/\d{3}\-\d{3}\-\d{4}/)==-1) { alert( "Invalid phone number.\r\nPlease enter a phone number with the format xxx-xxx-xxxx."); document.getElementById( "txt_Phoneno").focus(); document.getElementById( "txt_Phoneno").value=""; /* clearing previously entered error value*/ } } i am calling it on textchanged event.
visakaPosted Dec 16, 2006, 4:57 AM
I am trying to send mails using C#.Presently i would like to use localhost as SMTP server.I have done it as follows MailMessage mail = new MailMessage(); mail.To = "[email protected]"; mail.From = "[email protected]"; mail.BodyFormat = MailFormat.Text; mail.Subject = "welcome message"; mail.Body = "Hearty welcome to our land"; SmtpMail.SmtpServer = "localhost"; //I tried with IP address of my system// SmtpMail.Send(mail); In addition to this i made some configurations to IIS server (SMTP virtual server) reffering sites :I changed connection and relay to the IP of my machine.It is not working.Also i tried adding the IP as 127.0.0.1 but in vain.I am not getting any error messages. But the mail i sent is found in the folder inetpub/mailroot/Queue.It is not available to the recepient.Do i have to take into account the mailaddresses of sender and receiver.Can i use any valid mailid's(yahoo.gmail......)?Can u suggest an idea to solve it?
visakaPosted Dec 6, 2006, 7:07 AM
In ur reply u have mentioned: server side event for a webcontrol to work, make the AutoPostBack="true" But in the same code i have used a button as follows: <asp:Button ID="cmdship" runat="server" Text="SHIP" OnClick="Button1_Click"/> In the Button1_Click event I have put values of a form to database(in C#).Here i am not using AutoPostBack="true" in the web control.But it works well.Why is it so?
visakaPosted Dec 1, 2006, 7:26 AM
sir, I have two files named controls.aspx and controls.aspx.cs.In controls.aspx i have included a code as below <asp:DropDownList ID="Dropdownlist" runat="server" Width="51px" OnSelectedIndexChanged="DropDownList3_SelectedIndexChanged" > <asp:ListItem>-----</asp:ListItem> <asp:ListItem Value="No">No</asp:ListItem> <asp:ListItem Value="Yes">Yes</asp:ListItem> </asp:DropDownList></div></td> On the second file i have included a code to execute while selecting a particular item from dropdown list(The included code is to fill some textboxes(as web controls) with some hardcoded values while clicking "Yes").The code is as below protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e) { if(Dropdownlist.SelectedItem.Value.Equals("Yes")) { cust_state.Text = "Orissa"; //cust_state is the attribute id of textbox cust_country.Text = "India"; } } But this is not working.No effect while selecting any value from combobox.I have heard of some postback problems.But it is not so clear to me.Can u tell the problem of code and the need of postback in this context.