<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="BBSTest._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>
</head>
<body>    <script type="text/javascript" language="javascript">
        /*function SetValue(value) {
        document.getElementById("<%= HiddenField1.ClientID %>").value = value
        }*/
        function ShowNo()                        //隐藏两个层 
        {
            document.getElementById("doing").style.display = "none";
            document.getElementById("divLogin").style.display = "none";
        }        function $(id) {            return (document.getElementById) ? document.getElementById(id) : document.all[id];
        }
        function showFloat()                    //根据屏幕的大小显示两个层 
        {
            var range = getRange();            $('doing').style.width = range.width + "px";
            $('doing').style.height = range.height + "px";
            $('doing').style.display = "block";
            document.getElementById("divLogin").style.display = "";
        }
        function getRange()                      //得到屏幕的大小 
        {
            var top = document.body.scrollTop;
            var left = document.body.scrollLeft;
            var height = document.body.clientHeight;
            var width = document.body.clientWidth;
            if (top == 0 && left == 0 && height == 0 && width == 0) {
                top = document.documentElement.scrollTop;
                left = document.documentElement.scrollLeft;
                height = document.documentElement.clientHeight;
                width = document.documentElement.clientWidth;
            }
            return { top: top, left: left, height: height, width: width };
        } 
    </script>    <form id="form1" runat="server">
    <div id="doing" style="filter: alpha(opacity=30); -moz-opacity: 0.3; opacity: 0.3;
        background-color: #000; width: 100%; height: 100%; z-index: 1000; position: absolute;
        left: 0; top: 0; display: none; overflow: hidden;">
        <asp:HiddenField ID="HiddenField1" Value="0" runat="server" />
    </div>
    <div id="divLogin" style="border: solid 10px #898989; background: #fff; padding: 10px;
        width: 780px; z-index: 1001; position: absolute; display: none; top: 50%; left: 50%;
        margin: -200px 0 0 -400px;">
        <div style="padding: 3px 15px 3px 15px; text-align: left; vertical-align: middle;">
            <div>
                <br />
                <br />
                <asp:TextBox ID="TextBox1" runat="server" Width="349px" Visible="false">
                </asp:TextBox>
                用户姓名:
                <asp:TextBox ID="TxtTitle" runat="server" Width="349px">
                </asp:TextBox>
            </div>
            <br />
            <div>
                密&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 码:
                <asp:TextBox ID="TxtContent" runat="server" Width="349px"></asp:TextBox>
            </div>
            <br />
            <div>
                &nbsp; &nbsp;
                <asp:Button ID="BtnCommite" runat="server" Text="确定" OnClick="BtnCommite_Click" />&nbsp;
                <input id="BttCancel" type="button" value="关 闭" onclick="ShowNo()" />
            </div>
        </div>
    </div>
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="3"
            DataKeyNames="id" GridLines="Horizontal" OnRowCancelingEdit="GridView1_RowCancelingEdit"
            BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px"
            Width="487px" Height="150px">
            <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
            <Columns>
                <asp:TemplateField HeaderText="用户ID">
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("id") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="name" HeaderText="用户姓名" />
                <asp:BoundField DataField="password" HeaderText="密码" />
                <asp:TemplateField HeaderText="编辑">
                    <ItemTemplate>
                        <asp:Button ID="btnEdit" runat="server" Text="编辑" OnClientClick="showFloat()" CommandArgument='<%#Eval("id")%>'
                            OnClick="btnEdit_Click" />
                    </ItemTemplate>
                    <ItemStyle HorizontalAlign="Center" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="删除">
                    <ItemTemplate>
                        <asp:Button ID="btnDel" runat="server" Text="删除" CommandArgument='<%#Eval("id")%>'
                            OnClick="btnDel_Click" OnClientClick="JavaScript:return confirm('确定删除吗?')" />
                    </ItemTemplate>
                    <ItemStyle HorizontalAlign="Center" />
                </asp:TemplateField>
            </Columns>
            <RowStyle ForeColor="#4A3C8C" BackColor="#E7E7FF" />
            <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
            <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
            <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
            <AlternatingRowStyle BackColor="#F7F7F7" />
        </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 System.Data.SqlClient;
