2010-01-19 84 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處理(除非你想)。