在asp.net中单击ImageButton是不行的

时间:2014-05-24 10:01:10

标签: c# asp.net postback imagebutton

OnClick ImageButton事件无法在asp.net中运行。这个问题已被问到here,但所有答案对我都不起作用。就我而言,唯一的区别是我的页面上没有UpdatePanel

我的HTML如下:

<ul>
    <asp:DataList ID="PhotoGalleryDataList" runat="server" RepeatColumns="2">
        <ItemTemplate>
            <li><a href="#">
            <div>
               <span>
                  <asp:Label ID="idlabel" runat="server" Text='<%#Eval("AdPhotoID") %>'></asp:Label>
                   <asp:ImageButton ID="AdGallaryImageButton" runat="server" ToolTip="View Photo" CssClass="AdPhotoGallery"
                        AlternateText="Photo" CausesValidation="false" ImageUrl='<%#string.Format("data:image/jpg;base64,{0}",Convert.ToBase64String((byte[])Eval("NormalImage"))) %>'
                        OnClick="AdGallaryImageButton_Click" CommandArgument='<%#Eval("AdPhotoID")%>' />
                </span>
            </div>
            </a></li>
         </ItemTemplate>
    </asp:DataList>
</ul>

在代码隐藏中,我这样绑定:

PhotoGalleryDataList.DataSource = dt1;
PhotoGalleryDataList.DataBind();

dt1我有NormalImageIsProfileImageAdPhotoID,其中NormalImagebyte arrayIsProfileImage是{ {1}}和ByteAdPhotoID

varchar(15)的{​​{1}}事件处理程序:

OnClick

1 个答案:

答案 0 :(得分:1)

DataListRepeater等数据绑定控件中,您应使用OnCommand事件来处理点击:

<asp:ImageButton ID="AdGallaryImageButton" runat="server" ToolTip="View Photo" CssClass="AdPhotoGallery"
        AlternateText="Photo" CausesValidation="false" ImageUrl='<%#string.Format("data:image/jpg;base64,{0}",Convert.ToBase64String((byte[])Eval("NormalImage"))) %>'
        OnCommand="AdGallaryImageButton_Click" CommandArgument='<%#Eval("AdPhotoID")%>' />

并更改事件处理程序的签名:

protected void AdGallaryImageButton_Click(object sender, CommandEventArgs e)
{
    // Here you can access AdPhotoID argument via e.CommandArgument property
}