using System.Collections.Generic;namespace BBSTest
{
    public partial class _Default : System.Web.UI.Page
    {
        private static string sqlstr = "server=.;database=bbszhuyuan;uid=sa;pwd=.";
        public static SqlConnection con = new SqlConnection(sqlstr);
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bind();
            }        }
        //绑定
        public void bind()
        {
            List<selectsql> list = new List<selectsql>();
            con.Open();
            string sql = "select * from selectsql";
            SqlCommand cmd = new SqlCommand(sql, con);
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                selectsql select = new selectsql();
                select.Id = (int)reader["Id"];
                select.Password = reader["password"].ToString();
                select.Name = reader["name"].ToString();
                list.Add(select);
            }
            reader.Close();
            con.Close();            GridView1.DataSource = list;
            GridView1.DataBind();
        }        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 BtnCommite_Click(object sender, EventArgs e)
        {
        }
        protected void btnDel_Click(object sender, EventArgs e)
        {
            string ID = ((Button)sender).CommandArgument;
            string sqlstr = "delete from selectsql where id='" + ID + "'";
            SqlConnection con = new SqlConnection("server=.;database=bbszhuyuan;uid=sa;pwd=.;");
            con.Open();
            SqlCommand sqlcom = new SqlCommand(sqlstr, con);
            int count = (int)sqlcom.ExecuteNonQuery();
            if (count > 0)
            {
                ClientScript.RegisterStartupScript(GetType(), "", "alert('删除成功!')", true);
            }
            else
            {
                ClientScript.RegisterStartupScript(GetType(), "", "alert('删除失败!')", true);
            }
            bind();
        }        protected void btnEdit_Click(object sender, EventArgs e)
        {
            string ID = ((Button)sender).CommandArgument;
            //SqlConnection con = new SqlConnection("server=.;database=bbszhuyuan;uid=sa;pwd=sa.;");
            //con.Open();
            //// GridView bm = (GridView)GridView1.Rows[e.NewEditIndex].DataItem;            string sqlstr = string.Format("update selectsql set name='"
            + TxtTitle.Text.ToString().Trim() + "',password='"
            + TxtContent.Text.ToString().Trim() + "' where id='"
            + ID + "'");
            SqlCommand sqlcom = new SqlCommand(sqlstr, con);
            con.Open();            int count = (int)sqlcom.ExecuteNonQuery();            
            if (count > 0&&e.Equals(""))
            {
                ClientScript.RegisterStartupScript(GetType(), "", "alert('更新成功!')", true);
               
            }
            else
            {
                ClientScript.RegisterStartupScript(GetType(), "", "alert('更新失败!')", true);
            }
            con.Close();
            GridView1.EditIndex = -1;
            bind();        }
    }
}
后台代码

解决方案 »

  1.   

     <asp:Button ID="btnEdit" runat="server" Text="编辑" OnClientClick="showFloat()" CommandArgument='<%#Eval("id")%>'
                                OnClick="btnEdit_Click" />你这个按钮本来就是调用的更新事件啊。
      

  2.   

    也许是遇上关键词,如name,id,password等。
    需要用"["括起来,如
    string sqlstr = string.Format("update [selectsql] set [name]='"
                + TxtTitle.Text.ToString().Trim() + "',[password]='"
                + TxtContent.Text.ToString().Trim() + "' where [id]='"
                + ID + "'");
      

  3.   

    LZ是想点编辑。然后show出那个div 
    编辑之后再确定时更新吧?
      

  4.   

    对呀 ,但是show出div之后,立即就是更新成功 ,郁闷
      

  5.   

    把OnClick="btnEdit_Click",把它的后台放OnClick="BtnCommite_Click"结果是一样的
      

  6.   

    给btnEdit加个CommandName="Edit",在GRID的Editing事件中处理显示DIV,在updating事件中,处理更新