views:

178

answers:

3

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:

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

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

Can anybody help to call function in codebehind from JavaScript?

+2  A: 

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.

Otávio Décio
is there any other method
Can you provide an example or give us a link?
Terry Donaghe
A: 

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

ria
Why not link directly to the article instead of to a blog full of ads?
Terry Donaghe
im sorry i just did it cos i had saved the link to my own blog and it was easier for me to search my own blog offline (I use windows live writer) instead of the actual blog.
ria
+2  A: 

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.

ASPX page:

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

KevnRoberts