在ASP.NET中访问DataGrid中所有控件的值(2)
点击次数:23 次 发布日期:2008-11-26 14:32:48 作者:源代码网
|
源代码网推荐 <%@ Page Language="<a href="http://dev.21tx.com/dotnet/csharp/" target="_blank">C#</a>" %> 源代码网推荐 <%@ 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> 源代码网推荐 源代码网推荐 源代码网推荐 做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。 源代码网推荐 源代码网供稿. |
