views:

31

answers:

1

in .ASPX this is working

<asp:ImageButton ID="lbHope6" runat="server" ImageUrl="~/Shared/Images/Site/ChartTypeProd.png"
CssClass="chart" OnClick="lbHope6_Click" />

 protected void lbHope6_Click(object sender, ImageClickEventArgs e)
    {
        EventArgs args = new EventArgs();

        if (Hope6 != null)
            Hope6(this, args);
    }

but when I do it this way it acts differently.. any ideas?? ive given up hope

 LinkButton lb = new LinkButton();
                lb.Text = s.Key.ToString();
                lb.Click += new EventHandler(lbHope6_Click);
                sourceNameCell.Controls.Add(lb);

   protected void lbHope6_Click(object sender, EventArgs e)
    {
        EventArgs args = new EventArgs();

        if (Hope6 != null)
            Hope6(this, args);
    }
+1  A: 

You're creating the new link button and assigning an eventhandler every time the page loads (including on postback). .Net won't understand that the LinkButton you create on postback is in fact the LinkButton you created when the page was first called. Your first code sample was fine.

Jeroen
its hard to explain the rest of the code aroudn it.. but basically the way it is now I need to click the link button twice for it to do what it is suppose to..
punkouter
So what is the proper way to create the event programmatically? I must do it on the page load I think because the link is made as part of a HTML table that is progarmmtically created.... So I mean its not something I can create outside that loop since inside the loop is the code that determines if that link/click event is created... that make sense? Only other work around I could figure it to just have a seperate table containing the links and I could make that seperate table declaritivly so I could use my first technique
punkouter
It all depends on how your html is generated. it's possible you are taking it the hard way. Maybe your solution is to define a DataBound ListView and use your first method to handle the clicks. The trick to get the event triggered is to construct the control on postback exactly as it was before postback in the CreateChildControls or DataBinding event.
Jeroen
I MUST create the HTML programmatically unfortunatly...
punkouter
There is no if (!IsPostBack)... so The same code is loaded bfore the click as it is loaded after the click... So that click event is subscribed to the same both ways... i dunno.. It just sucks to tell the client that this simple concept of clicking a link and it going to a page I cannot get to work.
punkouter