2012-08-08 68 views

回答

2
+0

谢谢!我会研究这一点。 – user1512593 2012-08-08 23:19:31

+0

那么FindControl实际上是否可以找到gridview中每个单元格的单个值? – user1512593 2012-08-08 23:22:28

+0

您可以在行或单元上使用它。例如'row.FindControl(“controlID”)'或'row.Cells [0] .FindControl(“coontrolID”)'。获取控件'TextBox txtName =(TextBox)row.FindContro(“txtName”)' – codingbiz 2012-08-08 23:27:19

0

正如@codingbiz说:你可以使用FindControlTemplateField

的例子:

EditGridView.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="EditGridView.aspx.cs" Inherits="Q11874496WebApp.EditGridView" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"> 
      <Columns> 
       <asp:CommandField ShowSelectButton="True" /> 
       <asp:TemplateField HeaderText="Id" SortExpression="Id"> 
        <ItemTemplate> 
         <asp:Label ID="LblId" runat="server" Text='<%# Bind("Id") %>'></asp:Label> 
        </ItemTemplate> 
       </asp:TemplateField> 
       <asp:TemplateField HeaderText="Name" SortExpression="Name"> 
        <ItemTemplate> 
         <asp:Label ID="Lblname" runat="server" Text='<%# Bind("Name") %>'></asp:Label> 
        </ItemTemplate> 
       </asp:TemplateField> 
       <asp:TemplateField HeaderText="Phone" SortExpression="Phone"> 
        <ItemTemplate> 
         <asp:Label ID="LblPhone" runat="server" Text='<%# Bind("Phone") %>'></asp:Label> 
        </ItemTemplate> 
       </asp:TemplateField> 
      </Columns> 
     </asp:GridView> 
    </div> 
    &nbsp;Id: 
    <asp:TextBox ID="TxtId" runat="server"></asp:TextBox> 
    &nbsp;Name: 
    <asp:TextBox ID="TxtName" runat="server"></asp:TextBox> 
    &nbsp;Phone: 
    <asp:TextBox ID="TxtPhone" runat="server"></asp:TextBox> 
    <asp:Button ID="BtnUpdate" runat="server" OnClick="BtnUpdate_Click" Text="Update" /> 
    </form> 
</body> 
</html> 

EditGridView.aspx.cs:

public partial class EditGridView : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!this.IsPostBack) 
     { 
      if (this.MyDataList.Count == 0) 
      { 
       this.populateData(); 
      } 
      this.GridView1.DataSource = this.MyDataList; 
      this.GridView1.DataBind(); 
     } 
    } 

    /// <summary> 
    /// Data for the GridView. Encapsulates a list that will be in session. 
    /// </summary> 
    public IList<MyDataPoco> MyDataList 
    { 
     get 
     { 
      if (this.Session["MyDataList"] == null) 
       this.Session["MyDataList"] = new List<MyDataPoco>(); 
      return (IList<MyDataPoco>)this.Session["MyDataList"]; 
     } 
    } 

    /// <summary> 
    /// Creates a list of 10 items. 
    /// </summary> 
    private void populateData() 
    { 
     for (int i = 0; i < 10; i++) 
     { 
      this.MyDataList.Add(
       new MyDataPoco() 
       { 
        Id = i.ToString(), 
        Name = "Name " + i, 
        Phone = i + "" + i + "" + i + "." + i + "" + i + "" + i + "" + i + "" 
       }); 
     } 
    } 

    /// <summary> 
    /// Here is the way you can get values from GridView. 
    /// </summary> 
    /// <param name="sender"></param> 
    /// <param name="e"></param> 
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     GridViewRow row = this.GridView1.SelectedRow; 
     Label LblId = (Label)row.FindControl("LblId"); 
     Label LblName = (Label)row.FindControl("LblName"); 
     Label LblPhone = (Label)row.FindControl("LblPhone"); 

     this.TxtId.Text = LblId.Text; 
     this.TxtName.Text = LblName.Text; 
     this.TxtPhone.Text = LblPhone.Text; 
    } 

    /// <summary> 
    /// Updates the data in the GridView. 
    /// </summary> 
    /// <param name="sender"></param> 
    /// <param name="e"></param> 
    protected void BtnUpdate_Click(object sender, EventArgs e) 
    { 
     if (this.GridView1.SelectedIndex < 0) 
     { 
      this.ClientScript.RegisterStartupScript(
       this.GetType(), 
       "alert", 
       "alert('Select before...');", true); 
     } 
     else 
     { 
      MyDataPoco myDateItem = this.MyDataList[this.GridView1.SelectedIndex]; 
      myDateItem.Id = this.TxtId.Text; 
      myDateItem.Name = this.TxtName.Text; 
      myDateItem.Phone = this.TxtPhone.Text; 

      this.GridView1.DataSource = this.MyDataList; 
      this.GridView1.DataBind(); 
     } 
    } 
} 

MyDataPoco.cs:

public class MyDataPoco 
{ 
    public String Id { get; set; } 
    public String Name { get; set; } 
    public String Phone { get; set; } 
} 

完整源:Q11874496WebApp.7z