此為備份,原文請看:http://www.dotblogs.com.tw/mis2000lab/archive/2009/09/09/10502.aspx

 

 

這是一個很基礎、很簡單的補充習題。
既然有讀者發問,我想:初學者到這裡可能也會困住吧?

不如就多一個小範例,作為補充。
-----------------------------------------------------------------------------

我們撰寫一個小範例,題目是--
      「當您點選 DropDownList下拉式選項的時候,例如:點選Yahoo,就會超連結到那個網站」。

這個題目需要兩項基本知識:
    第一,DropDownList的子選項(Item),有兩個屬性。分別是出現在畫面上的選項文字(Text),以及這個 Item選項所傳遞的數值(Value)
   
    第二,當我們點選DropDownList的子選項(Item),會觸發 DropDownList的 SelectedIndexChanged()事件。

 

以下是範例 DropDownList_Hyperlink.aspx的畫面(.aspx檔):

1         點選後,便可以超連結到其他網站:<asp:DropDownList ID="DropDownList1" runat="server"  
2             AutoPostBack="True">
3             <asp:ListItem Value="www.yahoo.com.tw">Yahoo</asp:ListItem>
4             <asp:ListItem Value="www.sina.com">Sina</asp:ListItem>
5             <asp:ListItem Value="www.dotblogs.com.tw/mis2000lab/">MIS2000 Lab.</asp:ListItem>
6             <asp:ListItem Value="www.find.org.tw">III-FIND</asp:ListItem>
7         </asp:DropDownList>

 註解:Yahoo就是子選項的 Text,是出現在畫面上的選項文字。
             www.yahoo.com.tw則是子選項所傳遞的數值(Value)。

2009/9/10補充:建議把完整的url寫進去,例如:http://www.yahoo.com.tw/ ,底下的程式,就不用加上 "http://" 字串。而且如果有些網站,是以 https:// 開頭的,也能用。

上面的 AutoPostBack = "True",請看 本書第3-7-1節的說明。

 

以下是範例 DropDownList_Hyperlink.aspx的後置程式碼(.aspx.vb檔):

1     Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
2
3         Response.Redirect("http://" & DropDownList1.SelectedValue)
4
5     End Sub

 

 

 ================================================================================

C# 版範例:

1     protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
2     {
3         Response.Redirect("http://" + DropDownList1.SelectedValue.ToString());
4     }

 

 

 


 

 

......  寄信給我    mis2000lab (at) 雅虎.com.台灣 ................................................................................................................
ASP.NET專題實務  (文魁出版,VB版 P8187 / C#版P09027

.............................................................................................................. 寄信給我    mis2000lab (at) 雅虎.com.台灣 ........

arrow
arrow
    全站熱搜

    MIS2000 Lab 發表在 痞客邦 留言(0) 人氣()