在ASP.NET中访问DataGrid中所有控件的值[1]

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 littlehb 的 blog

在asp.net中访问datagrid中所有控件的值
作者:孟宪会 出自:【孟宪会之精彩世界】 发布日期:2003年5月5日 2点50分20秒

要在asp.net中访问datagrid中所有控件的值,可以遍历datagrid中每个控件:下面就是实现这一功能的aspx代码和脚本代码【vb.net】

<%@ page language="vb" autoeventwireup="false" codebehind="datagridaccessvalues.aspx.vb" inherits="aspxweb.datagridaccessvalues"%> <!doctype html public "-//w3c//dtd html 4.0 transitional//en"> <html> <head> </head> <body> <form runat="server" id="form1"> <asp:datagrid id="mydatagrid" runat="server" width ="100%" autogeneratecolumns="false"> <itemstyle verticalalign="top"></itemstyle> <columns> <asp:boundcolumn datafield="name" headertext="name"></asp:boundcolumn> <asp:templatecolumn headertext="age"> <itemtemplate> <asp:textbox id="agefield" columns="5" text='<%# databinder.eval(container.dataitem,"age") %>' runat="server"></asp:textbox> </itemtemplate> </asp:templatecolumn> <asp:templatecolumn headertext="graduate"> <itemtemplate> <asp:checkbox id="isgraduatefield" checked='<%# databinder.eval(container.dataitem,"isgraduate") %>' runat="server"></asp:checkbox> </itemtemplate> </asp:templatecolumn> <asp:templatecolumn headertext="skills"> <itemtemplate> <asp:checkboxlist id="checkboxlist1" runat="server"> <asp:listitem value="c#" selected="true">c#</asp:listitem> <asp:listitem value="c++">c++</asp:listitem> <asp:listitem value="vb">vb</asp:listitem> <asp:listitem value="sql server" selected="true">sql server</asp:listitem> </asp:checkboxlist> </itemtemplate> </asp:templatecolumn> <asp:templatecolumn headertext="experience"> <itemtemplate> <asp:radiobuttonlist id="radiobuttonlist1" runat="server"> <asp:listitem value="1">1 year</asp:listitem> <asp:listitem value="3">3 year</asp:listitem> <asp:listitem value="5" selected="true">5 year</asp:listitem> <asp:listitem value="10">10 year</asp:listitem> </asp:radiobuttonlist> </itemtemplate> </asp:templatecolumn> <asp:templatecolumn headertext="degree"> <itemtemplate> <asp:dropdownlist id="dropdownlist1" runat="server"> <asp:listitem value="highschool">highschool</asp:listitem> <asp:listitem value="graduate" selected="true">graduate</asp:listitem> <asp:listitem value="masters">masters</asp:listitem> <asp:listitem value="phd">phd</asp:listitem> </asp:dropdownlist> </itemtemplate> </asp:templatecolumn> </columns> </asp:datagrid> <br> <asp:button id="getvalues" onclick="getvalues_click" runat="server" text="getvalues"></asp:button> <br> <asp:label id="resultfield" runat="server"></asp:label> </form> </body> </html> 后端代码: imports system.collections public class datagridaccessvalues inherits system.web.ui.page protected withevents mydatagrid as system.web.ui.webcontrols.datagrid protected withevents getvalues as system.web.ui.webcontrols.button protected withevents resultfield as system.web.ui.webcontrols.label #region " web 窗体设计器生成的代码 " '该调用是 web 窗体设计器所必需的。 <system.diagnostics.debuggerstepthrough()> private sub initializecomponent() end sub private sub page_init(byval sender as system.object, byval e as system.eventargs) handles mybase.init 'codegen: 此方法调用是 web 窗体设计器所必需的 '不要使用代码编辑器修改它。 initializecomponent() end sub #end region public sub getvalues_click(byval sender as object, byval e as system.eventargs) handles getvalues.click dim result as string = "" dim datagriditem as datagriditem for each datagriditem in mydatagrid.items dim name as string = datagriditem.cells(0).text dim agefield as textbox = datagriditem.findcontrol("agefield") dim age as integer = system.convert.toint64(agefield.text).tostring() dim isgraduatefield as checkbox = datagriditem.findcontrol("isgraduatefield") dim isgraduate as boolean = isgraduatefield.checked dim skills as string = "" dim item as listitem dim checkboxlist1 as checkboxlist = datagriditem.findcontrol("checkboxlist1") for each item in checkboxlist1.items if item.selected then skills = skills + item.value + "," end if next skills = skills.trimend(",") dim radiobuttonlist1 as radiobuttonlist = datagriditem.findcontrol("radiobuttonlist1") dim experience as string = radiobuttonlist1.selecteditem.text dim dropdownlist1 as dropdownlist = datagriditem.findcontrol("dropdownlist1") dim degree as string = dropdownlist1.selecteditem.text result = result + name result = result + "[年龄:" + age.tostring() + "]" result += " " if isgraduate then result += "已经毕业 , " else result += "没有毕业 , " end if result += "技能:" + skills + " , " result += "经验: " + experience + " , 和 " result += "学位: " + degree + "。" result += "<br>" next resultfield.text = result end sub private sub page_load(byval sender as system.object, byval e as system.eventargs) handles mybase.load '在此处放置初始化页的用户代码 if not page.ispostback then dim data as arraylist = new arraylist() data.add(new person("net_lover", 33, true)) data.add(new person("孟子e章", 28, true)) data.add(new person("精彩世界", 20, false)) data.add(new person("xml开发", 27, true)) mydatagrid.datasource = data mydatagrid.databind() end if end sub end class public class person private _name as string private _age as integer private _isgraduate as boolean public sub new(byval name as string, byval age as integer, byval isgraduate as boolean) _name = name _age = age _isgraduate = isgraduate end sub public property name() as string get return _name end get set(byval value as string) _name = value end set end property public property age() as integer get return _age end get set(byval value as integer) _age = value end set end property public property isgraduate() as boolean get return _isgraduate end get set(byval value as boolean) _isgraduate = value end set end property end class c#例子代码: <%@ page language="c#" %> <%@ import namespace="system.collections" %> <script runat="server"> void page_load(object sender, eventargs e) { if(!page.ispostback){ arraylist data = new arraylist(); data.add(new person("tom",33,true)); data.add(new person("jhon",39,false)); data.add(new person("mark",20,false)); data.add(new person("linda",27,true)); mydatagrid.datasource = data; mydatagrid.databind(); } } void getvalues_click(object sender, eventargs e) { string result = ""; foreach(datagriditem datagriditem in mydatagrid.items){ //get name from cell[0] string name = datagriditem.cells[0].text; //get text from textbox in cell[1] string age = ((textbox)datagriditem.findcontrol("agefield")).text; //get checked property of checkbox control bool isgraduate = ((checkbox)datagriditem.findcontrol("isgraduatefield")).checked; // get values from checkboxlist string skills = ""; foreach(listitem item in ((checkboxlist)datagriditem.findcontrol("checkboxlist1")).items){ if (item.selected){ skills += item.value + ","; } } skills = skills.trimend(','); //get radiobuttonlist selected text string experience = ((radiobuttonlist)datagriditem.findcontrol("radiobuttonlist1")).selecteditem.text; //get dropdownlist selected text string degree = ((dropdownlist)datagriditem.findcontrol("dropdownlist1")).selecteditem.text; // build string to show result. result += name; result += " [age -" + age + "] "; if (isgraduate){ result += "is graduate , "; }else{ result += "is not graduate , "; } result += "has skills[" + skills + "] , "; result += "has " + experience + " experience , and " ; result += "has " + degree + " degree." ; result += "<br></p> <div class="clear-both"></div> </div> <div class="pages"><a href=20050818013747331.htm target="_self" >首页</a> <a href=20050818013747331_2.htm target="_self">下页</a> <a href=20050818013747331_2.htm target="_self">尾页</a> <a href=20050818013747331.htm target="_self"><strong>[1]</strong></a> <a href=20050818013747331_2.htm target="_self">[2]</a> </div> <div class="keywords"> <strong>本文关键:</strong>在ASP.NET中访问DataGrid中所有控件的值 </div> <div class="ad_doc_ad_2"> <script type="text/javascript"><!-- google_ad_client = "pub-1022332794981269"; google_ad_width = 728; google_ad_height = 90; google_ad_format = "728x90_as"; google_ad_type = "text_image"; //2007-03-23: 方案站728x90 google_ad_channel = "2351411502"; google_color_border = "FFFFFF"; google_color_bg = "FFFFFF"; google_color_link = "3D81EE"; google_color_text = "4C4C4C"; google_color_url = "6C82B5"; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script> </div> <div class="clear-both"></div> </div> </div> <div id="content_right"> <div class="cbox" id="relate_doc"> <div class="darkboxTitle"><span>  相关方案</span></div> <div class="ad_site_index_right_1"> <!-- SiteSearch Google --> <form method="get" action="http://www.google.cn/custom" target="google_window"> <table border="0" bgcolor="#ffffff"> <tr><td nowrap="nowrap" valign="top" align="left" height="32"> <a href="http://www.google.com/"> <img src="http://www.google.com/logos/Logo_25wht.gif" border="0" alt="Google" align="middle"></img></a> <br/> <input type="hidden" name="domains" value="www.cn-doc.com"></input> <label for="sbi" style="display: none">输入您的搜索字词</label> <input type="text" name="q" size="15" maxlength="255" value="" id="sbi"></input> <label for="sbb" style="display: none">提交搜索表单</label> <input type="submit" name="sa" value="搜索" id="sbb"></input> </td></tr> <tr> <td nowrap="nowrap"> <table> <tr> <td> <input type="radio" name="sitesearch" value="" id="ss0"></input> <label for="ss0" title="搜索网络"><font size="-1" color="#000000">Web</font></label></td> <td> <input type="radio" name="sitesearch" value="www.cn-doc.com" checked id="ss1"></input> <label for="ss1" title="搜索 www.cn-doc.com"><font size="-1" color="#000000">www.cn-doc.com</font></label></td> </tr> </table> <input type="hidden" name="client" value="pub-1022332794981269"></input> <input type="hidden" name="forid" value="1"></input> <input type="hidden" name="channel" value="9722277709"></input> <input type="hidden" name="ie" value="GB2312"></input> <input type="hidden" name="oe" value="GB2312"></input> <input type="hidden" name="flav" value="0000"></input> <input type="hidden" name="sig" value="Dl1iwOxBnXTtaDIa"></input> <input type="hidden" name="cof" value="GALT:#008000;GL:1;DIV:#336699;VLC:663399;AH:center;BGC:FFFFFF;LBGC:336699;ALC:0000FF;LC:0000FF;T:000000;GFNT:0000FF;GIMP:0000FF;LH:50;LW:200;L:http://www.cn-doc.com/images/forgoogle.gif;S:http://www.cn-doc.com;FORID:1"></input> <input type="hidden" name="hl" value="zh-CN"></input> </td></tr></table> </form> <!-- SiteSearch Google --> </div> <ul class="darklist"> <li><a href="../../_soft_dot_net_tech_info/2005_08_18_01/20050818013747891.htm" title="[收藏]Asp.Net 连接Oracle数据库 出现&quot;找到 Oracle 客户端和网络组件&quot; 彻底解决方法!" target="_blank">[收藏]Asp.Net 连接O</a>…</li><li><a href="../../_soft_dot_net_tech_info/2005_08_18_01/20050818013747584.htm" title="Attribute使用(二)" target="_blank">Attribute使用(二)</a>…</li><li><a href="../../_soft_dot_net_tech_info/2005_08_18_01/20050818013748282.htm" title="编程模式" target="_blank">编程模式</a>…</li><li><a href="../../_soft_dot_net_tech_info/2005_08_18_01/20050818013748200.htm" title="Effective C# 5&#58 警惕隐式box和unbox操作对程序性能的影响" target="_blank">Effective C# 5&</a>…</li><li><a href="../../_soft_dot_net_tech_info/2005_08_18_01/20050818013748844.htm" title="令你的网页速度大大提高" target="_blank">令你的网页速度大大提高</a>…</li><li><a href="../../_soft_dot_net_tech_info/2005_08_18_01/20050818013748625.htm" title="The MD4 Class.(C#)" target="_blank">The MD4 Class.(</a>…</li><li><a href="../../_soft_dot_net_tech_info/2005_08_18_01/20050818013748247.htm" title=".NET 1.1中预编译ASP.NET页面实现原理浅析 [1] 自动预编译机制浅析" target="_blank">.NET 1.1中预编译ASP</a>…</li><li><a href="../../_soft_dot_net_tech_info/2005_08_18_01/20050818013749902.htm" title="关于在Web下操作WORD文档中的问题(Win32Exception)。" target="_blank">关于在Web下操作WORD文档</a>…</li><li><a href="../../_soft_dot_net_tech_info/2005_08_18_01/20050818013749782.htm" title="关于使用DbDataAdapter.Update 方法更新数据库" target="_blank">关于使用DbDataAdapt</a>…</li><li><a href="../../_soft_dot_net_tech_info/2005_08_18_01/20050818013749218.htm" title="Crystal Reports 产品家族" target="_blank">Crystal Reports</a>…</li><li><a href="../../_soft_dot_net_tech_info/2005_08_18_01/20050818013747406.htm" title="经典水晶报表设计——用交叉报表实现成绩单!" target="_blank">经典水晶报表设计——用交叉报表</a>…</li><li><a href="../../_soft_dot_net_tech_info/2005_08_18_01/20050818013746428.htm" title="The UDPChat Source(VB.NET)" target="_blank">The UDPChat Sou</a>…</li><li><a href="../../_soft_dot_net_tech_info/2005_08_18_01/20050818013746982.htm" title=".Net/C# 封装磁盘目录文件搜索功能的工具类 (实现了与搜索相关的事件,以便插入客户处理代码)" target="_blank">.Net/C# 封装磁盘目录文</a>…</li><li><a href="../../_soft_dot_net_tech_info/2005_08_18_01/20050818013746713.htm" title="ASP.NET 中 Session 实现原理浅析 [2] 状态管理器" target="_blank">ASP.NET 中 Sessi</a>…</li><li><a href="../../_soft_dot_net_tech_info/2005_08_18_01/20050818013746438.htm" title="NewsBar 辅助软件 发布" target="_blank">NewsBar 辅助软件 发布</a>…</li><li><a href="../../_soft_dot_net_tech_info/2005_08_18_01/20050818013745715.htm" title="如何在水晶报表中动态添加字段" target="_blank">如何在水晶报表中动态添加字段</a>…</li><li><a href="../../_soft_dot_net_tech_info/2005_08_18_01/20050818013745989.htm" title="ASP.net实现信用卡检查和自定义确认控件(二)" target="_blank">ASP.net实现信用卡检查和</a>…</li><li><a href="../../_soft_dot_net_tech_info/2005_08_18_01/2005081801374573.htm" title="使用TreeView实现无限级扩展节点(原创)" target="_blank">使用TreeView实现无限级</a>…</li><li><a href="../../_soft_dot_net_tech_info/2005_08_18_01/20050818013745283.htm" title="即将发表创建型模式论文敬请关注" target="_blank">即将发表创建型模式论文敬请关注</a>…</li><li><a href="../../_soft_dot_net_tech_info/2005_08_18_01/20050818013745938.htm" title="The Overview of ECMA C# Language Specialisation (1)" target="_blank">The Overview of</a>…</li> </ul> <div class="ad_site_index_right_2"> <script type="text/javascript"><!-- google_ad_client = "pub-1022332794981269"; google_ad_width = 180; google_ad_height = 60; google_ad_format = "180x60_as_rimg"; google_cpa_choice = "CAAQ0cX8zwEaCEpGRZ0Cl8yyKPu_93M"; google_ad_channel = "6903800490"; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script> </div> </div> </div> <div class="clear-both">&nbsp;</div> <div id="ContentBottom" class="darkBox"> <p>本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)</p><a href="#"><img src="../../images/btn_top.gif" width="51" height="11" alt="go top"/></a> </div> </div> </div> <script type="text/javascript" language="javascript" src="../../comm/doc/w3c_99_bottom.js"></script> <script type="text/javascript" language="javascript" src="../../comm/clicksum_doc.asp?typeid=41&infoid=375169"></script> <script type="text/javascript" language="JavaScript" src="../../comm/statistics.js"></script> </body> </html>