0

Gridview imagebutton onclick and OnCommand events are not being fired.

<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

here is C# Code

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 .

Mubashir Ali
  • 559
  • 2
  • 8
  • 23
  • If you have OnRowCommand then it should be triggered. Whats the behavior you are seeing? – Jafar Kofahi Aug 06 '13 at 15:20
  • protected void grdResult_RowCommand(object sender, GridViewCommandEventArgs e) { if(e.CommandName == "ABC") { Response.Redirect("~/video.aspx", false); } } this OnRowCommand function which is not working too – Mubashir Ali Aug 06 '13 at 15:25
  • Put a breakpoint at the `if` statement in RowCommand method in code-behind, and check the value of `e.CommandName`. – Garrison Neely Aug 06 '13 at 15:27
  • I have already tried that this event is not being fried i kept the break point at the starting of this function and on the if condition this function is not being called. – Mubashir Ali Aug 06 '13 at 15:29
  • Could you edit your post with the full ASPX markup of your page (if it's not too long)? May be that something's not hooked to the code-behind correctly. – Garrison Neely Aug 06 '13 at 15:36
  • Could you make sure `AutoEventWireup="true"` in the page like this - `<%@ Page Language="C#" AutoEventWireup="true" ..." %>`. If still not working, as Garrison suggested you need to upload the entire ASPX page. – Win Aug 06 '13 at 15:42
  • @win yes it is set to {AutoEventWireup="true"} – Mubashir Ali Aug 06 '13 at 15:45
  • Could you remove object datasource and test it with this - `protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { grdResult.DataSource = new List – Win Aug 06 '13 at 15:49

1 Answers1

0

If you are trying to do something (i.e. Response.Redirect("~/video.aspx", false); when the image button is clicked you can do something like this:

1- First add an OnClick event for the image button control (e.g. DoSomething)

2- Then your DoSomething event handler would be something like this (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
Jafar Kofahi
  • 763
  • 6
  • 22
  • I don't know how and why the `grdResult_RowCommand`function was fired when I added the `OnClientClick="imgClick('<%= 'VideoName' %>')"` in `asp:ImageButton` tag – Mubashir Ali Aug 06 '13 at 15:58