2010-01-19 79 views
3

我有一个JavaScript函数,我在dropdownlist的onchange处理程序中调用。如果dropdownlist的选定值是“1”,我想在代码隐藏中调用一个函数。 以下是在JavaScript函数:如何从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的话,我想工作的下面的代码在代码隐藏

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

可有人帮忙就打电话给在代码隐藏功能,从JavaScript?

回答

2

最简单的方法是将代码隐藏函数公开为Web服务调用,并使用类似jQuery的方法从Javascript调用它。

+0

是否有其他方法 – user42348 2010-01-19 16:14:02

+1

您能否提供一个示例或给我们一个链接? – 2010-01-19 16:19:55

0
+0

为什么不直接链接到文章,而不是链接到广告博客? – 2010-01-19 16:15:25

+0

即时通讯对不起,我刚刚做了这些,因为我已经将链接保存到了我自己的博客中,并且我更容易在离线搜索自己的博客(我使用Windows Live Writer)而不是实际的博客。 – ria 2010-01-19 16:42:35

2

这可能取决于你想在下拉列表中的其他值做什么。但是,最简单的方法是将下拉列表包装在更新面板中,并在代码隐藏中处理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处理(除非你想)。