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?

friskyweaselAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

bsdotnetCommented:
simply use this,
        imgbtn_ta.Attributes.Add("onclick", "this.src='images/selected.gif';");
0
friskyweaselAuthor Commented:
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...
0
bsdotnetCommented:
Because of post back and imgbtn_ta is recreated.

Try this,
imgbtn_ta.Attributes.Add("onmouseover", "this.src='images/selected.gif';");
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

jatinderalaghCommented:
 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
0
friskyweaselAuthor Commented:
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
0
jatinderalaghCommented:
Accepted Answer from TheAvenger
Date: 01/06/2005 04:55PM TPT
Grade: A
 Accepted Answer  

It is important that once you create a dynamic control and then post the page back, you have to recreate the dynamic controls. This means: When you click the button, the page is sent again to the server. Now your application should know that a control was already created (on the previous step) and recreate it in the Page_Load. Then it will handle the new click.

In general: whenever new controls are created, you need to recreate them once the page is posted back.

See the following pages for more details:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbtskaddingcontrolstowebformspageprogrammatically.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbconwebformspageprocessingstages.asp
 


By default, dynamically created controls are not saved to the Page's ViewState, meaning that they are lost across postbacks. So every time you get back to form (post back your form ) you need to recreate them.

look how to create dynamic controls and use them http://support.microsoft.com/default.aspx?scid=kb;EN-US;317515

Look how u can save the viewstates
http://www.codeproject.com/Purgatory/ViewState___Dynamic_Cntrl.asp
 

Jatinder
 
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
friskyweaselAuthor Commented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.