asp .net datagrid 编辑 取值

问题描述

在DATAGRID中点了编辑按钮后,会把所有字段变成文本框,然后点更新就执行了方法,进行UPDATE这个时候,就是要把用户新填写的东西写入数据库,那么,取得用户输入的数据是关键,这里我用到了一个方法,如下privatevoiddg_estop_item_UpdateCommand(objectsource,System.Web.UI.WebControls.DataGridCommandEventArgse){TextBoxtb=(TextBox)(e.Item.Cells[1].Controls[0]);stringid=tb.Text.Trim();tb=(TextBox)e.Item.Cells[2].Controls[0];stringitem=tb.Text.Trim();略过下面执行的SQL方法}这里我得到了该字段的1列和2列,但是老大有需求,说必须用STRINGKEY的方式获得,也就是说e.Item.Cells[数字].Controls[数字]这个方法不可取,需要用类似e.Item.Cells[字符串].Controls[0]小弟是在无解,请达人帮忙,感激不尽老大有提示,类似如此LinkButtonl=(LinkButton)e.Item.Cells[9].FindControl("lbtn_del");但是,这个通过寻找控件ID的方法,我无法知道那几个TEXTBOX的ID呀,不知道要怎么做

解决方案

解决方案二:
求人帮忙呀感激不尽
解决方案三:
求人帮忙呀感激不尽
解决方案四:
protectedvoidGridView1_RowUpdating(objectsender,GridViewUpdateEventArgse){stringteaid=Convert.ToString(this.GridView1.DataKeys[e.RowIndex].Value);stringteaname=(this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0]asTextBox).Text;stringteaage=(this.GridView1.Rows[e.RowIndex].Cells[3].Controls[0]asTextBox).Text;using(SqlConnectioncon=newSqlConnection("server=.;uid=sa;pwd=;database=login1")){con.Open();stringstr="updateteachersetteaname='"+teaname+"',teaage='"+teaage+"'whereteaid='"+teaid+"'";SqlCommandcmd=newSqlCommand(str,con);cmd.ExecuteNonQuery();}this.GridView1.EditIndex=-1;this.bindGridView();}

以上是,cs<asp:GridViewID="GridView1"runat="server"AutoGenerateColumns="False"OnRowEditing="GridView1_RowEditing"OnRowUpdating="GridView1_RowUpdating"DataKeyNames="teaid"OnRowCancelingEdit="GridView1_RowCancelingEdit"><Columns><asp:BoundFieldDataField="teaid"HeaderText="编号"ReadOnly="true"/><asp:BoundFieldDataField="teaname"HeaderText="姓名"/><asp:BoundFieldDataField="teasex"HeaderText="性别"/><asp:BoundFieldDataField="teaage"HeaderText="年龄"/><asp:BoundFieldDataField="teaspe"HeaderText="专业"/><asp:BoundFieldDataField="teaaddress"HeaderText="地址"/><asp:CommandFieldShowEditButton="True"/></Columns></asp:GridView>

html
解决方案五:
protectedvoidGridView1_RowUpdating(objectsender,GridViewUpdateEventArgse){stringteaid=Convert.ToString(this.GridView1.DataKeys[e.RowIndex].Value);stringteaname=(this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0]asTextBox).Text;stringteaage=(this.GridView1.Rows[e.RowIndex].Cells[3].Controls[0]asTextBox).Text;using(SqlConnectioncon=newSqlConnection("server=.;uid=sa;pwd=;database=login1")){con.Open();stringstr="updateteachersetteaname='"+teaname+"',teaage='"+teaage+"'whereteaid='"+teaid+"'";SqlCommandcmd=newSqlCommand(str,con);cmd.ExecuteNonQuery();}this.GridView1.EditIndex=-1;this.bindGridView();}

以上是,cs<asp:GridViewID="GridView1"runat="server"AutoGenerateColumns="False"OnRowEditing="GridView1_RowEditing"OnRowUpdating="GridView1_RowUpdating"DataKeyNames="teaid"OnRowCancelingEdit="GridView1_RowCancelingEdit"><Columns><asp:BoundFieldDataField="teaid"HeaderText="编号"ReadOnly="true"/><asp:BoundFieldDataField="teaname"HeaderText="姓名"/><asp:BoundFieldDataField="teasex"HeaderText="性别"/><asp:BoundFieldDataField="teaage"HeaderText="年龄"/><asp:BoundFieldDataField="teaspe"HeaderText="专业"/><asp:BoundFieldDataField="teaaddress"HeaderText="地址"/><asp:CommandFieldShowEditButton="True"/></Columns></asp:GridView>

