aspx代码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="数据绑定._Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <style type="text/css"> .ni{ background-color:Red;} </style> </head> <body> <form id="form1" runat="server"> <div> <tr id="qw" runat="server"></tr> <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" InsertMethod="Insert" OldValuesParameterFormatString="original_{0}" SelectMethod="GetData" TypeName="数据绑定.DAL.DataSet1TableAdapters.T_UserTableAdapter"> <InsertParameters> <asp:Parameter Name="UserName" Type="String" /> <asp:Parameter Name="State" Type="String" /> </InsertParameters> </asp:ObjectDataSource> <asp:Repeater ID="Repeater1" runat="server" DataSourceID="ObjectDataSource1" onitemcommand="Repeater1_ItemCommand" onitemdatabound="Repeater1_ItemDataBound" > <HeaderTemplate><table><tr><td>用户名</td><td>状态</td><td>操作</td></tr></HeaderTemplate> <ItemTemplate><tr runat="server" id="hrColor"><td><%#Eval("UserName") %></td><td><%#Eval("State") %></td><td><asp:Button ID="btnEnabled" runat="server" Text="启用" CommandName="Enabled" CommandArgument=‘<%#Eval("Id") %>‘ /> <asp:Button ID="btnDisabled" runat="server" Text="禁用" CommandName="Disabled" CommandArgument=‘<%#Eval("Id") %>‘ OnClientClick=‘return confirm("是否禁用?");‘ /></td></tr> </ItemTemplate> <FooterTemplate ></table> </FooterTemplate> </asp:Repeater> <br /> <br /> <br /> </div> </form> </body> </html>
aspx.cs代码
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using 数据绑定.DAL.DataSet1TableAdapters; using System.Data; using System.Web.UI.HtmlControls; namespace 数据绑定 { public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e) { T_UserTableAdapter adapter = new T_UserTableAdapter(); if (e.CommandName == "Enabled") { adapter.UpdateEnabled(Convert.ToInt32(e.CommandArgument)); Repeater1.DataBind(); } else { adapter.UpdateDisabled(Convert.ToInt32(e.CommandArgument)); Repeater1.DataBind(); } } protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { DataRowView viewRow = (DataRowView)e.Item.DataItem; var row = (数据绑定.DAL.DataSet1.T_UserRow)viewRow.Row; if (row.State == "启用") { Button btnQ = (Button)e.Item.FindControl("btnEnabled"); btnQ.Visible = false; } else if (row.State == "禁用") { HtmlTableRow rColor = (HtmlTableRow)e.Item.FindControl("hrColor"); rColor.Attributes["class"] = "ni"; Button btnJ = (Button)e.Item.FindControl("btnDisabled"); btnJ.Visible = false; } else { throw new Exception("非法的State状态!"); } } } } }
数据绑定控件Repeater的使用,布布扣,bubuko.com
时间: 2024-11-02 01:22:15