惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Privacy & Cybersecurity Law Blog
Vercel News
Vercel News
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
N
Netflix TechBlog - Medium
罗磊的独立博客
F
Fortinet All Blogs
T
Threatpost
Y
Y Combinator Blog
博客园_首页
美团技术团队
Security Latest
Security Latest
博客园 - 三生石上(FineUI控件)
T
Tailwind CSS Blog
V
V2EX - 技术
The Cloudflare Blog
L
LINUX DO - 热门话题
博客园 - 司徒正美
Jina AI
Jina AI
P
Proofpoint News Feed
宝玉的分享
宝玉的分享
C
CXSECURITY Database RSS Feed - CXSecurity.com
C
Cybersecurity and Infrastructure Security Agency CISA
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
WordPress大学
WordPress大学
The Hacker News
The Hacker News
P
Privacy International News Feed
T
The Exploit Database - CXSecurity.com
Scott Helme
Scott Helme
有赞技术团队
有赞技术团队
V
V2EX
Stack Overflow Blog
Stack Overflow Blog
M
MIT News - Artificial intelligence
Latest news
Latest news
NISL@THU
NISL@THU
Google DeepMind News
Google DeepMind News
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Cisco Blogs
雷峰网
雷峰网
Application and Cybersecurity Blog
Application and Cybersecurity Blog
B
Blog RSS Feed
W
WeLiveSecurity
D
DataBreaches.Net
G
Google Developers Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
G
GRAHAM CLULEY
Spread Privacy
Spread Privacy
Know Your Adversary
Know Your Adversary
TaoSecurity Blog
TaoSecurity Blog
S
Securelist
Help Net Security
Help Net Security

博客园 - 一麦

团队开发之环境搭建 .Net下SqlServer命名规范 XXX公司CRM项目开发日志 Coolite Toolkit学习笔记 LINQ新特性简介及入门教程 反射学习 常用正则表达式 正则表达式基本用法 正则表达式基础 页面生命周期小结 设计模式小结 Asp.net 中HttpHandler,HttpModule,IHttpHandlerFactory的原理与应用(一) 3.x新特性 FileStream读写文件【StreamWriter 和 StreamReader】 .NET常用集合类 面向对象点滴 C#基础点滴 全新对待.net---一次全面的旅程 - 一麦 委托和事件--一直以来的痛 - 一麦
GridView的增删改查和分页 - 一麦 - 博客园
一麦 · 2010-12-10 · via 博客园 - 一麦


                <asp:GridView ID="GridView1" runat="server" Width="100%" CellPadding="4" ForeColor="#333333"
                            AutoGenerateColumns="False" AllowPaging="True" PageSize="12" OnRowCancelingEdit="GridView1_RowCancelingEdit"
                            OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnRowDeleting="GridView1_RowDeleting"
                            DataKeyNames="id" OnPageIndexChanging="GridView1_PageIndexChanging"  OnRowDataBound="GridView1_RowDataBound" GridLines="None">
                            <Columns>
                                <asp:TemplateField HeaderText="登录名">
                                    <EditItemTemplate>
                                        <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("admin") %>'></asp:TextBox>
                                    </EditItemTemplate>
                                    <ItemTemplate>
                                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("admin") %>'></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="密码">
                                    <ItemTemplate>
                                        <%# Eval("UserPassword")%>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("UserPassword") %>'></asp:TextBox>&nbsp;
                                    </EditItemTemplate>
                                    <ItemStyle Width="100px" />
                                </asp:TemplateField>
                                <asp:BoundField HeaderText="最后登录时间" DataField="logintime" ReadOnly="True" />
                                <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" HeaderText="操作" />
                            </Columns>
                            <PagerSettings FirstPageText="" LastPageText="" NextPageText="" PreviousPageText="" />
                            <RowStyle Height="20px" BackColor="#F7F6F3" ForeColor="#333333" />
                            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                            <EditRowStyle BackColor="#999999" />
                            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                        </asp:GridView>

SqlConnection conn = null;
    
string connstr = System.Configuration.ConfigurationManager.AppSettings["Connstring"];//在Config中自己搞,不会的看书
    protected void Page_Load(object sender, EventArgs e)
    
{
        
        
if (!IsPostBack)//这句不会,去S吧
        {
            GridViewBind();
//做了一个绑定数据的方法.
        }

         
    }

    
private void GridViewBind()
    
{
         
   
        
string SqlStr = "Select * FROM admin";
        DataSet ds 
= new DataSet();

        
try
        
{
            conn 
= new SqlConnection(connstr);
            
if (conn.State.ToString() == "Closed") conn.Open();
            SqlDataAdapter da 
= new SqlDataAdapter(SqlStr, conn);
            da.Fill(ds, 
"AdminInfo");
            
if (conn.State.ToString() == "Open") conn.Close();

            GridView1.DataSource 
= ds.Tables[0].DefaultView;
            GridView1.DataBind();
        }

        
catch (Exception ex)
        
{
            Response.Write(
"数据库错误,错误原因:" + ex.Message);
            Response.End();
        }
      
    }




    
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    
{
        GridView1.EditIndex 
= e.NewEditIndex;//获取当前行
        GridViewBind();
    }

    
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    
{
        GridView1.EditIndex 
= -1;//取消更新
        GridViewBind();
    }

    
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    
{
        GridView1.PageIndex 
= e.NewPageIndex;
        GridViewBind();
    }

    
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    
{
        
string id = GridView1.DataKeys[e.RowIndex].Values[0].ToString();//获取当前行ID
        string admin = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2")).Text;//强奸转化

        
string SqlStr = "update admin set admin='" + admin + "'where id=" + id;

        
try
        
{
            SqlConnection conn 
= new SqlConnection(connstr);
            
if (conn.State.ToString() == "Closed") conn.Open();
            SqlCommand comm 
= new SqlCommand(SqlStr, conn);
            comm.ExecuteNonQuery();
            comm.Dispose();
            
if (conn.State.ToString() == "Open") conn.Close();

            GridView1.EditIndex 
= -1;
            GridViewBind();
        }

        
catch (Exception ex)
        
{
            Response.Write(
"数据库错误,错误原因:" + ex.Message);
            Response.End();
        }

    }

    
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    
{
       
//我不想写了...因为剩下的都...
        
//类似更新操作,改一下SQL语句就可以了
    }