views:

269

answers:

3

Hi,

I have a code snippet like below and I want to add imagebuttons into my asp:Panel during page load. But the events are firing already when I run the page. I want it to be fired when it is clicked.

Thanks in advance for all helps

    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;
    }
A: 

Add controls in your Page_Init, not in your Page_Load. [1]

Furthermore, you are not doing this the way it should. Consider this code

//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 from some other question I answered last week):

Everything that has to be maintained between page cycles should be declared in Page_Init, not Page_Load.

All the initialization, like adding event handlers, and adding controls should be added during initialization, as the state is saved between page cycles. Handling with the content of controls and the viewstate, should be done in Load.

Check also http://msdn.microsoft.com/en-us/library/ms178472.aspx.

Init

Raised after all controls have been initialized and any skin settings have been applied. Use this event to read or initialize control properties.

.

Load

The Page calls the OnLoad event method on the Page, then recursively does the same for each child control, which does the same for each of its child controls until the page and all controls are loaded.

Use the OnLoad event method to set properties in controls and establish database connections.

Jan Jongboom
well the same thing happens when I changed from Page_Load to Page_Init.Is there anything wrong with this lineib.Click += Purchase(g);Because it fires the click event before I click on image buttons !
Kubi
check my edit..
Jan Jongboom
A: 

You need to take a look at the Page Lifecycle - http://msdn.microsoft.com/en-us/library/ms178472.aspx

Moo
A: 

You should add the controls in the Page Init event, as other have said.

Your image click event handler does not conform to the ImageButton Click event handler signature. That should look something like this:

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

}

Note that you can't pass your "Gift" object directly to the ImageButton_Click. You will have to find another method of doing that.