2012-06-05 110 views
1

我很难得到一个gridview的按钮列做任何事情。我正在使用DirectoryInfo对象来获取文件的详细信息。我把文件名和日期创建到gridview列中。第三列是一个按钮列。我已经设置了datakeys(Name,CreationTime),将按钮列的commandName命名为“sendcommand”。我想将文件名发送到另一页。我有RowCommand事件的代码:asp.net c#gridview按钮

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("[email protected]=" + 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> 
+0

我们是否还可以看到gridview本身(生成它的aspx或c#)的设置? –

+0

你没有说你将CommandArgument属性设置为任何东西?此外,gridview订阅了这个事件,并且这个debuger是否在事件内部触发了断点? –

+2

你错过了GridView的'OnRowCommand'属性,所以gvFiles_RowCommand永远不会被调用。 –

回答

3

你必须在OnRowCommand属性添加到在ASPX你的GridView,否则在GridView不知道什么时候,你就可以执行一个命令来调用什么方法。这是一个完全可选的属性,不是通过设计器生成的,所以当你想使用它时你必须手动添加它。