从选定的行gridview中检索值

时间:2012-08-08 23:05:10

标签: c# asp.net gridview textbox

我有一些数据绑定到gridview,并想知道是否可以检索所选行中的值并将这些值插入到一系列文本框和标签中。

2 个答案:

答案 0 :(得分:2)

答案 1 :(得分: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