关于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 ...
随机推荐
- [No00008A]bat改变cmd命令提示符颜色
从Windows 95到现在的Windows 10,系统中带的DOS命令提示符软件都是黑白画面,下面教大家几个自定义DOS命令提示符颜色的小技巧. 改变DOS命令提示符的标题:在开始菜单点运行,输入 ...
- 我的操作系统复习——I/O控制和系统调用
上篇博客介绍了存储器管理的相关知识——我的操作系统复习——存储器管理,本篇讲设备管理中的I/O控制方式和操作系统中的系统调用. 一.I/O控制方式 I/O就是输入输出,I/O设备指的是输入输出设备和存 ...
- [LeetCode] Path Sum III 二叉树的路径和之三
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] N-Queens N皇后问题
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens ...
- [开源].NET高性能框架Chloe.ORM-完美支持SQLite
扯淡 这是一款轻量.高效的.NET C#数据库访问框架(ORM).查询接口借鉴 Linq(但不支持 Linq).借助 lambda 表达式,可以完全用面向对象的方式就能轻松执行多表连接查询.分组查询. ...
- 从随机过程到马尔科夫链蒙特卡洛方法(MCMC)
从随机过程到马尔科夫链蒙特卡洛方法 1. Introduction 第一次接触到 Markov Chain Monte Carlo (MCMC) 是在 theano 的 deep learning t ...
- C语言内存分配方法。
当C程序运行在操作系统上时,操作系统会给每一个程序分配一定的栈空间. 堆为所有程序共有的,需要时需要申请访问. 一.栈 局部变量.函数一般在栈空间中. 运行时自动分配&自动回收:栈是自动管理的 ...
- Not Hello World
通常对于一门语言的学习,一般都是以"Hello,World!"开始的.但对于汇编语言的学习,输出这句话并不容易,首先得了解寄存器等硬件知识. 汇编语言要得以运行,首先要讲源文件编译 ...
- 《黑客反汇编揭秘》(2e)推荐书单
Must-Read Books and Other References Books on C/C++: The C Programming Language by Brian W. Kernigha ...
- PAT Basic Level 1001
大纲考察内容 数据存储结构:数组.链 基础算法:递归.排序.计算时间复杂度.空间复杂度.分析算法稳定性 1001.害死人不偿命的(3n+1)猜想 (15) https://www.patest.cn/ ...