关于GridView中控件的问题
最近做项目报表时,会遇到在Gridview中有一些控件,报表中也会有更新、删除等一系列的操作,但往往会遇到一些控件取值取不到或者找不到控件得问题,通过网上查阅资料对其中的一些做一总结:
前台代码如下:
<asp:GridView ID="gv" runat="server" AutoGenerateColumns="False" CssClass="GridViewStyle"
DataKeyNames="ID" OnRowEditing="gv_RowEditing" OnRowCancelingEdit="gv_RowCancelingEdit"
OnRowUpdating="gv_RowUpdating" OnRowDataBound="gv_RowDataBound">
<FooterStyle CssClass="GridViewFooterStyle" Font-Bold="False" />
<RowStyle CssClass="GridViewRowStyle" />
<SelectedRowStyle CssClass="GridViewSelectedRowStyle" />
<PagerStyle CssClass="GridViewPagerStyle" />
<AlternatingRowStyle CssClass="GridViewAlternatingRowStyle" />
<HeaderStyle CssClass="GridViewHeaderStyle" />
<EmptyDataTemplate>
<asp:Table ID="Table1" runat="server" CellPadding="-1" CellSpacing="" CssClass="GridViewHeaderStyle"
GridLines="Vertical">
<asp:TableRow>
<asp:TableCell HorizontalAlign="center" Text="QAD" Width="40px"></asp:TableCell>
<asp:TableCell HorizontalAlign="center" Text="单位" Width="50px"></asp:TableCell>
<asp:TableCell HorizontalAlign="center" Text="物料描述1" Width="120px"></asp:TableCell>
<asp:TableCell HorizontalAlign="center" Text="描述" Width="120px"></asp:TableCell>
<asp:TableCell HorizontalAlign="center" Text="数量" Width="60px"></asp:TableCell>
</asp:TableRow>
</asp:Table>
</EmptyDataTemplate>
<Columns>
<asp:TemplateField HeaderText="QAD" >
<EditItemTemplate>
<%--<asp:Label ID="txtQAD" runat="server" CssClass="cssQAD" Text='<%# Bind("rp_QAD") %>'></asp:Label>--%>
<asp:TextBox ID="txtQAD" runat="server" AutoComplete="Off" CssClass="SmallTextBox CCPPart cssQAD" Text='<%# Bind("rp_QAD") %>'
Width="100px"></asp:TextBox>
</EditItemTemplate>
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle HorizontalAlign="Center" Width="100px" />
<ItemTemplate>
<%#Eval("rp_QAD")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="基本单位">
<EditItemTemplate>
<asp:Label ID="txtPtUm" runat="server" CssClass="cssPtUm" Text='<%# Bind("rp_PtUm") %>'></asp:Label>
</EditItemTemplate>
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle HorizontalAlign="Center" Width="30px" />
<ItemTemplate>
<%#Eval("rp_PtUm")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="实际采购单位" >
<EditItemTemplate>
<asp:DropDownList ID="ddlUm" Width="35px" runat="server" DataTextField="pc_um" >
</asp:DropDownList>
<asp:TextBox ID="txtUmOther" Width="35px" runat="server"></asp:TextBox>
</EditItemTemplate>
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle HorizontalAlign="Center" Width="70px" />
<ItemTemplate>
<asp:Label ID="labUm" runat="server" Text='<%# Bind("rp_Um") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="实际数量">
<EditItemTemplate>
<asp:TextBox ID="txtQty" runat="server" CssClass="SmallTextBox cssQty" Text='<%# Bind("rp_Qty") %>'
Width="40px"></asp:TextBox>
</EditItemTemplate>
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle HorizontalAlign="Center" Width="40px" />
<ItemTemplate>
<%#Eval("rp_Qty")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="物料描述1">
<EditItemTemplate>
<asp:Label ID="txtQADDesc1" runat="server" CssClass="cssQADDesc1" Text='<%# Bind("rp_QADDesc1") %>'></asp:Label>
</EditItemTemplate>
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle HorizontalAlign="Center" Width="100px" />
<ItemTemplate>
<%#Eval("rp_QADDesc1")%>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="rp_Descript" HeaderText="描述" ReadOnly="True">
<HeaderStyle Width="100px" HorizontalAlign="Right" Font-Bold="False" />
<ItemStyle Width="100px" HorizontalAlign="Right" />
</asp:BoundField>
<asp:CommandField ShowEditButton="True" CancelText="<u>取消</u>" DeleteText="<u>删除</u>"
EditText="<u>编辑</u>" UpdateText="<u>更新</u>">
<HeaderStyle Width="70px" HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
<ControlStyle Font-Bold="False" Font-Size="12px" />
</asp:CommandField>
</Columns>
</asp:GridView>
更新和绑定时时获取控件的值要根据每一行的index,然后根据控件的ID值获取到对应控件的值
后台代码如下:
protected void gv_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
7 string um = ((DropDownList)gv.Rows[e.RowIndex].Cells[6].FindControl("ddlUm")).Text.ToString().Trim();
8 string qaddesc1 = ((Label)gv.Rows[e.RowIndex].Cells[8].FindControl("txtQADDesc1")).Text.ToString().Trim();
9 string qty = ((TextBox)gv.Rows[e.RowIndex].Cells[7].FindControl("txtQty")).Text.ToString().Trim();
13 .......
154 }
179 protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
180 {
181 if (e.Row.RowType == DataControlRowType.DataRow)
182 {
183 string qad= ((TextBox)e.Row.FindControl("txtQAD")).ToString();
188 string dum = ((DropDownList)e.Row.FindControl("ddlUm")).ToString();
198 string lum = ((Label)e.Row.FindControl("labUm")).Text.ToString();
218 .......
222 }
223 }
以上只是在做的过程中遇到的,在以后遇到类似的问题会进行补充更新;
不足之处多多指教,如有雷同请联系楼主!
关于GridView中控件的问题的更多相关文章
- 扩展GridView控件——为内容项添加拖放及分组功能
引言 相信大家对GridView都不陌生,是非常有用的控件,用于平铺有序的显示多个内容项.打开任何WinRT应用或者是微软合作商的网站,都会在APP中发现GridView的使用.“Tiles”提供了一 ...
- .Net语言 APP开发平台——Smobiler学习日志:用Gridview控件设计较复杂的表单
最前面的话:Smobiler是一个在VS环境中使用.Net语言来开发APP的开发平台,也许比Xamarin更方便 一.目标样式 我们要实现上图中的效果,需要如下的操作: 1.从工具栏上的”Smobil ...
- GridView控件隐藏列
GridView隐藏列visible="false" 后你就无法取得这列的值了 下面是迄今为止最简洁的解决方法了. protected void GVList_RowDataBou ...
- GridView控件中加自动排列序号
GridView控件中加自动排列序号 为 Gridview 增加一个新的空白列,如下: <asp:BoundField HeaderText="序号"> < ...
- GRIDVIEW 控件
http://www.cnblogs.com/shanymen/archive/2009/05/22/1486654.html GridView控件是.net里的一个显示数据控件,该控件制作很人性化, ...
- asp.net GridView控件的列属性
BoundField 默认的数据绑定类型,通常用于显示普通文本 CheckBoxField 显示布尔类型的数据.绑定数据为TRUE时,复选框数据绑定列为选中状态:绑定数据为FALSE时,则显示未选中状 ...
- ASP.NET中GridView控件删除数据的两种方法
今天在用GridView控件时,发现了一个问题,就是使用GridView控件在删除数据时的问题.接下来我们通过模板列方式和CommandField方式删除某条数据讲解下两者之间的区别. 方式一:通 ...
- C#中控件数组的讨论
VB用得习惯后,到C#中来觉得很奇怪,如此好的控件数组怎么不见了.“众所周知,控件数组最主要的两个优点:可以循环附值:可以响应同一个事件.从而大大简化了代码.引自http://wenku.baidu. ...
- WPF 中获取DataGrid 模板列中控件的对像
WPF 中获取DataGrid 模板列中控件的对像 #region 当前选定行的TextBox获得焦点 /// <summary> /// 当前选定行的TextBox获得焦点 /// &l ...
随机推荐
- BZOJ 3172: [Tjoi2013]单词 [AC自动机 Fail树]
3172: [Tjoi2013]单词 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 3198 Solved: 1532[Submit][Status ...
- 第9章 Shell基础(2)_Bash基本功能
3. Bash的基本功能 3.1 历史命令与命令补全 (1)历史命令:#history [选项] [历史命令保存文件] ①选项:-c:清空历史命令: -w:把缓存中的历史命令写入文件~/.bash_h ...
- ubuntu14.04 yuv文件的播放及视频信息的查看
1.安装ffmpeg sudo add-apt-repository ppa:mc3man/trusty-media sudo apt-get update sudo apt-get install ...
- 机器学习实战笔记--k近邻算法
#encoding:utf-8 from numpy import * import operator import matplotlib import matplotlib.pyplot as pl ...
- JsonResult作为Action返回值时的错误
JsonResult作为Action返回值时的错误 System.InvalidOperationException: This request has been blocked because ...
- [LeetCode] Binary Tree Maximum Path Sum 求二叉树的最大路径和
Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...
- python读取excel一例-------从工资表逐行提取信息
在工作中经常要用到python操作excel,比如笔者公司中一个人事MM在发工资单的时候,需要从几百行的excel表中逐条的粘出信息,然后逐个的发送到员工的邮箱中.人事MM对此事不胜其烦,终于在某天请 ...
- javascript正则表达式(RegExp)简述
首先我们来思考以下两个个场景 我们使用window操作系统,有时候需要找一个文件,刚刚好这个文件我不知道放哪里去了,这个时候我们该怎么办呢? 我们使用word写论文的时候,不小心将"订价&q ...
- 踢出非法Linux用户
非法添加用户及非法进去的远程操作用户! 01.非法用户闯入系统 最简单的办法就是用 w 命令来检查. 如果确认有非法用户出现在系统内,可以立即 kill 用户相关进程. kill -9 `lsof ...
- 【前端积累】SEO 学习
白帽SEO:网站标题 关键字 描述 网站内容优化 Robot.txt文件 网站地图 增加外链引用 网站结构布局优化:扁平化结构 控制首页链接数量:中小网站100以内,页面导航.底部 ...