ASP.NET Tutorial/Data Binding/CheckBoxList

Материал из .Net Framework эксперт
Версия от 11:59, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

CheckBoxList Control data binding with asp:SqlDataSource

Three properties that affect its layout:
RepeatColumns:    The number of columns of check boxes to display.
RepeatDirection:  The direction in which the check boxes are rendered. 
                  Possible values are Horizontal and Vertical.
RepeatLayout:     Determines whether the check boxes are displayed in an HTML table. 
                  Possible values are Table and Flow.
                  
<%@ 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)
    {
        foreach (ListItem item in cblProducts.Items)
            if (item.Selected)
                lblProduct.Text += "<li>" + item.Text;
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Show CheckBoxList</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:CheckBoxList
        id="cblProducts"
        DataSourceID="srcProducts"
        DataTextField="Title"
        DataValueField="Id"
        RepeatColumns="2"
        Runat="server" />
    
    <asp:Button
        id="btnSubmit"
        Text="Submit"
        OnClick="btnSubmit_Click"
        Runat="server" />
    
    <hr />
    <asp:Label
        id="lblProduct"
        EnableViewState="false"
        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>