1

我无法从代码隐藏中的 Gridview ItemTemplate 访问 asp.NET HiddenField。我需要能够读取这些隐藏字段包含的值,以便我可以执行删除方法。代码如下

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="MemberList.ascx.cs" Inherits="UserControls_MemberList" %>
<asp:RadioButtonList ID="ReportSelect" runat="server" RepeatDirection="Horizontal">
<asp:ListItem Value="1">All</asp:ListItem>
<asp:ListItem Value="2">Current Members</asp:ListItem>
<asp:ListItem Value="3">Perspective Members</asp:ListItem>
</asp:RadioButtonList>
<asp:Button ID="ReportSelectButton" runat="server" OnClick="ReportSelectButton_Click"
Text="Select Report Type" />
<asp:Button ID="LinkToHomePage" runat="server" Text="Back to Homepage" OnClick="LinkToHomePage_Click">
</asp:Button>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" Height="308px"
Width="1282px" onselectedindexchanged="GridView1_SelectedIndexChanged">
<Columns>
    <asp:BoundField HeaderText="First Name" AccessibleHeaderText="FirstName" DataField="FirstName">
    </asp:BoundField>
    <asp:BoundField HeaderText="Last Name" AccessibleHeaderText="LastName" DataField="LastName">
    </asp:BoundField>
    <asp:BoundField HeaderText="Street Address" AccessibleHeaderText="StreetAddress"
        DataField="StreetAddress"></asp:BoundField>
    <asp:BoundField HeaderText="City" AccessibleHeaderText="City" DataField="City"></asp:BoundField>
    <asp:BoundField HeaderText="State" AccessibleHeaderText="State" DataField="State">
    </asp:BoundField>
    <asp:BoundField HeaderText="Zip" AccessibleHeaderText="Zip" DataField="Zip"></asp:BoundField>
    <asp:BoundField HeaderText="Birthday" AccessibleHeaderText="Birthday" DataField="Birthday" />
    <asp:BoundField HeaderText="Email" AccessibleHeaderText="Email" DataField="Email">
    </asp:BoundField>
    <asp:BoundField HeaderText="PrimaryPhone" AccessibleHeaderText="PrimaryPhone" DataField="PrimaryPhone" />
    <asp:BoundField HeaderText="AlternatePhone" AccessibleHeaderText="AlternatePhone"
        DataField="AlternatePhone" />
    <asp:BoundField HeaderText="Pending" AccessibleHeaderText="Pending" DataField="Pending" />
    <asp:BoundField HeaderText="IsMember" AccessibleHeaderText="IsMember" DataField="IsMember" />
    <asp:BoundField HeaderText="Username" AccessibleHeaderText="Username" DataField="Username">
    </asp:BoundField>
    <asp:BoundField HeaderText="Description" AccessibleHeaderText="Descripton" DataField="Description" />
    <asp:TemplateField HeaderText="Edit" AccessibleHeaderText="Edit">
        <ItemTemplate>
            <asp:HyperLink ID="EditUsername" runat="server" NavigateUrl='<%# Link.ToMemberAdmin(Eval("Username").ToString())%>'
                Text="Edit" />
            <asp:Button ID="DeleteButton" runat="server" Text="Delete Entry" OnClick="DeleteButton_Click"/>
            <asp:HiddenField ID="HiddenUsername" Value='<%#Bind("Username") %>' runat="server" />
            <asp:HiddenField ID="HiddenEmail" Value='<%#Bind("Email") %>' runat="server" />
        </ItemTemplate>
    </asp:TemplateField>
</Columns>
</asp:GridView>

代码隐藏

protected void DeleteButton_Click(object sender, EventArgs e)
{
    HiddenField Username = GridView1.FindControl("HidderUsername") as HiddenField;
    HiddenField Email = GridView1.FindControl("HiddenEmail") as HiddenField;
    string username = Username.Value;
    string email = Email.Value;
    AdminAccess.DeleteMemberApplication(username, email);
}

任何帮助将不胜感激。

4

2 回答 2

0

HiddenField 控件将成为GridView 呈现<td>器之一的一部分。<tr>GridView 控件的 FindControl 只知道它的直接子级,并且由于 Hidden 控件低于它的两个级别,因此它不会找到它们。相反,从 开始sender并尝试找到它的兄弟 Hidden 控件。替换事件处理程序中的两个 HiddenControl 行,如下所示:

HiddenField Username = (HiddenField) ((Button)sender).Parent.Controls.FindControl("HiddenUsername");
HiddenField Email = (HiddenField) ((Button)sender).Parent.Controls.FindControl("HiddenEmail");

它应该获得您正在寻找的值以删除该行中的数据。

于 2012-08-14T02:43:34.567 回答
0

只需使用CommandArgument按钮

<asp:TemplateField HeaderText="Edit" AccessibleHeaderText="Edit">
    <ItemTemplate>
        <asp:HyperLink ID="EditUsername" runat="server" NavigateUrl='<%# Link.ToMemberAdmin(Eval("Username").ToString())%>'
            Text="Edit" />
        <asp:Button ID="DeleteButton" runat="server" Text="Delete Entry" OnClick="DeleteButton_Click"
            CommandArgument='<%#Eval("Username")+"^"+Eval("Email") %>'/>
    </ItemTemplate>
</asp:TemplateField>

protected void DeleteButton_Click(object sender, EventArgs e)
{
    var btn = sender as Button;
    var args = btn.CommandArgument.Split('^');
    string username = args[0];
    string email = args[1];
    AdminAccess.DeleteMemberApplication(username, email);
}
于 2012-08-14T02:55:06.180 回答