SQLDataSource + SQLDataReader + GridView + ItemTemplate + RadioButtonList 使用方法
WebForm3.aspx |
<%@ Page Language="C#" AutoEventWireup="true" MasterPageFile="~/Site.master" CodeBehind="WebForm3.aspx.cs" Inherits="WebApplication9.WebForm3" %> <asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent"> </asp:Content> <asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent"> <asp:RadioButtonList ID="LeagueRadioBoxList" runat="server" AutoPostBack="true" onselectedindexchanged="LeagueRadioBoxList_SelectedIndexChanged" /> <br /> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Data Source=.\SqlExpress; Initial Catalog=Soccer; Integrated Security=SSPI" /> <asp:GridView ID="GridView1" runat="server" runat="server" AllowPaging="true" PageSize="10" DataSourceID="SqlDataSource1" AutoGenerateColumns="false"> <Columns> <asp:TemplateField HeaderText="Team Name" ItemStyle-Width="200"> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Eval("Team_Name") %>' /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </asp:Content> |
WebForm3.aspx.cs |
using System; using System.Web; using System.Web.UI.WebControls; using System.Data; using System.Data.SqlClient; using System.Text; namespace WebApplication9 { public partial class WebForm3 : System.Web.UI.Page { protected void Page_Init(object sender, EventArgs e) { CreateCheckListBox(); } protected void Page_Load(object sender, EventArgs e) { bool press = true; foreach(ListItem item in LeagueRadioBoxList.Items){ if(item.Selected){ if (item.Value.ToString().Trim() == "0"){ SqlDataSource1.SelectCommand = "SELECT * FROM dbo.Team ;"; }else{ SqlDataSource1.SelectCommand = "SELECT * FROM dbo.Team WHERE League_ID = " + item.Value.ToString().Trim() + ";"; } press = false; } } if (press) SqlDataSource1.SelectCommand = "SELECT * FROM dbo.Team;"; GridView1.PageIndex = 0; } void CreateCheckListBox() { ListItem LeagueRadioBox; LeagueRadioBox = new ListItem(); LeagueRadioBox.Text = "All"; LeagueRadioBox.Value = "0".ToString().Trim(); LeagueRadioBoxList.Items.Add(LeagueRadioBox); String connectionString = "Data Source=.\\SqlExpress; Initial Catalog=Soccer; Integrated Security=SSPI"; SqlConnection con1 = new SqlConnection(connectionString); con1.Open(); StringBuilder sb = new StringBuilder(); sb.Append("SELECT League_ID, League_Name FROM dbo.League;"); SqlCommand cmd1 = new SqlCommand(sb.ToString(), con1); SqlDataReader dr = cmd1.ExecuteReader(); if (dr.HasRows) { while (dr.Read()) { if (dr["League_Name"].ToString().Trim() != null && dr["League_Name"].ToString().Trim() != ""){ LeagueRadioBox = new ListItem(); LeagueRadioBox.Text = dr["League_Name"].ToString(); LeagueRadioBox.Value = dr["League_ID"].ToString(); LeagueRadioBoxList.Items.Add(LeagueRadioBox); } } } con1.Close(); con1.Dispose(); } protected void LeagueRadioBoxList_SelectedIndexChanged(object sender, EventArgs e) { foreach (ListItem item in LeagueRadioBoxList.Items) { if (item.Selected) { if(item.Value.ToString().Trim() == "0"){ SqlDataSource1.SelectCommand = "SELECT * FROM dbo.Team ;"; }else{ SqlDataSource1.SelectCommand = "SELECT * FROM dbo.Team WHERE League_ID = " + item.Value.ToString().Trim() + ";"; } } } GridView1.DataSourceID = SqlDataSource1.ID; GridView1.DataBind(); } } } |