要在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>";
}
resultfield.text = result;
}
class person{
string _name;
int _age;
bool _isgraduate;
public person(string name,int age, bool isgraduate){
_name = name;
_age = age;
_isgraduate = isgraduate;
}
public string name{
get{return _name;}
}
public int age{
get{return _age;}
}
public bool isgraduate{
get{return _isgraduate;}
}
}
</script>
<html>
<head>
</head>
<body>
<form runat="server">
<asp:datagrid id="mydatagrid" runat="server" 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=<%# (bool)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" selected="true">less then 1 year</asp:listitem>
<asp:listitem value="3">less then 3 year</asp:listitem>
<asp:listitem value="5">less then 5 year</asp:listitem>
<asp:listitem value="10">less then 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>
