简体   繁体   中英

Having trouble calling aspx.cs method

My aspx page contains a drop down mwnu with some values. I would like to call my loadInstances() method in the related aspx.cs page when a change is detected in the drop down. I've got an event listener attached to the menu like this:

$("#ContentPlaceHolder1_DropDownListCounter").change(function () {
    console.log("I've been hit!");
    <%loadInstances();%>;
});

My aspx.cs method looks like this:

public void loadInstances()
{
    foreach (string dataInfo in DataHelper.getInfo())
    {
        ListItem item = new ListItem(dataInfo, dataInfo);
        ListBoxData.Items.Add(item);
    }
}

Where ListBoxData is a ListBox in my aspx page which I would like to be populated on change of the dropdown. I can confirm that the event listener is working as I've got the "I've been hit!" appearing in my console. However the loadInstances() method isn't being called. Have I missing a step to call my aspx.cs method from my aspx page?

Please modify your code like this:-

$("#<%=DropDownListCounter.ClientID%>").change(function () {
    console.log("I've been hit!");
    "<%=loadInstances()%>";        
});

But the operation you are doing for that you need to add AutoPostBack="true" in DropDownList then this script will work. if you don't want AutoPostBack you can use Ajax or UpdatePanel

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