最近做项目报表时,会遇到在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中控件的问题的更多相关文章

  1. 扩展GridView控件——为内容项添加拖放及分组功能

    引言 相信大家对GridView都不陌生,是非常有用的控件,用于平铺有序的显示多个内容项.打开任何WinRT应用或者是微软合作商的网站,都会在APP中发现GridView的使用.“Tiles”提供了一 ...

  2. .Net语言 APP开发平台——Smobiler学习日志:用Gridview控件设计较复杂的表单

    最前面的话:Smobiler是一个在VS环境中使用.Net语言来开发APP的开发平台,也许比Xamarin更方便 一.目标样式 我们要实现上图中的效果,需要如下的操作: 1.从工具栏上的”Smobil ...

  3. GridView控件隐藏列

    GridView隐藏列visible="false" 后你就无法取得这列的值了 下面是迄今为止最简洁的解决方法了. protected void GVList_RowDataBou ...

  4. GridView控件中加自动排列序号

    GridView控件中加自动排列序号 为 Gridview 增加一个新的空白列,如下: <asp:BoundField  HeaderText="序号">    < ...

  5. GRIDVIEW 控件

    http://www.cnblogs.com/shanymen/archive/2009/05/22/1486654.html GridView控件是.net里的一个显示数据控件,该控件制作很人性化, ...

  6. asp.net GridView控件的列属性

    BoundField 默认的数据绑定类型,通常用于显示普通文本 CheckBoxField 显示布尔类型的数据.绑定数据为TRUE时,复选框数据绑定列为选中状态:绑定数据为FALSE时,则显示未选中状 ...

  7. ASP.NET中GridView控件删除数据的两种方法

      今天在用GridView控件时,发现了一个问题,就是使用GridView控件在删除数据时的问题.接下来我们通过模板列方式和CommandField方式删除某条数据讲解下两者之间的区别. 方式一:通 ...

  8. C#中控件数组的讨论

    VB用得习惯后,到C#中来觉得很奇怪,如此好的控件数组怎么不见了.“众所周知,控件数组最主要的两个优点:可以循环附值:可以响应同一个事件.从而大大简化了代码.引自http://wenku.baidu. ...

  9. WPF 中获取DataGrid 模板列中控件的对像

    WPF 中获取DataGrid 模板列中控件的对像 #region 当前选定行的TextBox获得焦点 /// <summary> /// 当前选定行的TextBox获得焦点 /// &l ...

随机推荐

  1. LinkedList<E>源码分析

    LinkedList的数据结构就是双向链表,如下所示: private static class Node<E> { E item;//数据元素 Node<E> next;// ...

  2. PHP 原创视频教程-网站开发新手视频教程

    PHP 原创视频教程-网站开发新手视频教程 有偿招徒弟,,视频免费提供. 本视频教程,面向的是毫无经验的新手,快速上手的. 第一次做视频做的不好的,请各位看官多多包含. 第一部分,HTML 视频教程 ...

  3. [LeetCode] Partition List 划分链表

    Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...

  4. [LeetCode] Search Insert Position 搜索插入位置

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  5. 【教程】简易CDQ分治教程&学习笔记

    前言 辣鸡蒟蒻__stdcall终于会CDQ分治啦!       CDQ分治是我们处理各类问题的重要武器.它的优势在于可以顶替复杂的高级数据结构,而且常数比较小:缺点在于必须离线操作. CDQ分治的基 ...

  6. [开源].NET高性能框架Chloe.ORM-完美支持SQLite

    扯淡 这是一款轻量.高效的.NET C#数据库访问框架(ORM).查询接口借鉴 Linq(但不支持 Linq).借助 lambda 表达式,可以完全用面向对象的方式就能轻松执行多表连接查询.分组查询. ...

  7. 手机浏览器浏览WebApp弹出的键盘遮盖住文本框的解决办法

    手机浏览器浏览WebApp弹出的键盘遮盖住文本框的解决办法 最近碰到Android微信内置浏览H5页面,因为其中的文本输入框(input)放置在靠近页面的中下方,点击文本框以后,则输入框会被弹出的手机 ...

  8. IDEA快捷键

    [常规] Ctrl+Shift + Enter,语句完成 "!",否定完成,输入表达式时按 "!"键 Ctrl+E,最近的文件 Ctrl+Shift+E,最近更 ...

  9. EF 二级缓存 EFSecondLevelCache

    EFSecondLevelCache ======= Entity Framework .x Second Level Caching Library. 二级缓存是一个查询缓存.EF命令的结果将存储在 ...

  10. C#计算一段程序运行时间的三种方法

    第一种方法利用System.DateTime.Now: static void SubTest() { DateTime beforDT = System.DateTime.Now; //耗时巨大的代 ...