您的位置:首页 > 编程语言

GridView实用示例代码:全选 反选 批量删除

2009-04-20 19:25 281 查看
GridView实用示例代码:全选 反选 批量删除
2009年04月16日 星期四 10:32

<form id="form1" runat="server">
<asp:SqlDataSource ID="GridViewAdvanceApplication" ConnectionString="<%$ ConnectionStrings:YKConnectionString %>" runat="server" SelectCommand="Select UserId,UserName,NickName,Realname,UserEmail,RegTIme,LastLoginIp From UserInfo"></asp:SqlDataSource>
<div style="margin:0 auto; text-align:center">
<asp:GridView DataKeyNames="UserID" AllowPaging="true" ID="GridView1" runat="server" AutoGenerateColumns="false" >
<FooterStyle BackColor="white" ForeColor="#000066" />
<Columns>
<asp:TemplateField HeaderText="选择">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" >
</asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="UserID" HeaderText="用户ID" ReadOnly="true" />
<asp:BoundField DataField="UserName" HeaderText="用户名" ReadOnly="true"/>
<asp:BoundField DataField="NickName" HeaderText="用户昵称" />
<asp:BoundField DataField="RealName" HeaderText="真实姓名" />
<asp:BoundField DataField="UserEmail" HeaderText="电子邮件" />
<asp:BoundField DataField="RegTime" HeaderText="注册时间" />
<asp:BoundField DataField="LastLoginIp" HeaderText="上次登陆IP" />
<asp:CommandField ShowCancelButton="true" ShowDeleteButton="true" ShowEditButton="true" ShowInsertButton="true" ShowSelectButton="true" />
</Columns>
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="true" ForeColor="White" />
<PagerStyle BackColor="Red" ForeColor="#FFFFFF" HorizontalAlign="center" />
<HeaderStyle BackColor="#000000" Font-Bold="true" ForeColor="White" />
</asp:GridView>
<asp:CheckBox ID="SelectAll" runat="server" AutoPostBack="true" Font-Bold="false" Font-Size="9pt" OnCheckedChanged="SelectAll_CheckedChanged" Text="全选" />
<asp:Button ID="SelectCancel" runat="server" Text="取消" OnClick="SelectCancel_Click" />
<asp:Button ID="SelectInvert" runat="server" Text="反选" OnClick="SelectInvert_Click" />
<asp:Button ID="DeleteData" runat="server" Text="删除" OnClick="DeleteData_Click" />
</div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</form>

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class GridView : System.Web.UI.Page
{
const string ConnStr = "Data Source=LANXIGANG;Initial Catalog=yeekang;Integrated Security=True";
SqlConnection conn = new SqlConnection();
SqlCommand SqlComm;
//conn.ConnectionString = ConnStr;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
databind();
}
}
private void databind()
{
string SqlStr = "Select * From UserInfo";
conn.ConnectionString = ConnStr;
SqlDataAdapter SqlDa = new SqlDataAdapter(SqlStr,conn);
DataSet ds = new DataSet();
conn.Open();
SqlDa.Fill(ds,"UserInfo");
GridView1.DataSource = ds;
GridView1.DataKeyNames = new string[] { "userid"};
GridView1.PageSize = 1;
GridView1.DataBind();
//GridView1.PageCount = "1";

conn.Close();
}
protected void SelectAll_CheckedChanged(object sender, EventArgs e)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox chk = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
if (SelectAll.Checked == true)
{
chk.Checked = true;
}
else
{
chk.Checked = false;
}
}
}
protected void SelectCancel_Click(object sender,EventArgs e)
{
SelectAll.Checked = false;
for (int i = 0; i < GridView1.Rows.Count; i ++ )
{
CheckBox chk = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
chk.Checked = false;
}
}
protected void SelectInvert_Click(object sender, EventArgs e)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox chk = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
if(chk.Checked == true)
{
chk.Checked = false;
}
else
{
chk.Checked = true;
}
}
}
protected void DeleteData_Click(object sender,EventArgs e)
{
SelectAll.Checked = false;
string TestStr = "";
for (int i = 0; i < GridView1.Rows.Count; i++)
{
conn.ConnectionString = ConnStr;
CheckBox chk = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
if(chk.Checked)
{
TestStr += "," + GridView1.DataKeys[i].Value;
SqlCommand SqlCmd = new SqlCommand();
string SqlStr = "Delete From UserInfo Where UserId=" + GridView1.DataKeys[i].Value + "";
SqlComm = new SqlCommand(SqlStr, conn);
conn.Open();
SqlComm.ExecuteNonQuery();
conn.Close();
TestStr = SqlStr;
}

}
databind();
Label1.Text = TestStr;
}
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