asp.net Gridview:访问每行数据的按钮

时间:2009-10-08 14:07:50

标签: c# asp.net gridview button

我正在尝试编写一个Gridview,每个行上都有一个按钮,单击该按钮时会显示要使用的特定行数据,但我不确定数据是如何传递的。

Gridview:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID"
    DataSourceID="SqlDataSource2">
    <Columns>
        <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True"
            SortExpression="ID" Visible="False" />
        <asp:BoundField DataField="RelationID" HeaderText="RelationID" InsertVisible="False"
            SortExpression="RelationID" Visible="False" />
        <asp:BoundField DataField="UserRole" HeaderText="UserRole" InsertVisible="False"
            SortExpression="UserRole" Visible="False" />
        <asp:BoundField DataField="FirstName" HeaderText="First Name" SortExpression="FirstName" />
        <asp:BoundField DataField="Surname" HeaderText="Surname" SortExpression="Surname" />
        <asp:BoundField DataField="Telephone" HeaderText="Telephone" SortExpression="Telephone" />
        <asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
        <asp:BoundField DataField="Hash" HeaderText="Hash" InsertVisible="False" SortExpression="Hash"
            Visible="False" />
        <asp:BoundField DataField="DateCreated" HeaderText="Date Invited" SortExpression="DateCreated" />
        <asp:TemplateField HeaderText="Resend Welcome Email">
            <ItemTemplate>
            <asp:Button runat="server" ID="btnResend" Text="Resend" OnClick="btnResend_Click" />
            </ItemTemplate>
            <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
        </asp:TemplateField>
    </Columns>
</asp:GridView>

button_OnClick

protected void btnResend_Click(object sender, EventArgs e)
{
    bool boolEmailSent = Email.sendWelcomeEmail(//Email from Row, //FirstName from Row, //Surname from Row, //Hash from Row);

    if (boolEmailSent == true)
    {
        //Confirm to User
    }
    else
    {
        //TODO: write error to log
    }
}

1 个答案:

答案 0 :(得分:1)

这篇文章比我们在这里回答的更深入地介绍了你所尝试的内容:

http://authors.aspalliance.com/aspxtreme/webforms/controls/addingbuttonfieldstoGridView.aspx

这是另一个:

http://msdn.microsoft.com/en-us/library/bb907626.aspx