简体   繁体   English

asp.net c#gridview按钮

[英]asp.net c# gridview buttons

I'm having trouble getting a gridview's button column to do anything. 我无法获得gridview的按钮列来执行任何操作。 I'm using a DirectoryInfo object to get the details of a file. 我正在使用DirectoryInfo对象来获取文件的详细信息。 I put the filename and the date created into the gridview columns. 我将文件名和创建日期放在gridview列中。 The third column is a button column. 第三列是按钮列。 I have set the datakeys(Name, CreationTime), named the button column's commandName to "sendcommand". 我已将datakeys(Name,CreationTime)设置为“sendcommand”,将按钮列的commandName命名为。 I want to send the filename to another page. 我想将文件名发送到另一个页面。 I have this code for the RowCommand event: 我有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); 
    }
}

Nothing happens, except for a postback I think. 没有什么事情发生,除了我认为的回发。 How do I do this? 我该怎么做呢?

<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>

You have to add the OnRowCommand attribute to your GridView in the ASPX, otherwise the GridView doesn't know what method to call when you execute a command on it. 您必须将OnRowCommand属性添加到ASPX中的GridView,否则GridView在您对其执行命令时不知道要调用的方法。

AFAIK this is an entirely optional attribute and isn't generated via the designer so you have to add it manually when you want to use it. AFAIK这是一个完全可选的属性,不是通过设计器生成的,因此您必须在需要时手动添加它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM