【问题标题】:asp.net c# gridview buttonsasp.net c# gridview 按钮
【发布时间】:2012-06-05 21:07:18
【问题描述】:

我无法让 gridview 的按钮列执行任何操作。我正在使用 DirectoryInfo 对象来获取文件的详细信息。我将文件名和创建日期放入 gridview 列中。第三列是按钮列。我已设置 datakeys(Name, CreationTime),将按钮列的 commandName 命名为“sendcommand”。我想将文件名发送到另一个页面。我有 RowCommand 事件的代码:

protected void gvFiles_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
{

    if (e.CommandName == "sendcommand")
    {

        int index = Convert.ToInt32(e.CommandArgument);

        string fileID = ((GridView)sender).DataKeys[index]["Name"].ToString();
        Response.Redirect("irMain.aspx?@filename=" + fileID); 
    }
}

什么都没有发生,除了我认为的回发。我该怎么做呢?

<asp:GridView ID="gvFiles" runat="server" Font-Name="Verdana" Font-Names="Verdana" 
        Width="401px" AutoGenerateColumns="False" BackColor="White" 
        BorderColor="Black" BorderStyle="Ridge" BorderWidth="2px" 
        DataKeyNames="Name,CreationTime" 
        >
        <Columns>
            <asp:HyperLinkField AccessibleHeaderText="File Name" 
                DataNavigateUrlFields="Name" DataNavigateUrlFormatString="~\Assets\reports\{0}" 
                DataTextField="Name" HeaderText="File Name" >

                <HeaderStyle BackColor="#0033CC" ForeColor="White" />
            </asp:HyperLinkField>
            <asp:BoundField AccessibleHeaderText="Date" DataField="CreationTime" 
                DataFormatString="{0:d}" HeaderText="Date">
                <HeaderStyle BackColor="Blue" ForeColor="White" />
            </asp:BoundField>

            <asp:ButtonField ButtonType="Button" Text="DO Stuff" CommandName="sendcommand" 
                HeaderText="WHAT?!" />

        </Columns>

        <AlternatingRowStyle BackColor="#6699FF" />

    </asp:GridView>

【问题讨论】:

  • 我们还能看到gridview本身的设置(生成它的aspx或c#)吗?
  • 您没有说您将 CommandArgument 属性设置为任何值?此外,gridview 是否订阅了此事件,调试器是否在事件中命中断点?
  • 您缺少 GridView 的 OnRowCommand 属性,因此永远不会调用 gvFiles_RowCommand。
  • 介意给我打个例子吗?

标签: c# asp.net button gridview


【解决方案1】:

你必须在ASPX中给你的GridView添加OnRowCommand属性,否则当你在它上面执行命令时,GridView不知道调用什么方法。

AFAIK 这是一个完全可选的属性,不是通过设计器生成的,因此您必须在需要时手动添加它。

【讨论】:

    猜你喜欢
    • 2012-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-03
    • 2011-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多