-1

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>
Luca Geretti
  • 10,206
  • 7
  • 48
  • 58
yohannist
  • 4,166
  • 3
  • 35
  • 58
  • Doesn't look like you are using MVC. MVC doesn't use viewstate, or have an autopostback=true, etc. – Mike C. Apr 19 '13 at 13:15

1 Answers1

2

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.

Jaime Yule
  • 981
  • 1
  • 11
  • 20
  • Why would i forget about postback – yohannist Apr 19 '13 at 12:52
  • *MVC* is a designer pattern @fire'fly, There's tons of good books about it. I recommend u to read about it. Before trying to write code. Or just keep with *WebForms*. And yes, you don't have postback on *MVC* projects. – Jaime Yule Apr 19 '13 at 18:28