1

I'm having trouble getting a gridview's button column to do anything. I'm using a DirectoryInfo object to get the details of a file. I put the filename and the date created into the gridview columns. The third column is a button column. I have set the datakeys(Name, CreationTime), named the button column's commandName to "sendcommand". I want to send the filename to another page. I have this code for the RowCommand event:

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>
6
  • Can we also see the setup of the gridview itself (the aspx or c# that generates it)? Commented Jun 5, 2012 at 21:09
  • you didn't say you were setting the CommandArgument property to anything? also, is gridview subscribed for this event, and does the debuger hit the breakpoint inside the event? Commented Jun 5, 2012 at 21:12
  • 2
    You're missing the OnRowCommand attribute for your GridView so gvFiles_RowCommand is never getting called. Commented Jun 5, 2012 at 21:13
  • Mind typing an example for me? Commented Jun 5, 2012 at 21:18
  • 1
    msdn.microsoft.com/en-us/library/… Commented Jun 5, 2012 at 21:19

1 Answer 1

3

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.

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.