Gridview控件
来源:互联网 发布:网络打印机关机后 编辑:程序博客网 时间:2024/05/01 00:18
前台代码:
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" AutoGenerateColumns ="False" GridLines="None" OnRowDeleting="GridView1_RowDeleting" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
<FooterStyle BackColor="#cccccc" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#EFF3FB" />
<EditRowStyle BackColor="#2461BF" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="编辑">
<EditItemTemplate>
<asp:LinkButton ID="update" runat="server" CommandName="update">更新</asp:LinkButton>
<asp:LinkButton ID="cancel" runat="server" CommandName="cancel">取消</asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="edit" runat="server" CommandName="edit">编辑</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("hao") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="学号">
<EditItemTemplate>
<asp:TextBox ID="ssid" runat="server" Width ="80px" Text='<%# Bind("sid") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("sid") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="姓名">
<EditItemTemplate>
<asp:TextBox ID="sname" runat="server" Width ="40" Text='<%# Bind("name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="性别">
<EditItemTemplate>
<asp:TextBox ID="ssex" runat="server" Width ="40" Text='<%# Bind("sex") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="CheckBox1" runat="server" Text='<%# Bind("sex") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="allcheck" runat="server" AutoPostBack="True" OnCheckedChanged="allcheck_CheckedChanged"
Text="全选" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="everycheck" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<FooterTemplate>
</FooterTemplate>
</asp:TemplateField>
</Columns>
<PagerSettings Visible="False" />
</asp:GridView>
<asp:Panel ID="Panel1" runat="server" BackColor="#cccccc" Height="23px" Width="333px">
当前<asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>页共<asp:Label ID="Label4"
runat="server" Text="Label"></asp:Label>页
<asp:Button ID="Button2" runat="server" Text="上一页" OnClick="Button2_Click" />
<asp:Button ID="Button3" runat="server" Text="下一页" OnClick="Button3_Click" /></asp:Panel>
<br />
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="删除所选项" /><br />
<br />
</div>
</form>
后台代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
}
private void bind()
{
GridView1.DataSource = gridview.sqlcomand("SELECT [hao],[sid], [name] ,[sex] FROM [sperson]");
GridView1.DataKeyNames = new string[] { "hao" };
GridView1.AllowPaging = true;
GridView1.PageSize = 5;
GridView1.DataBind();
Label3.Text = Convert.ToString(GridView1.PageIndex + 1);
Label4.Text = Convert.ToString(GridView1.PageCount);
if (GridView1.PageIndex == 0)
{
Button2.Enabled = false;
Button3.Enabled = true;
}
else
{
if (GridView1.PageIndex == GridView1.PageCount - 1)
{
Button2.Enabled = true;
Button3.Enabled = false;
}
else
{
Button2.Enabled = true;
Button3.Enabled = true;
}
}
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
GridViewRow row = GridView1.Rows[e.RowIndex];
string hao = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
string str = "delete from sperson where hao='" + hao + "'";
if (gridview.sqlsc(str) > 0)
{
Response.Write("<script> alert('数据删除成功');</script>");
}
bind();
GridView1.EditIndex =1;
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
bind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
bind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = GridView1.Rows[e.RowIndex];
string hao = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
string ssid=((TextBox)row.FindControl("ssid")).Text.ToString();
string sname = ((TextBox)row.FindControl("sname")).Text.ToString();
string ssex = ((TextBox)row.FindControl("ssex")).Text.ToString();
string str = "update sperson set name='" +sname+ "',sex='"+ssex+"',sid='"+ssid+"' where hao='" + hao + "'";
if ( gridview.sqlgx(str)>0)
{
//Response.Write("<script> alert('数据更新成功');</script>");
}
GridView1.EditIndex = -1;
bind();
}
protected void allcheck_CheckedChanged(object sender, EventArgs e)
{
foreach (GridViewRow gr in GridView1.Rows)
{
CheckBox chk = (CheckBox)gr.Cells[4].FindControl("everycheck");
if (!chk.Checked)
chk.Checked = true;
else
chk.Checked = false;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridViewRow gr in GridView1.Rows)
{
CheckBox chk = (CheckBox)gr.Cells[4].FindControl("everycheck");
if (chk.Checked)
{
Label lb = (Label)gr.Cells[1].FindControl("label2");
string hao = lb.Text.ToString();
string str = "delete from sperson where hao='" + hao + "'";
if (gridview.sqlsc(str) <= 0)
{
Response.Write("<script> alert('数据删除失败');</script>");
}
}
bind();
}
}
protected void Button2_Click(object sender, EventArgs e)
{
GridView1.PageIndex = GridView1.PageIndex - 1;
bind();
}
protected void Button3_Click(object sender, EventArgs e)
{
GridView1.PageIndex = GridView1.PageIndex + 1;
bind();
}
gridview类代码:
#region 数据库连接操作
public static SqlConnection CreateCon()
{
string con = ConfigurationManager.ConnectionStrings["SQLCONN"].ConnectionString;
return (new SqlConnection(con));
}
#endregion
#region 数据查询
public static DataSet sqlcomand(string a)
{
SqlConnection conn = CreateCon();
conn.Open();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(a,conn);
da.Fill(ds);
conn.Close();
return ds;
}
#endregion
#region 数据条件删除
public static int sqlsc(string a)
{
SqlConnection conn = CreateCon();
int biao= 0;
conn.Open();
SqlCommand cmd = new SqlCommand(a,conn);
biao = cmd.ExecuteNonQuery();
if (biao>0)
{
biao = 1;
}
else
{
biao = 0;
}
conn.Close();
return biao;
}
#endregion
#region 数据更新
public static int sqlgx(string b)
{
SqlConnection conn = CreateCon();
conn.Open();
SqlCommand cmd = new SqlCommand(b, conn);
int dr = (int)cmd.ExecuteNonQuery();
conn.Close();
return dr;
}
#endregion
- Gridview控件
- Gridview控件
- GridView控件
- GridView控件
- GridView控件
- GridView控件
- GridView控件
- Gridview控件
- GridView控件
- GridView控件
- GridView控件
- GridView 控件
- GridView控件
- GridView 控件
- GridView控件
- Gridview控件
- GridView控件
- gridview控件
- js的几个小知识
- 系统架构师是怎样炼成的(转自CSDN)
- IP地址和MAC地址的区别与联系
- WEB人机交互的设计FAQ(摘自大牛的对话,转而精之)
- 堆和栈(明确的说明了计算机堆和栈的区别,令我受益很多)
- Gridview控件
- C#控件TreeView与数据库的操作
- SQL2005只有.mdf文件的恢复方法
- 实验一:SQL server 2005高可用性之----日志传送
- ubuntu的brainstorm
- Preview image when uploading it on client side. Available for IE and FF
- pscp实现远程文件和文件夹传输
- 如何识别优秀人才
- JAVA中String与StringBuffer 、StringBuilder的区别