数据修改维护

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="gridview111.aspx.cs" Inherits="test.gridview111" %>

<!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 id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
    </div>
    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" PageSize="2"
        Width="788px" AutoGenerateColumns="False"
        onrowcancelingedit="GridView1_RowCancelingEdit"
        onrowediting="GridView1_RowEditing" onrowdeleting="GridView1_RowDeleting"
        onpageindexchanging="GridView1_PageIndexChanging1"
        onrowupdating="GridView1_RowUpdating1">
        <Columns>
            <asp:TemplateField>
                <EditItemTemplate>
                    <asp:TextBox ID="userid" runat="server" Text='<%# Bind("userid") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="id" runat="server" Text='<%# Bind("userid") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:CommandField />
           
           
            <asp:TemplateField>
           
           
                <EditItemTemplate>
               
                    <asp:TextBox ID="name" runat="server" Text='<%# Bind("username") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("username") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <EditItemTemplate>
                    <asp:TextBox ID="pwd" runat="server" Text='<%# Bind("userpwd") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("userpwd") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <EditItemTemplate>
                    <asp:TextBox ID="dizhi" runat="server" Text='<%# Bind("dizhi") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label3" runat="server" Text='<%# Bind("dizhi") %>'></asp:Label>
                       
                   
           
                </ItemTemplate>
               
             
            </asp:TemplateField>
           
          
               <asp:CommandField ShowEditButton="True" />
               
                <asp:CommandField ShowDeleteButton="True" />
          
        </Columns>
       
       
       
        <PagerTemplate>
                第<asp:Label ID="lblCurrent" runat="server" Text='<%# ((GridView)Container.Parent.Parent).PageIndex+1%>'></asp:Label>页
                共<asp:Label ID="lblTotal" runat="server" Text='<%# ((GridView)Container.Parent.Parent).PageCount%>'></asp:Label>页
                <asp:LinkButton ID="lb_first" runat="server" CommandName="Page"  CommandArgument="First">首页</asp:LinkButton>
                <asp:LinkButton ID="lb_prev" runat="server" CommandName="Page" CommandArgument="Prev">上页</asp:LinkButton>
                <asp:LinkButton ID="lb_next" runat="server" CommandName="Page" CommandArgument="Next">下页</asp:LinkButton>
                <asp:LinkButton ID="lb_last" runat="server" CommandName="Page" CommandArgument="Last">末页</asp:LinkButton>
            </PagerTemplate>
    </asp:GridView>
    </form>
</body>
</html>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using DAL;
using BLL;

namespace test
{
    public partial class gridview111 : System.Web.UI.Page
    {

 

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }

        }

        Userbess2 user = new Userbess2();
        //数据绑定
        private void BindData()
        {
            DataTable dt = user.getAlluser();


            this.GridView1.DataSource = dt;
            this.GridView1.DataBind();

        }

        //protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        //{
        //    int i = e.NewPageIndex;
        //    int j = i < 0 ? 0 : i;
        //    this.GridView1.PageIndex = j;
        //    BindData();

        //}

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {

            this.GridView1.EditIndex = e.NewEditIndex;
            BindData();
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            this.GridView1.EditIndex = -1;
            BindData();
        }


    

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        //删除
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            GridViewRow gvr = this.GridView1.Rows[e.RowIndex];
            Label id = gvr.FindControl("id") as Label;

user.DeleteUser(Convert.ToInt32(id.Text));
            this.GridView1.EditIndex = -1;
            BindData();

        }

        protected void GridView1_PageIndexChanging1(object sender, GridViewPageEventArgs e)
        {
            int i = e.NewPageIndex;
            int j = i < 0 ? 0 : i;
            this.GridView1.PageIndex = j;
            BindData();
        }

        protected void GridView1_RowUpdating1(object sender, GridViewUpdateEventArgs e)
        {
            GridViewRow gvr = this.GridView1.Rows[e.RowIndex];
            TextBox name = gvr.FindControl("name") as TextBox;

            TextBox pwd = gvr.FindControl("pwd") as TextBox;
            TextBox id = gvr.FindControl("userid") as TextBox;
            TextBox dizhi = gvr.FindControl("dizhi") as TextBox;

 


            user.UpdateUser((Convert.ToInt32(id.Text)), name.Text, pwd.Text, dizhi.Text);

            this.GridView1.EditIndex = -1;
            BindData();
        }
    }
}

 

 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="allteacher.aspx.cs" Inherits="考试试题管理系统.WebForm6" %>

