效果图:
代码:
1,前台代码:
<asp:TextBox ID="TextBox1" runat="server" Text="请输入查找内容"></asp:TextBox> <asp:LinkButton ID="LinkButton1" runat="server" οnclick="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>