0

Gridview imagebutton onclick 和 OnCommand 事件不会被触发。

<form id="form1" runat="server">

        <div class="heading">
            <div class="Search">
                <customContorls:Header ID="Header1" runat="server" />
            </div>
            <div class="MenuItems">
                <customContorls:MenuItems ID="MenuItems1" runat="server" />
            </div>
        </div>

        <div class="content-wrapper">
            <div style="margin-left: 50px">
                <asp:GridView ID="grdResult" runat="server" OnRowCommand="grdResult_RowCommand" AllowPaging="true" EmptyDataText="No Video Found"
                    AlternatingRowStyle-HorizontalAlign="Center" GridLines="None" DataSourceID="objSource"
                    AutoGenerateColumns="False">
                    <Columns>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:ImageButton ImageUrl='<%# Eval("Thumbnail") %>' runat="server" Width="200" Height="150"
                                ID="imgThumbnail" CommandName="ABC" CommandArgument="123"/>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:BoundField DataField="VideoName" SortExpression="VideoName"></asp:BoundField>
                    </Columns>
                </asp:GridView>

                <asp:ObjectDataSource ID="objSource" runat="server"
                    TypeName="PagingSource" SortParameterName="sortExpression"
                    OldValuesParameterFormatString="original_{0}" EnablePaging="True"
                    SelectMethod="GetVideoDataBy" SelectCountMethod="TotalNumberOfRecords"></asp:ObjectDataSource>
            </div>
        </div>
    </form>

这是我使用 ObjectDataSource 的完整标记代码,因为数据源分页/真实分页

这是C#代码

protected void Page_Load(object sender, EventArgs e)
    {
        Title = "Search Result";
        string SortExp = "";
        if (!IsPostBack)
        {
            if (Page.RouteData.Values["videoname"] != null)
                SortExp = Page.RouteData.Values["videoname"].ToString();

            grdResult.Sort(SortExp, SortDirection.Ascending);
            grdResult.PageSize = 5;
        }
    }

protected void grdResult_RowCommand(object sender, GridViewCommandEventArgs e) 
{ 
  if(e.CommandName == "ABC") 
  { 
    Response.Redirect("~/video.aspx", false); 
  } 
}

我也尝试过 OnRowCommand 事件,但它也没有被触发。

4

1 回答 1

0

如果您正在尝试做某事(即Response.Redirect("~/video.aspx", false);单击图像按钮时,您可以执行以下操作:

1-首先OnClick为图像按钮控件添加一个事件(例如DoSomething

2-然后您的DoSomething事件处理程序将是这样的(VB.Net

  Protected Sub DoSomething(ByVal sender As Object, ByVal e As EventArgs)
     Dim row As GridViewRow = sender.NamingContainer --get the row that triggered it
     ...then do something
  End Sub
于 2013-08-06T15:32:14.390 回答