在ASP.NET中访问DataGrid中所有控件的值
来源:互联网 发布:硬件温度检测软件 编辑:程序博客网 时间:2024/05/17 09:01
要在ASP.NET中访问DataGrid中所有控件的值,可以遍历DataGrid中每个控件
实例1(来至孟子E章):
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="DataGridAccessValues.aspx.vb" Inherits="aspxWeb.DataGridAccessValues"%>后端代码:
Imports System.CollectionsPublic 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 窗体设计器所必需的。 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 += "
" 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 SubEnd ClassPublic 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 PropertyEnd ClassC#例子代码: <%@ Page Language="C#" %><%@ import Namespace="System.Collections" %>实例2(来至ASP.NET快速入门):<html> <script language="C#" runat="server"> DataView CartView; Double runningTotal = 0; //Cart 是 Page 上的一个属性 DataTable Cart { get { if (Session["DGC_ShoppingCart"] == null) { DataTable tmpCart = new DataTable(); tmpCart.Columns.Add(new DataColumn("数量", typeof(string))); tmpCart.Columns.Add(new DataColumn("产品", typeof(string))); tmpCart.Columns.Add(new DataColumn("价格", typeof(Double))); tmpCart.Columns.Add(new DataColumn("礼品包", typeof(bool))); Session["DGC_ShoppingCart"] = tmpCart; // 第一次加载 -- 预填充一些数据 for (int i=1; i<=6; i++) { DataRow dr = tmpCart.NewRow(); dr[0] = "1"; dr[1] = "产品" + i.ToString(); dr[2] = 1.23 * (i+1); dr[3] = false; tmpCart.Rows.Add(dr); } return tmpCart; } else return (DataTable)Session["DGC_ShoppingCart"]; } } void Page_Init(Object sender, EventArgs e) { MyDataGrid.EnableViewState=true; } void Page_Load(Object sender, EventArgs e) { CartView = Cart.DefaultView; if (!IsPostBack) BindGrid(); } void BindGrid() { MyDataGrid.DataSource = CartView; MyDataGrid.DataBind(); } void btnUpdate_click(Object sender, EventArgs e) { for (int i=0; i// 对于数据库,我们应使用一条更新命令。 // 因为这是一个内存内数据表,所以我们只更改内存内的行。 DataRow dr = Cart.Rows[i]; dr[0] = qtyTextBox.Text; dr[3] = giftCheckBox.Checked; } BindGrid(); } Double CalcTotal (int count, Double price) { Double total = count * price; runningTotal += total; return(total); } < FONT>script><body> <h3><font face="宋体">DataGrid 的自定义编辑< FONT>font>< FONT>h3> <form runat=server> <Columns> <asp:TemplateColumn HeaderText="数量"> <ItemTemplate> '> Width="40px" /> < FONT>ItemTemplate> < FONT>asp:TemplateColumn> <asp:BoundColumn HeaderText="产品" DataField="产品"/> <asp:BoundColumn HeaderText="价格" DataField="价格" DataFormatString="{0:c}" /> <asp:TemplateColumn HeaderText="礼品包?"> <ItemTemplate> <center> '> /> < FONT>center> < FONT>ItemTemplate> < FONT>asp:TemplateColumn> <asp:TemplateColumn HeaderText="小计"> <ItemTemplate> <p align="right"> '> /> < FONT>p> < FONT>ItemTemplate> <FooterTemplate> <p align="right"><b> '> /> < FONT>b>< FONT>p> < FONT>FooterTemplate> < FONT>asp:TemplateColumn> < FONT>Columns> < FONT>asp:DataGrid> < FONT>form>< FONT>body>< FONT>html>