简体   繁体   English

如何在项目Asp.net MVC4的服务器端手动触发回发事件以激活selectedIndexChanged事件

[英]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 ; 这是我的问题,我有一个带dropdownlist控件(服务器端)的mvc项目,在运行时填充了数据,将autopostback设置为true,并启用了viewstate this dropdown list has a selectedIndexChanged event handler which was suppose to trigger when a user selects something. 此下拉列表具有selectedIndexChanged事件处理程序,该事件处理程序假定是在用户选择某些内容时触发的。

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. 但是,在页面加载时, Page.IsPostBack始终为false,因此将重新填充数据列表,因此无需进行更改即可触发事件处理程序。 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. 我已经阅读了这篇文章,虽然ASP Web表单会自动处理回发,但是在MVC上,我必须手动编写JavaScript代码才能触发回发。

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? 你们知道我能以任何方式做到这一点,以便它进入selectedIndexChanged EventHandler并不再再次填充dropdown list吗?

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. 如我所见,您正在尝试使用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. 您可能会向服务器发出ajax异步请求。 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. 它的jQuery。 #state is the id of your dropdownlist. #state是您的下拉列表的ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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