Gridview ImageButton OnCommand事件未被触发

时间:2013-08-06 15:16:48

标签: c# asp.net .net datagridview imagebutton

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#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); 
  } 
}

我也尝试过OnRowCommand事件,但它也没有被解雇。

1 个答案:

答案 0 :(得分: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