关于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 ...
随机推荐
- 轻量级C#编辑器RoslynPad
简介 RoslynPad是一个Apache 2.0协议开源的轻量级C#编辑器.支持自动完成,语法提示,修改建议等功能.很适合平时随手写个C#程序看看运行结果. 目前版本:0.10.1,无需保存也可以运 ...
- Intellij IDEA 一些不为人知的技巧
Intellij IDEA 一些不为人知的技巧 2016/12/06 | 分类: 基础技术 | 0 条评论 | 标签: IntelliJ 分享到:38 原文出处: khotyn 今天又听了 Jetbr ...
- [LeetCode] Reverse Nodes in k-Group 每k个一组翻转链表
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...
- 从点云到网格(三)Poisson重建
Possion重建是Kazhdan等2006年提出的网格重建方法[1].Possion重建的输入是点云及其法向量,输出是三维网格.Poisson有公开的源代码[2].PCL中也有Poisson的实现. ...
- Java的修饰符
转自:http://blog.csdn.net/manyizilin/article/details/51926230#L42 修饰符: 像其他语言一样,Java可以使用修饰符来修饰类中方法和属性.主 ...
- 逻辑回归 Logistic Regression
逻辑回归(Logistic Regression)是广义线性回归的一种.逻辑回归是用来做分类任务的常用算法.分类任务的目标是找一个函数,把观测值匹配到相关的类和标签上.比如一个人有没有病,又因为噪声的 ...
- 分享10条PHP性能优化的小技巧,帮助你更好的用PHP开发:
1. foreach效率更高,尽量用foreach代替while和for循环. 2. 循环内部不要声明变量,尤其是对象这样的变量. 3. 在多重嵌套循环中,如有可能,应当将最长的循环放在内层,最短循环 ...
- Django models 操作高级补充
Django models 操作高级补充 字段参数补充: 外键 约束取消 ..... ORM中原生SQL写法: raw connection extra
- [个人论文]一种基于GPU并行计算的MD5密码解密方法
求轻喷... [顺便get一份LaTeX论文模板....还是XeLaTex好用.珍爱生命远离CJK http://files.cnblogs.com/files/pdev/paper.zip
- php采集远程图片