[英]asp.net c# gridview buttons
我無法獲得gridview的按鈕列來執行任何操作。 我正在使用DirectoryInfo對象來獲取文件的詳細信息。 我將文件名和創建日期放在gridview列中。 第三列是按鈕列。 我已將datakeys(Name,CreationTime)設置為“sendcommand”,將按鈕列的commandName命名為。 我想將文件名發送到另一個頁面。 我有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>
您必須將OnRowCommand
屬性添加到ASPX中的GridView,否則GridView在您對其執行命令時不知道要調用的方法。
AFAIK這是一個完全可選的屬性,不是通過設計器生成的,因此您必須在需要時手動添加它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.