OnItemCommand未加载

时间:2014-01-24 10:51:20

标签: c# asp.net datalist

这是我的代码: -

 <asp:DataList runat="server" Width="340px" BorderStyle="None" BorderWidth="2px" RepeatColumns="4" RepeatDirection="Horizontal" ID="dlImages" CellPadding="3" CellSpacing="2"
     OnItemCommand="dlImages_ItemCommand" >
        <ItemTemplate>
            <div style="float:left;">
                <asp:ImageButton ID="Image1" CommandName="imgClick" runat="server" CssClass="imgSampleImage" ImageUrl='<%# Container.DataItem %>'  />
            </div>
        </ItemTemplate>
    </asp:DataList>

protected void dlImages_ItemCommand(object source, DataListCommandEventArgs e)
{
    if (e.CommandName == "imgClick")
    {
        //string imgURL = ((Label)e.Item.FindControl("lblImgName")).Text;
        //Response.Redirect("dailydealsdetail.aspx?imgurl=" + imgURL);
    }
}

在我的页面加载中也放了: -

if (!IsPostBack)
{
  dlImages.DataSource = images;

  dlImages.DataBind();
}

我尝试将断点放在dlImages_ItemCommand中,但没有运气。请帮忙。

1 个答案:

答案 0 :(得分:0)

你是否尝试过这样做,在我的情况下工作正常:

<asp:DataList ID="list" runat="server" OnItemCommand="list_ItemCommand" RepeatColumns="7"
                    ShowFooter="true" ShowHeader="true"  Width="98%" OnSelectedIndexChanged="list_SelectedIndexChanged">
                <ItemTemplate>
                    <div align="center">
                        <asp:Image ID="img" runat="server" BorderColor="white" BorderWidth="2" Height="100"
                            ImageUrl='<%# "~/Image.aspx?id="+Eval("id") %>' Width="100px" />
                      <asp:HiddenField ID="hf1" runat="server" Value='<%# Eval("id") %>' />
                        <br />
                        <span onclick="return confirm('Are you sure to Remove this Image?')">
                            <asp:LinkButton ID="linkremove" runat="server" CommandName="Removes" Text="Remove"></asp:LinkButton>
                        </span>
                    </div>
                </ItemTemplate>

                </asp:DataList>

protected void list_ItemCommand(object source, DataListCommandEventArgs e)
{
    try
    {
        if (e.CommandName == "Removes")
        {
            HiddenField id1 = (HiddenField)list.Items[e.Item.ItemIndex].FindControl("hf1");
            if (dbo.deleterecords("image", "id='" + id1.Value + "'") == true)
            {

               BindItemsList();
                lbldel.Text = "Image Deleted";
            }
        }
    }
    catch (Exception ex)
    {
        Exception E = ex;
    }
}