博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
GridView如何实现双击行进行编辑,更新
阅读量:5092 次
发布时间:2019-06-13

本文共 6253 字,大约阅读时间需要 20 分钟。

虽然标题是原创,但是其实主要的思想呢还是接见了晓风残月的思路,今天在晓风残月的博客上看到了如何利用GridView来实现双击进行编辑。我决定动手实现一下,由于还没有实现双击进行更改操作,所以顺便就把这个功能加了上去,希望对大家能有帮助,同时也谢谢晓风残月。

效果图如下:

 

<%...# Eval("customerid")%>
<%...# Eval("CompanyName")%>
<%...# Eval("ContactName")%>
<%...# Eval("Address")%>

  

string ConStr = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;    protected void Page_Load(object sender, EventArgs e)    ...{        if (!IsPostBack)        ...{            BindData();        }    }    private void BindData()    ...{        SqlConnection MyCon = new SqlConnection(ConStr);        string QueryStr = "SELECT customerid,CompanyName,ContactName,Address FROM customers";        SqlDataAdapter Da = new SqlDataAdapter(QueryStr, MyCon);        DataSet Ds = new DataSet();        Da.Fill(Ds, "Customers");        GridView1.DataSource = Ds.Tables[0];        GridView1.DataKeyNames = new string[] ...{ "customerid" };        GridView1.DataBind();    }    protected override void Render(HtmlTextWriter writer)    ...{        foreach (GridViewRow Row in GridView1.Rows)        ...{            if (Row.RowType == DataControlRowType.DataRow)            ...{                //双击进入编辑模式                Row.Attributes["ondblclick"] = ClientScript.GetPostBackEventReference(GridView1, "Edit$" + Row.RowIndex.ToString(), true);                Row.Attributes["style"] = "cursor:pointer";                Row.Attributes["title"] = "双击进入编辑";                if (Row.RowState == DataControlRowState.Edit)                ...{                    Row.Attributes.Remove("ondblclick");                    Row.Attributes.Remove("style");                    Row.Attributes["title"] = "编辑行";                    for (Int32 i = 1; i < GridView1.Columns.Count; i++)                    ...{                        ((TextBox)Row.Cells[i].Controls[1]).Attributes.Add("onmouseover", "this.select()");                    }                    //双击更新                    Row.Attributes["ondblclick"] = ClientScript.GetPostBackEventReference(GridView1, "Update$" + Row.RowIndex.ToString(), true);                }                //                for (int i = 1; i < Row.Cells.Count; i++)                ...{                    Page.ClientScript.RegisterForEventValidation(Row.UniqueID + "$ctl00", i.ToString());                }            }        }        base.Render(writer);    }    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)    ...{        GridView1.EditIndex = e.NewEditIndex;        BindData();    }    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)    ...{        string ID = GridView1.DataKeys[e.RowIndex].Value.ToString();        //防止非法的输入,预防脚本攻击        string CustomerId = Server.HtmlDecode(((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[1]).Text.ToString());        string CompanyName = Server.HtmlDecode(((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[1]).Text.ToString());        string ContactName = Server.HtmlDecode(((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[1]).Text.ToString());        string Address = Server.HtmlDecode(((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[1]).Text.ToString());        SqlConnection Con = new SqlConnection(ConStr);        string UpdateStr = "UPDATE customers SET companyname=@CompanyName,contactname=@ContactName,address=@Address  WHERE customerid=@ID";        //插入数据的时候用参数来可以预防SQL注入攻击,提高系统的安全性        SqlCommand UpdateCmd = new SqlCommand(UpdateStr,Con);        SqlParameter ParmID = new SqlParameter("@ID", SqlDbType.NVarChar,20);        ParmID.Value = ID;        SqlParameter ParmCName = new SqlParameter("@CompanyName", SqlDbType.NVarChar, 20);        ParmCName.Value = CompanyName;        SqlParameter ParmName = new SqlParameter("@ContactName",SqlDbType.NVarChar,20);        ParmName.Value = ContactName;        SqlParameter ParmAddr = new SqlParameter("@Address",SqlDbType.NVarChar,20);        ParmAddr.Value = Address;        try        ...{            UpdateCmd.Parameters.Add(ParmCName);            UpdateCmd.Parameters.Add(ParmName);            UpdateCmd.Parameters.Add(ParmAddr);            UpdateCmd.Parameters.Add(ParmID);            Con.Open();            UpdateCmd.ExecuteNonQuery();            Con.Close();        }        catch        ...{            ShowMessage("输入格式不正确,请检查");        }        finally        ...{            Con.Close();            GridView1.EditIndex = -1;            BindData();        }    }    private void ShowMessage(string Message)    ...{        Literal TxtMsg = new Literal();        TxtMsg.Text = "";        Page.Controls.Add(TxtMsg);    }    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)    ...{    }    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)    ...{        GridView ControlGridView = (GridView)sender;        if (e.CommandName == "SingleClick")        ...{            int RowIndex = int.Parse(e.CommandArgument.ToString());            int ColIndex = int.Parse(Request.Form["__EVENTARGUMENT"]);            Response.Write("");        }    }}

  http://www.hello-code.com/blog/asp.net/201311/2223.html

转载于:https://www.cnblogs.com/shangshen/p/3613973.html

你可能感兴趣的文章
WebDriverExtensionsByC#
查看>>
我眼中的技术地图
查看>>
lc 145. Binary Tree Postorder Traversal
查看>>
sublime 配置java运行环境
查看>>
在centos上开关tomcat
查看>>
重启rabbitmq服务
查看>>
正则表达式(进阶篇)
查看>>
无人值守安装linux系统
查看>>
【传道】中国首部淘宝卖家演讲公开课:农业本该如此
查看>>
jQuery应用 代码片段
查看>>
MVC+Servlet+mysql+jsp读取数据库信息
查看>>
黑马程序员——2 注释
查看>>
用OGRE1.74搭建游戏框架(三)--加入人物控制和场景
查看>>
转化课-计算机基础及上网过程
查看>>
android dialog使用自定义布局 设置窗体大小位置
查看>>
ionic2+ 基础
查看>>
互联网模式下我们更加应该“专注”
查看>>
myeclipse集成jdk、tomcat8、maven、svn
查看>>
查询消除重复行
查看>>
Win 10 文件浏览器无法打开
查看>>