欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ASP.NET中访问DataGrid中所有控件值的方法

程序员文章站 2023-12-16 10:27:34
本文实例讲述了asp.net中访问datagrid中所有控件值的方法。分享给大家供大家参考,具体如下: <%@ page language="c#" %&g...

本文实例讲述了asp.net中访问datagrid中所有控件值的方法。分享给大家供大家参考,具体如下:

<%@ 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>

更多关于asp.net相关内容感兴趣的读者可查看本站专题:《asp.net文件操作技巧汇总》、《asp.net ajax技巧总结专题》及《asp.net缓存操作技巧总结》。

希望本文所述对大家asp.net程序设计有所帮助。

上一篇:

下一篇: