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.
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 %>','');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With