Accessing the values of all controls in the DataGrid in ASP.net
Source: Internet
Author: User
asp.net|datagrid| Access | Control to access the values of all controls in the DataGrid in ASP.net, you can traverse each control in the DataGrid: The following is the ASPX code and the scripting code "vb.net" that implements this functionality:
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 the code generated by the Web Forms Designer
' This call is required by the Web Forms Designer.
<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: This method call is required by the Web Forms Designer
' Do not modify it using the Code Editor.
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:" + age.tostring () + "]"
result = ""
If isgraduate Then
result = "Already graduated,"
Else
result = "No graduation,"
End If
Result + + "skill:" + Skills + ","
Result + + "experience:" + Experience + ", and"
result = = "Degree:" + 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
' The user code to place the initialization page here
If not Page.IsPostBack Then
Dim data as ArrayList = New ArrayList ()
Data. ADD (New person ("Net_lover", True)
Data. ADD (New person ("Mencius e chapter", True)
Data. ADD (New person ("Wonderful World", False)
Data. ADD (New person ("XML development", 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 # example code:
<%@ 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));
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 = Name;
_age = age;
_isgraduate = isgraduate;
}
Public String name{
Get{return _name;}
}
public int age{
Get{return _age;}
}
public bool isgraduate{
Get{return _isgraduate;}
}
}
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