Link to home
Start Free TrialLog in
Avatar of friskyweasel
friskyweasel

asked on

Dynamic Controls / Event Handlers

Hi all - I've found a couple of other posts on this topic, but I'd like to get a solution to my specific situation. I'm dynamically building a table, and for each row in the table i'm adding an image button and several textboxes dynamically. The imagebuttons post back when clicked, but I have some other code that changes the ImageURL property of the image button. This code is in the onlclick event, but it appears this event isn't firing for the imagebuttons. The textboxes seem to be keeping their state just fine as far as i can tell...they all "come back" after i've posted to the page, and they all retain their values...

I have something like this in my client side page:
<TABLE id="MainTable" border="0" runat="server"></TABLE>

then in my code behind page, I call something like "AddRow1();" from inside the Page_Load event...inside AddRow1 is something like this for adding image buttons:
//add image button column
HtmlTableCell imgbtn_cell = new HtmlTableCell();
imgbtn_cell.NoWrap = true;
ImageButton imgbtn_ta = new ImageButton();
imgbtn_ta.ImageUrl      = "images/edit.gif";
imgbtn_ta.ID = "imgbtn_ta";
//imgbtn_ta.Click += System.Web.UI.ImageClickEventHandler(imgbtn_ta_Click());
imgbtn_cell.Controls.Add(imgbtn_ta);
row.Cells.Add(imgbtn_cell);

I have a hard-coded version of the page(all controls manually added),  which has this event handler for the image button - it changes the ImageURL once the image button is clicked, to show that it is now the currently selected row of the table:
private void imgbtn_ta_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
      imgbtn_ta.ImageUrl = "images/selected.gif";
}

I tried just bringing this event handler over to the dynamic page, but that didn't work out too well at all : ). Can someone give me the quickest, cleanest way to get my event handlers working?

Avatar of bsdotnet
bsdotnet

simply use this,
        imgbtn_ta.Attributes.Add("onclick", "this.src='images/selected.gif';");
Avatar of friskyweasel

ASKER

I put that change in but still getting the same behavior...clicking the image buttons posts the form, but the image associated with the button doesn't change...
Because of post back and imgbtn_ta is recreated.

Try this,
imgbtn_ta.Attributes.Add("onmouseover", "this.src='images/selected.gif';");
 imgbtn_ta.Attributes.Add("onclick", "this.src='images/selected.gif';return false;");

use the return false statement in end. That stops the post back.

Cheers
Jatinder
hi guys - thanks for the responses -  I think maybe I wasn't entirely clear about what I wanted.

I'm not trying to bypass or disable the postback - I actually want to keep the postback in there...just as it was when i built the form manually by dragging the ImageButton onto the form using the visual designer - What I want that I can't quite get to work is to be able to execute some code in the OnClick event of that Image button...but I'm missing something somewhere because even though it posts back to the page when i click the image button, it seems to be completely ignoring the OnClick event handler
ASKER CERTIFIED SOLUTION
Avatar of jatinderalagh
jatinderalagh
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
good articles - that works for me - for the benefit of others out there, this is what I did in a nutshell that seems to work:

used something like this to dynamically create my control - in this case an image button - Important -I had to put this code in the "OnInit" section of the code behind page...otherwise it didn't keep the state of my control values properly...and also caused problems with firing off my event handlers for the image buttons..

HtmlTableCell mybutton_cell = new HtmlTableCell();
ImageButton mybutton = new ImageButton();
mybutton.ImageUrl = "images/someimage.gif";
mybutton.ID = "mybutton";
mybutton.Click += new System.Web.UI.ImageClickEventHandler(this.mybutton_Click);
mybutton_cell.Controls.Add(mybutton);
row.Cells.Add(mybutton_cell);

then I just manually added an event handler somewhere out in my main page code:
private void mybutton_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
//do whatever processing you need here...
}

the key seems to have been placing the dynamic control declaration code in the "oninit" section - maybe this will help somebody else with a similar problem...thanks jatinderalagh for digging up that great post