Showing posts with label buttons. Show all posts
Showing posts with label buttons. 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!

programmatically created buttons click event handler not working in updatepanel

i have a hovermenuextender with Target Control - an updatepanel, PopupControl - a panel with button named edit.

when i press edit, a textbox, and a button are created programmatically and placed inside the updatepanel.

the button's click event has a handler (added programmatically) which shud replace the text in updatepanel with the text in textbox, but this is not working - here's the snippet:

1protected void OrseButton_Click(object sender, EventArgs e)2 {3 OrseUpdatePanel.ContentTemplateContainer.Controls.Add(tbox);4 OrseUpdatePanel.ContentTemplateContainer.Controls.Add(savebtn);5 savebtn.Text ="Save";6 savebtn.Click +=new EventHandler(savebtn_Click);7 }89void savebtn_Click(object sender, EventArgs e)10 {11string temp;12 temp = tbox.Text;13 OrseUpdatePanel.ContentTemplateContainer.Controls.Clear();14 OrseUpdatePanel.ContentTemplateContainer.Controls.Add(new LiteralControl(temp));1516//throw new Exception("The method or operation is not implemented.");17 }

help me on this...i m using vs 2005, asp with ajax, c#

whats hapening is ur manipulating ur button and events at runtime, so when the page postback, the buton info and event is lost, what u need to to is save the whole thing to a viewstate, then on page call back u must recall this viewstate and reassign it to the button.

savebtn.Text ="Save";
savebtn.Click +=new EventHandler(savebtn_Click);

ViewsState["btn"] = savebtn;

On ur callback

savebtn = (button)ViewsState["btn"]

Hope this helps


i placed :

savebtn = (button)ViewsState["btn"]

in savebtn_click method.

now when i run it, it gives error serializing value 'system.web.ui.webcontrols.button' of type 'system.web.ui.webcontrols.button'


ok, on each callback recreate the event for ur button, coz when creating event at runtime, and when there'sa postback or callback, the event is lost, so have to recreate it and Once created ASP.Net directly rebind it.


can i have some example code...i wanna make sure i got u completely.

Programmatically created Submit button on UpdatePanel want fire without The UpdatePanel be

I have programmatically created some controls(labels, buttons) that make up and object in several rows of a table on an UpdatePanel. On the initial load of the page, I create all the objects(controls). Then I click one of the buttons to do a partialrendering update of some the controls in the UpdatePanel. The button click causes a post back which I throught was good. The problem is that if i do not refesh the entire set of controls on the UpdatePanel first, the button click want fire and I can't firgure out why.

Here is some of the code:

<asp:ScriptManagerID="ScriptManager1"runat="server"EnablePartialRendering="true"AsyncPostBackTimeout="36000"/><asp:UpdatePanelID="UpdatePanel2"runat="server"ChildrenAsTriggers="false"UpdateMode="Conditional"><ContentTemplate><asp:TableID="Table1"runat="server"></asp:Table><asp:UpdateProgressID="UpdateProgress"runat="server"AssociatedUpdatePanelID="UpdatePanel2"><ProgressTemplate>

Processing Service Request .........

</ProgressTemplate></asp:UpdateProgress></ContentTemplate></asp:UpdatePanel>

protectedvoid btnSubmit_Click(object sender,EventArgs e)

{

RefreshStatus(); --> Causes controls to be built on UpdatePanel.

}

protectedvoid Page_Load(object sender,EventArgs e)

}

if (!IsPostBack)

{

}

else

{

<-- If I do a"RefreshStatus();"here, all works fine but the entire set of controls on the UpdatePane is re-Built. Not what I want.

}

Button Click:

void ChangeStatus_Click(object sender,EventArgs e)

{

Button ChangeStatus = (Button)sender;

----- Update subset of controls on UpdatePanel here -------

}

Since there is no ReFreshStatus() during the postback the button wantfire and nothing happens. I am puzzel by this.

Any Ideas?

Thanks

I forgot mention the each button is built with a AsyncPostBack trigger, ie:

// Add Updatepanel Triggers for the buttonAsyncPostBackTrigger trigForBtns;

trigForBtns =

newAsyncPostBackTrigger();

trigForBtns.ControlID = ChangeStatus.ID;

trigForBtns.EventName =

"Click";

Thanks again, I really need some help with this.


I just wanted to mention that I solved this problem byImplementing Ajax incremental asynochronous updating of controls on the web page.


Thats niceSmile

Can you post some code details here ?

Wednesday, March 21, 2012

Question about updatepanel

Hi,
I got a problem about update panel.
My scenario is that:
I have two tasks, each of which is triggered by buttons..
"A" needs 10 seconds to complete, and "B" needs 2 seconds.
After clicking A button, the user click B within 10 seconds.
Normally the program will stop doing A and start doing B, right?
But once I use the UpdatePanel, it just keep doing A and doesn't do B.
WHY?

Here is my example code:

<body> <form id="form1" runat="server"> <atlas:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="True"/> <asp:Button ID="ButtonA" runat="server" Text="intensive task" /> <asp:Button ID="ButtonB" runat="server" Text="interrupt task" />  <asp:Label ID="Label2" runat="server" /> <atlas:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <asp:Label ID="Label1" runat="server" /l> </ContentTemplate> <Triggers> <atlas:ControlEventTrigger ControlID="ButtonA" EventName="Click" /> </Triggers> </atlas:UpdatePanel> <atlas:UpdateProgress ID="UpdateProgress1" runat="server"> <ProgressTemplate> Processing... </ProgressTemplate> </atlas:UpdateProgress> </form> </body>
Protected Sub ButtonA_Click(ByVal senderAs Object,ByVal eAs System.EventArgs)Handles ButtonA.Click System.Threading.Thread.Sleep(10000) Label1.Text ="Intensive task suceeded!!"End Sub Protected Sub ButtonB_Click(ByVal senderAs Object,ByVal eAs System.EventArgs)Handles ButtonB.Click Label2.Text ="Interrupted task succeeded!!"End Sub
I need the normal function, that is to stop A and do B when clicking ButtonB, and also the function of partial refreshing. Any ideas?Could anyone give me some suggestion?