Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 307
uzay95 Avatar asked May 26 '09 12:05

uzay95


2 Answers

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();
like image 140
Kirtan Avatar answered Oct 07 '22 03:10

Kirtan


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 %>','');
like image 24
PURNA MAGUM Avatar answered Oct 07 '22 04:10

PURNA MAGUM