简体   繁体   English

调用aspx.cs方法时遇到问题

[英]Having trouble calling aspx.cs method

My aspx page contains a drop down mwnu with some values. 我的aspx页面包含带有某些值的下拉mwnu。 I would like to call my loadInstances() method in the related aspx.cs page when a change is detected in the drop down. 当下拉列表中检测到更改时,我想在相关的loadInstances()页面中调用我的loadInstances()方法。 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: 我的aspx.cs方法看起来像这样:

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. 其中ListBoxData是我的aspx页面中的一个ListBox,我希望在下拉列表的更改中填充它。 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. 但是,未调用loadInstances()方法。 Have I missing a step to call my aspx.cs method from my aspx page? 我是否缺少从aspx页面调用aspx.cs方法的步骤?

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. 但是您要执行的操作是需要在DropDownList添加AutoPostBack="true" ,然后此脚本才能工作。 if you don't want AutoPostBack you can use Ajax or UpdatePanel 如果您不希望使用AutoPostBack ,则可以使用AjaxUpdatePanel

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

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