How can I call the codebehind function from JavaScript?

I have a JavaScript function that I call in the onchange handler of the drop down list. If the selected dropdownlist value is "1", I want to call one function in codebehind. The following is a function in 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 1, then I want to work with code in codebehind

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

Can someone help calling a function in codebehind from JavaScript?

+3
source share
3 answers

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

+2
source

, . - 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>

:

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

javascript ( ).

+2

Source: https://habr.com/ru/post/1729093/


All Articles