Tutorial :How to generate fake postback with javascript?


I have Dropdownlist on my page and its selectedindexchanged method created in code behind file (.cs).

I wanted to create fake postback with A tag (onmouseover event).

First i viewed source of html.

<select name="ctl00$cpholder_ana$ddlFaturaNolar" onchange="javascript:setTimeout('__doPostBack(\'ctl00$cpholder_ana$ddlFaturaNolar\',\'\')', 0)" id="ctl00_cpholder_ana_ddlFaturaNolar">      <option selected="selected" value="CHOOSE"></option>      <option value="001926">[ 30.04.2009 - 156.492,00 TL ]  001926</option>  </select>  
  • Then, I copied

    onchange="javascript:setTimeout('__doPostBack(\'ctl00$cpholder_ana$ddlFaturaNolar\',\'\')', 0)"   

And, I created A tag with mouseover event(to make Postback but as it fired by Dropdownlist)

<a onmouseover="javascript:setTimeout('__doPostBack(\'ctl00$cpholder_ana$ddlFaturaNolar\',\'\')', 0)">asdasdasdasdad</a>  

But it didn't drop to SelectedIndexChanged method.

  • First, WHY?
  • Second, How can i do this?

Thanks from now.


You can use this code snippet -

__doPostBack('<%= dropdownlist.UniqueID %>', '');  

You can't use hard-coded unique ids because they may change due to many reasons. For e.g. the id will change if the parent control's id changes, etc. You will have to get the UniqueID rendered from the server side using the code like the one given above.

EDIT: Forgot to mention one important thing. The page will postback only when the selectedIndex of the dropdown changes :) So, if you want to fire that event, change the index of the dropdown list using this and then call the __doPostBack code -

document.getElementById("<%= dropdownlist.UniqueID %>").selectedIndex = 1;  __doPostBack('<%= dropdownlist.UniqueID %>', '');  

EDIT2: Adding upon what Bob said, you can make use of hidden server controls. I suggest you use a asp:Hidden control and hook up its OnValueChanged event. So, whenever you want to post your page back to the server, you just have to change the value of your hidden variable. This way you won't have to use a hidden button.

document.getElementById("<%= hiddenField.UniqueID %>").value = (new Date()).getTime();  


I am sorry to say guys

__doPostBack is not working for DropDownList but it is working for Button. So do one thing

Add a button which should be followed like this

<asp:DropDownList ID="ddlCategory" runat="server" AutoPostBack="true"   OnSelectedIndexChanged="ddlCategory_SelectedIndexChanged">    <asp:ListItem Text="All" Value="0" Selected="True"></asp:ListItem>    <asp:ListItem Text="Published" Value="1"></asp:ListItem>    <asp:ListItem Text="Pending" Value="2"></asp:ListItem>    <asp:ListItem Text="Rejected" Value="3"></asp:ListItem></asp:DropDownList>  

now call your __doPostBack method

__doPostBack('<%= btnRefresh.UniqueID %>','');  


Is it not <%= ddlFaturaNolar.ClientID %>?


Another option you can try, which I find easier and is like likely to break, is to create a hidden (style="display:none" don't use the visible property) asp:Button on your page. When you want to post back you can just simulate the click on that button

 document.getElementById("<%= Button1.ClientID %>").click();  

If you want to stick to posting back on the drop down, make sure the AutoPostBack property of the dropdown is still set to true. Just keep in mind this behavior is a little strange, you are firing a selected changed event on a mouse over. Not only is this a little confusing, you also are increasing the risk of accidental postbacks, as it is very easy to mouse over something and expect nothing to happen.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »