views:

1437

answers:

3

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.

+4  A: 

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();
Kirtan
I changed to: <a onmouseover="javascript:__doPostBack('<%= ddlFaturaNolar.UniqueID %>', '');">asdasdasdasdad</a> But it doesn't work.
uzay95
A: 

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.

Bob
A: 

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

meep
ClientID doesn't work for buttons, etc. So its better to use UniqueID for every control.
Kirtan
They are generating same results (I tried) :)
uzay95
Okay, then ignore my answer. :-)
meep