?
Solved

Dynamic Controls / Event Handlers

Posted on 2006-04-03
7
Medium Priority
?
416 Views
Last Modified: 2012-08-13
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?

0
Comment
Question by:friskyweasel
  • 3
  • 2
  • 2
7 Comments
 
LVL 12

Expert Comment

by:bsdotnet
ID: 16367538
simply use this,
        imgbtn_ta.Attributes.Add("onclick", "this.src='images/selected.gif';");
0
 

Author Comment

by:friskyweasel
ID: 16367626
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
 
LVL 12

Expert Comment

by:bsdotnet
ID: 16367841
Because of post back and imgbtn_ta is recreated.

Try this,
imgbtn_ta.Attributes.Add("onmouseover", "this.src='images/selected.gif';");
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Expert Comment

by:jatinderalagh
ID: 16368038
 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
 

Author Comment

by:friskyweasel
ID: 16370995
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
 
LVL 11

Accepted Solution

by:
jatinderalagh earned 2000 total points
ID: 16378050
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
 

Author Comment

by:friskyweasel
ID: 16391487
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month14 days, 11 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question