:
代碼:
1,前台代碼:
<asp:TextBox ID="TextBox1" runat="server" Text="請輸入尋找內容"></asp:TextBox> <asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click" style=" text-align:center; border:1px solid #333; padding:3px 10px 3px 10px; background:#cfcdcd; ">搜尋</asp:LinkButton>
<br />
<asp:RadioButtonList ID="rbl" runat="server" AutoPostBack="false" RepeatColumns="2" RepeatLayout="Table" OnSelectedIndexChanged="rbl_SelectedIndexChanged">
<asp:ListItem Value="1">產品</asp:ListItem>
<asp:ListItem Value="2">新聞</asp:ListItem>
</asp:RadioButtonList>
2,後台代碼:
public int sel;
protected void LinkButton1_Click(object sender, EventArgs e)
{
if (this.TextBox1.Text != "")
{
string key = this.TextBox1.Text;
if (sel!= 0)
{
switch(sel){
case 1:
Response.Redirect("productlist.aspx?key=" + key + "");
break;
case 2:
Response.Redirect("newslist.aspx?key=" + key + "");
break;
}
}
}
}
protected void rbl_SelectedIndexChanged(object sender, EventArgs e)
{
sel = Convert.ToInt32(rbl.SelectedItem.Value);
}
知識點:
1,RadioButtonList
RadioButtonList 控制項用於建立選項按鈕組。RadioButtonList 控制項中的每個可選項是通過 ListItem 元素來定義的!提示:該控制項支援資料繫結!
一、RadioButtonList 控制項屬性和事件
1、AutoPostBack屬性:用於設定當單擊RadioButtonList控制項時,是否自動回送到伺服器。True表示回送;False(預設)表示不回送。
2、DataSource屬性:用於指定填充清單控制項的資料來源。
3、DataTextField屬性:用於指定DataSource中的一個欄位,該欄位的值對應於清單項目的Text屬性。
4、DataValueField屬性:用於指定DataSource中的一個欄位,該欄位的值對應於清單項目的Value屬性。
5、RepeatColumns屬性:用於指定在CheckBoxList控制項中顯示選項佔用幾列。預設值為0,表示任意多列。
6、RepeatDirection屬性:用於指定CheckBoxList控制項的顯示方向。Vertical時,清單項目以列優先排列的形式顯示;Horizontal時,列項以行優先排列的形式顯示。
7、RepeatLayout屬性:用於設定選項的相片順序。Table(預設)時,以表結構顯示,屬性值為Flow時,不以表結構顯示。
8、SelectedIndex屬性:用於擷取或設定列表中選定項的最低序號索引值。如果清單控制項中只有一個選項被選中,則該屬性工作表示當前選定項的索引值。
9、SelectedItem屬性:用於擷取清單控制項中索引值最小的選定項。如果列表中只有一個選項被選中,則該屬性工作表示當前選定項。通過該屬性可獲得選定項的Text和Value屬性值。
10、TextAlign屬性:用於指定列表中各項文本的顯示位置。當該屬性值為Right(預設)時,文本顯示在選項按鈕的右邊;當屬性值為Left時,文本顯示在選項按鈕的左邊。
11、SelectIndexChange事件:當使用者選擇了列表中的任意選項時,都將引發SelectedIndexChange事件。
二、使用文法
<ASP:RadioButtonList
Id="控制項名稱"
Runat="Server"
AutoPostBack="True | False"
CellPadding="像素"
DataSource="<%資料來源%>"
DataTextField="資料來源的欄位"
DataValueField="資料來源的欄位"
RepeatColumns="欄位數量"
RepeatDirection="Vertical | Horizontal"
RepeatLayout="Flow | Table"
TextAlign="Right | Left"
OnSelectedIndexChanged="事件程式名稱"
>
<ASP:ListItem/> //選項
</ASP:RadioButtonList>