如何从JavaScript调用代码后函数?

时间:2022-04-30 07:15:31

I have a JavaScript function which i call in the onchange handler of a dropdownlist. If the selected value of dropdownlist is "1" i want to call one function in codebehind. Following is the function in JavaScript:

我有一个JavaScript函数,我在下拉列表的onchange处理程序中调用它。如果下拉列表的选择值是“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")
        {
        }
  }

If type is 1 then i want to work following code in codebehind

如果类型为1,那么我希望使用codebehind中的代码

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

Can anybody help to call function in codebehind from JavaScript?

有人能帮忙从JavaScript代码后调用函数吗?

3 个解决方案

#1


2  

The easiest way to do that is to expose the codebehind function as a web service call and use something like jQuery to call it from Javascript.

最简单的方法是将codebehind函数公开为web服务调用,并使用jQuery之类的工具从Javascript调用它。

#2


2  

It may depend on what you want to do with the other values in the drop-down list. But, the easiest way to do this is to wrap your drop-down list in an update panel and handle the OnSelectedIndexChanged event in your code-behind.

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

ASPX page:

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>

Code behind:

背后的代码:

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;
    }
}

Then you wouldn't need to do any javascript processing (unless you wanted to).

然后,您就不需要进行任何javascript处理(除非您愿意)。

#3


0  

please see this post http://usmanshabbir.blogspot.com/2009/10/simplest-way-to-call-server-side.html

请参阅本文http://usmanshabbir.blogspot.com/2009/10/simplest- to-call-server-side.html

#1


2  

The easiest way to do that is to expose the codebehind function as a web service call and use something like jQuery to call it from Javascript.

最简单的方法是将codebehind函数公开为web服务调用,并使用jQuery之类的工具从Javascript调用它。

#2


2  

It may depend on what you want to do with the other values in the drop-down list. But, the easiest way to do this is to wrap your drop-down list in an update panel and handle the OnSelectedIndexChanged event in your code-behind.

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

ASPX page:

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>

Code behind:

背后的代码:

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;
    }
}

Then you wouldn't need to do any javascript processing (unless you wanted to).

然后,您就不需要进行任何javascript处理(除非您愿意)。

#3


0  

please see this post http://usmanshabbir.blogspot.com/2009/10/simplest-way-to-call-server-side.html

请参阅本文http://usmanshabbir.blogspot.com/2009/10/simplest- to-call-server-side.html

相关文章