html
解决方案六:
是在2003里使用DATAGRID啊....stringteaname=(this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0]asTextBox).Text;这个取值方式被否决了CELLS[数字]不允许这么取值要我找其他方法
解决方案七:
求人帮忙呀感激不尽
解决方案八:
又学习了,朋友,不过我在做的时候,不是这样做,而是直接在datagrid中把要编辑的项直接用文本框显示。也就是说,在datagrid中在相应的列添加文本框,并在每行添加一个checkbox,这样,通过它选中后,再用代码中的findcontrol()方法,去判断是那个被选中,这样就能保存或更新那个值了。
解决方案九:
楼上的朋友请问相关代码能贴出来看看吗谢谢

时间: 2024-07-28 19:39:13

asp .net datagrid 编辑 取值的相关文章

asp cookie 创建,取值,删除 教程

asp教程 cookie 创建,取值,删除 教程 功能:cookie 常用来对用户进行识别. 原理:  cookie 是一种服务器留在用户电脑中的小文件.每当同一台电脑通过浏览器请求页面时,这台电脑就会发送 cookie;   asp 如果创建cookie  Response.Cookies("aa")'就这一句就己经创建了cookie了.  Response.Cookies("cookies")="111cn.net"  Response.Co

asp.net checkboxlist 取值实例

asp教程.net checkboxlist 取值实例 checkboxlist是复选框的集合,对checkboxlist赋值是没有意义的,要对里面的选 项赋值 checkboxlist1.items.add(new listitem("文本1", "value1")); checkboxlist1.items.add(new listitem("文本2", "value2")); 核心代码 string strapp = &

关于GridView中各种列后台取值的方法

1.GridView中默认的是BoundField在后台可用GridView1.Rows[0].Cells[0]来取值. 2.如果是其他列如HyperLinkField,ButtonField,CheckBoxField则要在后台进行转化. 转化方法如下: ((HyperLink)GridView1.Rows[RowIndex].Cells[0].Controls[0]).Text 对应的Field转化为相应的类型,简单的方法就是Field的类型名如ButtonField去掉Field就OK了

asp.net ajax 获取返回值问题

问题描述 用ajaxpro框架可以这样取值,CheckUser为服务器端函数,只是随便返回一个字符串varvvv=_Default.CheckUser().value;//取得服务器的值AjaxPro在用asp.netajax时候这样取值:varvvv=PageMethods.CheckUser().value;这样会提示"PageMethods.CheckUser().value为空或不是对象",必须PageMethods.CheckUser(CallBack):CallBack为回

(论坛答疑点滴)有的时候DataGrid取值取不到?

datagrid   有的时候想取不是编辑状态的值,这个时候通常不会使用模板列中放置TextBox通过e.Item.FindControl取值,有的网友问为什么取不到?这要看你前台的列怎么写的了,下面分别写出四种情况和这四种情况下面的取值方式: <asp:DataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False" AllowPaging="True"

如何解决ASP.NET新增时多字段取值的问题_实用技巧

ASP.NET 开发人员在卡发时经常会碰到一个情况,就是新增的页面中字段太多,在点击保存的时候需要一个一个的赋值实体或者构建SQL语句去保存.这样不仅浪费体力还需要占用大量的文本行控件去写代码.经过构思是否可以使用一种更方便的办法去解决呢?提高代码的内聚性.  1.思路  我们知道一般新增页面最多的就是一大堆文本框让用户输入内容,然后点击保存按钮进行提交将数据持久化到数据库. 在点击提交的时候传统方式就是一个一个文本框的读取赋值.  HTML代码如下: <asp:TextBox ID="T

asp.net GridView手写事件,包括取主键、取值、更新、选择、删除

刚才在调整网站友情链接管理页面,里面有个简单的GridView.因为更改了架构,所以需要手工给GridView编写编辑.删除等事件.最近也经常碰到有人问我GridView的问题,于是写成经验之书以警后人. 图片是本网站后台的友情链接管理页面:     前两者的代码比较固定,一般都是:  代码如下 复制代码 protected void gvFriendLink_RowEditing(object sender, GridViewEditEventArgs e){ gvFriendLink.Edi

数据库组件 Hxj.Data (三十)(Asp.Net 页面自动赋值与取值)

在项目中的页面中经常会遇到界面的赋值与取值,特别是页面中项特别多的时候,取值和赋值往往是 体力活. 在组件中添加了一个简单的辅助,取值通过Request.From[""]来实现,赋值则 document.getElementById("").value来实现. 下面我们来看示例代码: 例子是:Northwind数据库的Products表 页面代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transi

ASP.NET中用js取CheckBoxList中值的方法实例

用脚本取CheckBoxList中的值,并用"|"将其分开,之后将取到的值放入文本框,返回数据库做添加或者修改   做的一些项目都比较小,而且时间紧,有好多东西都没来得急总结,趁这会还有点时间把前面项目中的用到的知识点分享下,只为以后方便使用. 前台页面代码 复制代码 代码如下: <!--关键字-->     <div id="keyWordsDiv" style="border: 2px solid #6FA1D9; display: