Showing posts with label events. Show all posts
Showing posts with label events. Show all posts

Wednesday, March 28, 2012

Programmatically Adding Controls and Events

Hi, I am trying to add several link buttons to a page programmatically with the following code:

int i;
int i;
for (i = 0; i < 60; i++)
{
LinkButton lnk = new LinkButton();
lnk.ID = "region" + i;
lnk.CssClass = "mystyle";
lnk.Click += new System.EventHandler(lnk_Click);

addContent.Controls.Add(lnk);

}


which adds them perfectly, however I also want to add an event handler to them all (the same event handler) which I had hoped to achieve by adding the ' lnk.Click += new System.EventHandler(lnk_Click);'
however when the link buttons are now clicked the page freezes, any suggestions?

All that is assigned on the onclick event is a write out to a label within an ajax.net update panel.

Any help would be appreciated, cheers Jon

I set this up in my dev environment and wa sunabel to duplictae it. Can you post more of your code... the ASP and the code behind to give us a better idea of whats going on.

I got the code working - for some reason the fact that the linkbuttons were placed inside a updatepanel and that the 'childrenastriggers' set to true seems to cause the slow down.

I think i know the problem but i am not sure how to solve it!

basically my onload function generates around 1500 linkbuttons in tiny squares within an update panel, each assigned a unique ID.

onclick of each button it posts its unique number to a textbox (temporarily so i can see if it is doing something) it takes around 60 seconds to do this when the 'childrenastriggers' is set to true.

When this is not set to true it performs it instantly, which is fine, however what I am trying to achieve that on click of the linkbutton it puts the number in the box, and changes the css of the linkbutton, updates so the user can see it has changed.

However the update panel is refreshing the generation of 1500 link buttons - which is causing the slow down.

Without the update panel refreshing, and if i cause a full postback the change is instant.

Any ideas on how to get around this but still achieve something similar.


Try and place your button generation code in the Page_Init event.


worked a treat, thank you!

Wednesday, March 21, 2012

Question on Modalpopupextender & according events

I'm using a modalpopupextender in a user control, which sits on a basepage of my website.
TargetControlID, PopupControlID & CancelControlID are all set and everything works great & smooth.

However I'd like to implement some custom code on the button click events.
What I want to do:

On my masterpage, there is an ajax timer control which is handling the automatic refresh of the basepage content.
The user control with the modalpopupextender is used on one of the basepages. Users can pop-up a screen with this to do selections in.
However, the ajax timer control will still be handling the refresh and it will cause the modal popup to close again during a refresh.

The way I thought to handle this is to stop the ajax timer control on the masterpage when a user clicks the button to show the modalpop-up
and start the timer again when the user clicks the cancel or ok button of the modalpopup.
I think you can't do this directly as the button controls that trigger the modalpopup events cannot be assigned to do anything else.
I tried the trick with the hidden control and the code-behind stuff to stop & start the timer, but I find it somehow slow and also get some weird effects with other controls on the basepage.

I know it's also possible to stop & start an ajax timer client side with the code shown here:http://forums.asp.net/t/1094798.aspx
but I was wondering how to implement it (I'm an Ajax n00b) and if this is the way to go or to stay away from to solve my little problem.

Any help is greatly appreciated!
Thanks!

I fixed it. For anyone who needs this, the problem was with the prefix that needs to be added to the timercontrol name, because it sits on the masterpage and
the function that stops & starts the timer is called from the basepage.

What I basically did:

//We define clientside scripts to stop & start the ajax refresh timer
//from clientside! We do this because the popupmodalextender
//binds to the linkbuttons lnkbtnHistory & lnkbtnCancel but will not handle server side code
//without using hidden 'in-between' controls
//note that the javascript looks for the control "ctl00_tmrRefresh".
//the 'ct100_' prefix is needed because the control resides on the masterpage!

Page.ClientScript.RegisterClientScriptBlock(this.GetType(),"stopTimer", @."function stopTimer() {var timer=$find('ctl00_tmrRefresh');timer._stopTimer(); }",true);
Page.ClientScript.RegisterClientScriptBlock(this.GetType(),"startTimer", @."function startTimer() {var timer=$find('ctl00_tmrRefresh');timer._startTimer(); }",true);

//add custom attributes that refer to the registered client scripts
//with client-side code to stop & start the refresh the timer on button click event

lnkbtnHistory.Attributes.Add("onclick","stopTimer()");
lnkbtnCancel.Attributes.Add("onclick","startTimer()");