C# 如何从JavaScript调用codebehind函数?

C# 如何从JavaScript调用codebehind函数?,c#,asp.net,javascript,C#,Asp.net,Javascript,我在dropdownlist的onchange处理程序中调用了一个JavaScript函数。如果dropdownlist的选定值为“1”,我想调用codebehind中的一个函数。 以下是JavaScript中的函数: function GetAdmissionType() { InitComponents(); var type=""; type=document.getElementById(dlAdmissionType.id).v

我在dropdownlist的onchange处理程序中调用了一个JavaScript函数。如果dropdownlist的选定值为“1”,我想调用codebehind中的一个函数。 以下是JavaScript中的函数:

function  GetAdmissionType()
    {
        InitComponents();
        var type="";
        type=document.getElementById(dlAdmissionType.id).value;
        document.getElementById(hdnAdmissionType.id).value=document.getElementById(dlAdmissionType.id).value;
        if(type=="1")
        {
        }
  }
如果类型是1,那么我想在codebehind中处理以下代码

public void LoadSemesters()
{
   //code to load other dropdownlists
}

有人可以帮助从JavaScript调用codebehind函数吗?

最简单的方法是将codebehind函数公开为web服务调用,并使用类似jQuery的东西从JavaScript调用它。

请参阅本文

这可能取决于您希望如何处理下拉列表中的其他值。但是,最简单的方法是将下拉列表包装在更新面板中,并在代码中处理OnSelectedIndexChanged事件

ASPX页面:

    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:DropDownList ID="DropDownList1" runat="server" onselectedindexchanged="DropDownList1_SelectedIndexChanged">
                <asp:ListItem Value="1">item 1</asp:ListItem>
                <asp:ListItem Value="2">item 2</asp:ListItem>
                <asp:ListItem Value="3">item 3</asp:ListItem>
                <asp:ListItem Value="4">item 4</asp:ListItem>
            </asp:DropDownList>
        </ContentTemplate>
    </asp:UpdatePanel>

那么你就不需要进行任何javascript处理(除非你愿意)。

为什么不直接链接到文章而不是一个充满广告的博客?很抱歉,我这么做了,因为我已经将链接保存到了我自己的博客,而且离线搜索我自己的博客更容易(我使用windows live writer)你能提供一个例子或者给我们一个链接吗?
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    switch (DropDownList1.SelectedValue)
    {
        case "1":
            LoadSemesters();
            break;
        case "2":
        case "3":
        case "4":
        default:
            // do something
            break;
    }
}