ASP.NET Tutorial/ASP.net Controls/RadioButtonList — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 12:00, 26 мая 2010

A combination of bullet-list and radio-button list controls

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" 
    Inherits="Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Bullets & Radios</title>
</head>
<body>
    <div id="pageContent">
        <form id="form1" runat="server">
            <asp:RadioButtonList ID="BulletOptions" runat="server" AutoPostBack="True" 
                OnSelectedIndexChanged="BulletOptions_SelectedIndexChanged" 
                RepeatColumns="3" 
                RepeatDirection="Horizontal" />
            <hr />
            <asp:BulletedList ID="BulletedList1" runat="server" 
                BulletImageUrl="/core35/images/bullet.gif" />
        </form>
    </div>
</body>
</html>
File: Default.aspx.cs
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Data : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    if (!IsPostBack)
    {
      FillOptions();
      FillCountries();
    }
  }
  protected void FillOptions()
  {
    BulletOptions.DataSource = Enum.GetValues(typeof(BulletStyle));
    BulletOptions.SelectedIndex = 0;
    BulletOptions.DataBind();
  }
  protected void FillCountries()
  {
    string connString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
    string cmdText = "SELECT DISTINCT country FROM customers";
    DataTable data = new DataTable();
    SqlDataAdapter adapter = new SqlDataAdapter(cmdText, connString);
    adapter.Fill(data);
    BulletedList1.DataSource = data;
    BulletedList1.DataTextField = "country";
    BulletedList1.DataBind();
  }
  protected void BulletOptions_SelectedIndexChanged(object sender, EventArgs e)
  {
    BulletStyle style = (BulletStyle) Enum.Parse(typeof(BulletStyle), BulletOptions.SelectedValue);
    BulletedList1.BulletStyle = style;
  }
}


Data binding with the RadioButtonList Control

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<script runat="server">
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        lblProduct.Text = rblProducts.SelectedItem.Text;
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Show RadioButtonList</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:RadioButtonList
        id="rblProducts"
        DataSourceID="srcProducts"
        DataTextField="Title"
        DataValueField="Id"
        RepeatColumns="3"
        Runat="server" />
    <asp:Button
        id="btnSubmit"
        Text="Submit"
        Runat="server" OnClick="btnSubmit_Click" />
    <hr />
    <asp:Label
        id="lblProduct"
        Runat="server" />
    <asp:SqlDataSource
        id="srcProducts"
        SelectCommand="SELECT Id, Title FROM Products"
        ConnectionString="<%$ ConnectionStrings:Products %>"
        Runat="server" />
    </div>
    </form>
</body>
</html>
            
File: Web.config
<configuration>
  <connectionStrings>
    <add name="Products" 
         connectionString="Data Source=.\SQLEXPRESS;
         AttachDbFilename=|DataDirectory|MyDatabase.mdf;Integrated Security=True;User Instance=True" />
  </connectionStrings>
</configuration>  
Three properties that have an effect on its layout:
RepeatColumns:     The number of columns of radio buttons to display.
RepeatDirection:   The direction that the radio buttons are repeated. 
                   Possible values are Horizontal and Vertical.
RepeatLayout:      Determines whether the radio buttons are displayed in an HTML table. 
                   Possible values are Table and Flow.


Get selected item index from asp:RadioButtonList (VB.net)

<%@ Page Language="VB" %>
<script runat="server">
   sub Submit(Sender as Object, e as EventArgs)
      dim strIncome as string = lbIncome.SelectedItem.Text
      dim strAge as string = rlAge.SelectedItem.Text
      
      lblMessage.Text = "Hello " & tbName.Text & "!" & _
         "Your income is: " & strIncome & "<br>" & _
         "Your age is: " & strAge & "<br>"
      
      if rlAge.SelectedIndex < 3 then
         lblMessage.Text += "You"re a young one!"
      else
         lblMessage.Text += "You"re a wise one!"
      end if
      
      if cbNewsletter.Checked then
         lblMessage.Text += "You will be receiving our" & _
            " newsletter shortly."
      end if
   end sub   
</script>
<html><body>
   <form runat="server">
      <asp:Label id="lblMessage" runat="server" />
      
      Enter your name: 
      <asp:TextBox id="tbName" runat="server" />
      
      Choose your age:<br>
      <asp:RadioButtonList id="rlAge" runat="server"
         RepeatDirection="horizontal">
         <asp:ListItem><18</asp:ListItem>
         <asp:ListItem>19-24</asp:ListItem>
         <asp:ListItem>25-34</asp:ListItem>
         <asp:ListItem>35-49</asp:ListItem>
         <asp:ListItem>50-65</asp:ListItem>
      </asp:RadioButtonList>
      
      Choose your income:<br>
      <asp:ListBox id="lbIncome" runat="server"
         size=1>
         <asp:ListItem>< $9</asp:ListItem>
         <asp:ListItem>$10-$99</asp:ListItem>
         <asp:ListItem>$100-$499</asp:ListItem>
         <asp:ListItem>> $500</asp:ListItem>
      </asp:ListBox>
     
      Do you want to receive our newsletter?<br>
      <asp:Checkbox id="cbNewsletter" runat="server"
         Text="Yes!" />
      
      <asp:Button id="btSubmit" runat="server"
         Text="Submit" OnClick="Submit" />
   </form>
</body></html>