简体   繁体   English

Gridview ImageButton OnCommand事件未触发

[英]Gridview ImageButton OnCommand event is not being fired

Gridview imagebutton onclick and OnCommand events are not being fired. 不会触发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>

This is the complete markup code I am using ObjectDataSource because of Data source paging/ true paging 这是我使用ObjectDataSource的完整标记代码,因为数据源分页/真正的分页

here is C# Code 这是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); 
  } 
}

I have also tried OnRowCommand event but it is also not being fired . 我也尝试过OnRowCommand事件,但也没有被触发。

If you are trying to do something (ie Response.Redirect("~/video.aspx", false); when the image button is clicked you can do something like this: 如果您想做某事 (即Response.Redirect("~/video.aspx", false);单击图像按钮时,您可以执行以下操作:

1- First add an OnClick event for the image button control (eg DoSomething ) 1-首先为图像按钮控件添加一个OnClick事件(例如DoSomething

2- Then your DoSomething event handler would be something like this ( VB.Net ) 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

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

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