ASP.NET内容在GridView中的onClick上消失

时间:2013-04-02 17:02:50

标签: asp.net button gridview

我在TemplateField中有一个带有Button的GridView。当我单击TemplateField生成的其中一个按钮时,将显示母版页内容,但不会显示我实际加载的页面中的内容。我甚至有一个OutputLabel(用于调试目的),它不在与GridView或包含GridView的div相关的任何内容之外,它也没有。

内容页面:

<%@ Page Title="Administration" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Admin.aspx.cs" Inherits="Default2" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="mainCon" Runat="Server">
    <div id="container" class="left-margin">
        <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="CustomGetAllUsers" TypeName="Helper"></asp:ObjectDataSource>
        <asp:GridView runat="server" ID="UserGrid" CssClass="UserList" AllowPaging="True" AllowSorting="True" AllowCustomPaging="True" DataSourceID="ObjectDataSource1" CellPadding="4" ForeColor="#333333" GridLines="None" style="margin: auto">
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            <EditRowStyle BackColor="#999999" />
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" Height="50"/>
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#E9E7E2" />
            <SortedAscendingHeaderStyle BackColor="#506C8C" />
            <SortedDescendingCellStyle BackColor="#FFFDF8" />
            <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
            <Columns>
               <asp:TemplateField runat="server">
                   <ItemTemplate>
                       <asp:Button ID="EditButton" runat="server" Text="Rediger" BorderColor="LightBlue" BackColor="LightGray" BorderWidth="3px" BorderStyle="Solid" OnClick="EditButton_Click"/>
                   </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </div>
    <asp:Label runat="server" ID="OutputLabel" />
</asp:Content>

点击时执行的代码:

protected void EditButton_Click(object sender, EventArgs e)
{
    Button btn = (Button)sender;
    GridViewRow gvr = (GridViewRow)btn.NamingContainer;
    HttpCookie cookie = new HttpCookie("Username", gvr.Cells[2].Text);
    Response.Cookies.Add(cookie);
    Response.Redirect("~/UserAdmin.aspx");
}

1 个答案:

答案 0 :(得分:0)

我现在已经找到了解决方案。我将TemplateField更改为具有CommandName =“Edit”的ButtonField,并为GridView创建了一个OnRowCommand。

整个问题是它没有在代码隐藏中调用适当的方法。现在它调用这个方法可以正常工作。

protected void UserList_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Edit")
    {
        int index = Convert.ToInt32(e.CommandArgument);
        String s = (String) UserGrid.DataKeys[index].Value;
        HttpCookie cookie = new HttpCookie("Username", s);
        Response.Cookies.Add(cookie);
        Response.Redirect("UserAdmin.aspx");
    }
}

感谢您的帮助!