【问题标题】:Adding ImageButton Programmatically以编程方式添加 ImageButton
【发布时间】:2010-01-18 17:10:46
【问题描述】:

我有一个如下所示的代码 sn-p,我想在页面加载期间将图像按钮添加到我的 asp:Panel 中。但是当我运行页面时,事件已经触发了。我希望它在被点击时被触发。

提前感谢所有帮助

    protected void Page_Load(object sender, EventArgs e)
    {...

        foreach (Gift g in bonusGifts)
        {
            ImageButton ib = new ImageButton();
            ib.ImageUrl = g.GiftBanner;
            ib.ID = g.GiftID.ToString();
            ib.Click += Purchase(g);
            BonusGiftPanel.Controls.Add(ib);

        }
    }

    private ImageClickEventHandler Purchase(Gift g)
    {
        _giftRep.Purchase(g, _userSession.CurrentUser);
        lblGifts.Text = "You have purcased " + g.GiftName + " for " + g.BonusPoints;

        return null;
    }

【问题讨论】:

    标签: asp.net controls


    【解决方案1】:

    在您的 Page_Init 中添加控件,而不是在您的 Page_Load 中。 [1]

    此外,您没有按照应有的方式执行此操作。考虑这段代码

    //your collection of objects goes here. It might be something different than 
    //this, but basically a Dictionary<int, YourType> goes fine
    public Dictionary<Int32, string> Ids
    {
        get { return (ViewState["ids"] ?? new Dictionary<Int32, string>()) as Dictionary<Int32, string>; }
        set { ViewState["ids"] = new Dictionary<Int32, string>(); }
    }
    
    protected void Page_Init(object sender, EventArgs e)
    {
        //load the data using your DAO
        Ids = new Dictionary<int, string>();
    
        Ids.Add(1, "http://www.huddletogether.com/projects/lightbox2/images/image-2.jpg");
        Ids.Add(2, "http://helios.gsfc.nasa.gov/image_euv_press.jpg");
    
        foreach (var item in Ids)
        {
            ImageButton imb = new ImageButton()
            {
                ImageUrl = item.Value,
                CommandArgument = item.Key.ToString(),
                CommandName = "open"
            };
    
            imb.Click += new ImageClickEventHandler(imb_Click);
    
            PH1.Controls.Add(imb);
        }
    }
    
    void imb_Click(object sender, ImageClickEventArgs e)
    {
        Response.Write("You purchased " + Ids[int.Parse(((ImageButton)sender).CommandArgument)]);
    }
    

    [1](上周我回答的其他问题中的 CTRL+C/CTRL+V):

    在页面周期之间必须维护的所有内容都应在 Page_Init 中声明,而不是在 Page_Load 中声明。

    所有初始化,如添加事件处理程序和添加控件都应该在初始化期间添加,因为状态会在页面周期之间保存。控件内容和视图状态的处理,应在Load 中完成。

    还要检查http://msdn.microsoft.com/en-us/library/ms178472.aspx

    初始化

    在所有控件已初始化且任何皮肤后引发 已应用设置。用这个 读取或初始化控件的事件 属性。

    .

    加载

    Page 调用 OnLoad 事件方法 在页面上,然后递归地执行 每个子控件都相同,其中 对每个孩子做同样的事情 控制直到页面和所有 控件已加载。

    使用 OnLoad 事件方法设置 控件中的属性并建立 数据库连接。

    【讨论】:

    • 当我从 Page_Load 更改为 Page_Init 时也会发生同样的事情。这行 ib.Click += Purchase(g); 有什么问题吗?因为它会在我点击图片按钮之前触发点击事件!
    【解决方案2】:

    正如其他人所说,您应该在 Page Init 事件中添加控件。

    您的图像单击事件处理程序不符合 ImageButton Click 事件处理程序签名。应该是这样的:

    private void ImageButton_Click(ByVal sender As Object, ByVal e As ImageClickEventArgs)
    {
    
    }
    

    请注意,您不能将“礼物”对象直接传递给 ImageButton_Click。你必须找到另一种方法来做到这一点。

    【讨论】:

      【解决方案3】:

      您需要查看页面生命周期 - http://msdn.microsoft.com/en-us/library/ms178472.aspx

      【讨论】:

        猜你喜欢
        • 2013-12-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-08
        • 2013-03-31
        • 1970-01-01
        相关资源
        最近更新 更多