Want to fire Dropdown SelectedIndexChanged Event in Javascript

ε祈祈猫儿з 提交于 2019-12-30 09:44:28

问题


I have dropdown on my page, I am changing selected value of dropdown from popup window using javascript. I have some logic in dropdown SelectedIndexChanged event, so I need to fire the SelectedIndexChanged event when dropdown selection changed from javascript.


回答1:


document.getElementById('<%= yourDropdown.ClientID %>').onchange();

This should work, if you are still getting some error, you can try like this:

setTimeout('__doPostBack(\'yourcontrolClientSideID\',\'\')', 0);

yourcontrolClientSideID is the ID of Rendered Client ID.




回答2:


Here is a working example:

function fireEvent(element,event){
if(document.createEvent){
 // dispatch for firefox + others
var evt = document.createEvent(”HTMLEvents”);
evt.initEvent(event, true, true ); // event type,bubbling,cancelable
return !element.dispatchEvent(evt);
}
else{
// dispatch for IE
var evt = document.createEventObject();
return element.fireEvent(’on’+event,evt)
}
}



回答3:


Call onchange method like that at client side :

document.getElementById('yourDropdownsClientId').onchange();

EDIT : If you set your dropdown's AutoPostBack property to true, the code above will post your page to server, than your server side event will be called.

But If you want to call your event manually, you can all it anywhere in your page's codebehind like that :

myDropDownList_SelectedIndexChanged(null, new EventArgs());



回答4:


yeah...i think what Canavar said will work but it will have to look like this

document.getElementById('<%=yourDropdown.ClientId%>').onchange();


来源:https://stackoverflow.com/questions/1007190/want-to-fire-dropdown-selectedindexchanged-event-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!