简体   繁体   中英

How to manually fire postback event to activate selectedIndexChanged event on the server side of a project Asp.net mvc4

Here is my problem, I have an mvc project with a dropdownlist control(Server Side) which i populate with data at run time, i have set autopostback to true,and enabled viewstate ; this dropdown list has a selectedIndexChanged event handler which was suppose to trigger when a user selects something.

However when the page loads, the Page.IsPostBack is always false so the datalist is repopulated and hence no change to trigger the event handler. I have read on this post that while asp web forms take care of postback automatically, on MVC I have to manually write the javascript code to trigger the postback.

Do you guys know of any way i can do that so that it goes to the selectedIndexChanged EventHandler and dont repopulate the dropdown list again?

EDIT: I have already tried

<script type="text/javascript">
     function OnIndexChanged() {
         var databaseList = document.getElementById("FeaturedContent_DatabaseList");

         __doPostBack(FeaturedContent_DatabaseList, '');
         //window.location.href("http://localhost:49729/home/schemas?dbName=SchoolDB");//+ document.getElementById("DatabaseLists").selectedIndex;
         //postMessage("h", "http://localhost:49729/home/schemas?dbName=SchoolDB");
         //__doPost(
     }
</script>

As i can see, you are trying to use MVC. So forget the idea of postback, you will not reload the page every time. You will probably do ajax async request to the server. Here some code to get your event:

 $("#state").change(function(){
       //on-change code goes in here.
       //variable "this" references the state dropdown element
 });

its jQuery. #state is the id of your dropdownlist.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM