Asp. NET to access all control values in the DataGrid-Practical Tips

Source: Internet
Author: User

The example in this article tells you how to access all the control values in the DataGrid in ASP.net. Share to everyone for your reference, specific as follows:

<%@ Page language= "C #"%> <%@ import namespace= system.collections "%>" <script runat= "Server" > void Pag E_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 the 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 = Nam
E
_age = age;
_isgraduate = isgraduate; Public String name{Get{return _name} is public int age{Get{return _age;} public bool isgraduate{Get{return _isgradu
ate;} }} &LT;/SCRipt> 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.