<!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>
</head>
<body>

     
    <form id="form1" runat="server">
    <div align="center">
      班级信息
   
 
        <asp:GridView ID="GridView1" runat="server" Width="630px"
            AutoGenerateColumns="False" onrowcommand="GridView1_RowCommand"
            onrowdatabound="GridView1_RowDataBound" style="margin-left: 0px"
            Height="85px" 
            AllowPaging="True" PageSize ="2"
            onrowupdating="GridView1_RowUpdating"
            onpageindexchanging="GridView1_PageIndexChanging1">
            <Columns>
            <asp:TemplateField HeaderText="班级编号">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("classid") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                 
                        <asp:Label ID="id1" runat="server" Text='<%# Bind("classid") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="班级名称">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("classname") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("classname") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
               
                <asp:TemplateField HeaderText="班主任">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("teacher") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("teacher") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
               
                  <asp:TemplateField HeaderText="修改班级信息">
                <ItemTemplate>
                    <asp:ImageButton  ID="btupdate" runat="server" Text="修改" CommandName="update" CommandArgument='<%# Eval("classid") %>' ImageUrl="~/LinkButtonImage/编辑.jpg"/> 
                </ItemTemplate>
               
                </asp:TemplateField>
               
                <asp:TemplateField HeaderText="删除班级信息">
                 <ItemTemplate>
                    <asp:ImageButton  ID="btdel" runat="server" Text="删除" CommandName="del" CommandArgument='<%# Eval("classid") %>' ImageUrl="~/LinkButtonImage/删除.jpg" OnClientClick='return confirm("确实要删除吗?")'/> 
                </ItemTemplate>
               
                </asp:TemplateField>
                <asp:TemplateField HeaderText="添加新班级">
                 <ItemTemplate>
                    <asp:ImageButton  ID="btadd" runat="server" Text="新建" CommandName="add" ImageUrl="~/LinkButtonImage/处理.jpg" /> 
                </ItemTemplate>
               
                </asp:TemplateField>
               
               
               
            </Columns>
             <PagerTemplate>
                    第<asp:Label ID="lb_current" runat="server" Text='<%#((GridView)Container.Parent.Parent).PageIndex+1%>'></asp:Label>页
                共<asp:Label ID="lb_total" runat="server" Text='<%#((GridView)Container.Parent.Parent).PageCount%>'></asp:Label>页
                <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Page" CommandArgument="First">首页</asp:LinkButton>
                <asp:LinkButton ID="LinkButton2" runat="server" CommandName="Page" CommandArgument="Prev">上页</asp:LinkButton>
                <asp:LinkButton ID="LinkButton3" runat="server" CommandName="Page" CommandArgument="Next">下页</asp:LinkButton>
                <asp:LinkButton ID="LinkButton4" runat="server" CommandName="Page" CommandArgument="Last">末页</asp:LinkButton>
                    </PagerTemplate>
           
        </asp:GridView>
   
   
    </div>
    </form>
</body>
</html>

 

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BIL;
namespace 考试试题管理系统
{
    public partial class WebForm6 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) {

                BindData();
            }

        }

        //显示全部信息
        public void BindData()
        {
          
                DataTable dt = BIL.teacherinfo.showteacher();

                this.GridView1.DataSource = dt;
            this.GridView1.DataBind();

        }
     //光棒效果
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow) {

                e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='pink'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor");
            }
        }
        //行命令
        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
       {
           
            if (e.CommandName == "del")
             {
                    string str = e.CommandArgument.ToString();
                    BIL.teacherinfo.deleteteacher(str);
                     BindData();
                }
                else if (e.CommandName == "add")
                {
                  
                   

                    Response.Redirect("addclass.aspx");
                    }
             
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            GridViewRow gvr = this.GridView1.Rows[e.RowIndex];
            Label str = gvr.FindControl("id1") as Label;
            int id = Convert.ToInt32(str.Text);
            Response.Redirect("Updateteacher.aspx?id=" + id);
        }
        //分页
      

        protected void GridView1_PageIndexChanging1(object sender, GridViewPageEventArgs e)
        {
            int i = e.NewPageIndex;
            int j = i < 0 ? 0 : i;
            this.GridView1.PageIndex = j;
            BindData();
        }

 

    }
}

 

 

dbhelp类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using   System.Data ;
using System.Configuration ;
using System.Data .SqlClient;
namespace DAL
{
    public class DBHelper
    {


        //属性:数据库链接对象      
        private static SqlConnection conn;
        public static SqlConnection Conn
        {
            get
            {
                //string connstr = "server=(local);database=netshop_ceshi;integrated security=true";
            string connstr = ConfigurationManager.ConnectionStrings["SqlConnStr1"].ConnectionString;
                if (conn == null) conn = new SqlConnection(connstr);
                if (conn.State == ConnectionState.Closed)
                    conn.Open();
                if (conn.State == ConnectionState.Broken)
                {
                    conn.Close();
                    conn.Open();
                }

                return conn;
            }
        }
        //方法:查询,DataReader      
        public static SqlDataReader GetReader(string SqlStr)
        { SqlCommand cmd = new SqlCommand(SqlStr, Conn); return cmd.ExecuteReader(); }

        public static SqlDataReader GetReader(string SqlStr, SqlParameter[] paras)
        {
            SqlCommand cmd = new SqlCommand(SqlStr, Conn);
            cmd.Parameters.AddRange(paras); return cmd.ExecuteReader();
        }
        //查询:DataTable     
        public static DataTable GetTable(string SqlStr)
        {
            SqlDataAdapter dap = new SqlDataAdapter(SqlStr, Conn);
            DataSet ds = new DataSet(); dap.Fill(ds); conn.Close(); return ds.Tables[0];
        }
        //增删改       
        public static bool Execute(string SqlStr)
        {
            SqlCommand cmd = new SqlCommand(SqlStr, Conn);
            int result = cmd.ExecuteNonQuery(); conn.Close();
            return result > 0;
        }
    }
}
      


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值