我有一个母版页,里面有一个带有图像按钮的转发器。但是ItemCommand事件不会在母版页中使用转发器控件触发。请帮帮我。
这是我的代码:
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>
<asp:repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_I">
<ItemTemplate>
<div class="product">
<a title="Details" href="#"> <img id="Img1" alt="" src='<%# Eval("image") %>' runat="server" class="img-zoom" /> </a>
<p><%# Eval("Pname") %></p>
<br />
<asp:ImageButton ID="ImageButton1" runat="server" class="buttonclass" ImageUrl="~/css/images/img1.png" visible="true" CommandName="ADD" CommandArgument='<%# Eval("Pid") %>' />
</div>
</ItemTemplate>
</asp:repeater>
代码隐藏:
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
return;
}
else
{
Repeater1.DataSource = adsource;
Repeater1.DataBind();
}
}
protected void Repeater1_I(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "ADD")
{
//Do Something
}
}
答案 0 :(得分:0)
不要使用Repeater.ItemCommand
,而是尝试使用ImageButton.Click
事件。
<asp:ImageButton ID="ImageButton1" runat="server" class="buttonclass"
ImageUrl="~/css/images/img1.png" visible="true"
CommandName="ADD" CommandArgument='<%# Eval("Pid") %>'
OnClick="ImageButton1_Click" />
和代码:
protected void ImageButton1_Click(object sender, EventArgs e)
{
ImageButton b = sender as ImageButton;
var arg = b.CommandArgument;
}