关于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 ...
随机推荐
- MySQL 用户登录密码和远程登录权限问题
1.mysql数据库,忘记root用户登录密码. 解决如下: a.重置密码 #/etc/init.d/mysqld stop #mysqld_safe --user=mysql --skip-gran ...
- PIC10F200/202/204/206/220/222/320/322芯片解密程序复制多少钱?
PIC10F200/202/204/206/220/222/320/322芯片解密程序复制多少钱? PIC10F单片机芯片解密型号: PIC10F200解密 | PIC10F202解密 | PIC10 ...
- [LeetCode] Reverse Vowels of a String 翻转字符串中的元音字母
Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Giv ...
- [LeetCode] Sudoku Solver 求解数独
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- PHP执行文档操作
1.POWINTPOINT系列 之前参与过一个商城的项目,里面有将excel 导出的功能,但是如果要弄成PPT的我们应该怎么办呢?PHP是属于服务器端的 总不能在里面装个Powintpoint吧.于是 ...
- 如何把Spring制作成jar包,然后在项目里运行。
第一步:首先我们先把Spring的代码准备好.如图一 (图1). 第二步:我们在桌面新建一个文件夹,如图二 (图2). 我们要在这个文件夹里新建两个夹,一个文件夹是你项目的包名,也就是我们图1的aop ...
- xv6的课本翻译之——附录B 系统启动器
Appendix B 附录 B Figure B-1 The relationship between logical, linear, and physical addresses. 图B-1:逻辑 ...
- java学习笔记之泛型
"泛型"这个术语的意思就是:"使用与许多许多的类型".泛型在编程语言中出现时,其最初的目的是希望类或方法能够具备最广泛的表达能力.如何做到这一点呢,正是通过解耦 ...
- COGS 2533. [HZOI 2016]小鱼之美
我们可以发现所有的操作当中,只有坐标的增加,没有坐标的减少. 所以我们可以发现这么一个简单的事实,一条鱼一旦出了渔网,那么它就不可能再回来. 但是目前这并没有什么卵用. 我们可以把询问一个矩阵当中的鱼 ...
- python通过函数改变变量取值
严格讲应该是"通过函数调用,改变引用对象".python中,要区分"变量名"和"对象" 如果是类的对象,是引用类型的,那么可以通过函数调用, ...