Button in dynamic user control need to be clicked twice to fire event?

I have created a website that uses dynamic user controls. I placed a button in a dynamic user control but its need to be clicked twice to fire event. First click doesn't work, but subsequent clicks work. What should I do?

Here is my code snippets:


<asp:Panel ID="Panel1" runat="server">

 <asp:Button ID="btnAnaSayfa" runat="server" Text="AnaSayfa" onclick="btnAnaSayfa_Click" />



if (this.IsPostBack)
    string controlName = string.Empty;
    if (null != ViewState["controlname"])
        controlName = ViewState[
UserControl uc = (UserControl)LoadControl(controlName);


protected void Button1_Click(object sender, EventArgs e)
    UserControl uc = (UserControl)LoadControl("detail.ascx");
"controlname"] = "detail.ascx";


12/8/2008 8:59:15 PM
asp.net.getting-started 91979 articles. 4 followers. Follow

1 Replies

Similar Articles

[PageSpeed] 41
Get it on Google Play
Get it on Apple App Store


Based on my test, please change the the code of the Button1 to the following.

      if (ViewState["controlname"] == null)
            UserControl uc = (UserControl)LoadControl("detail.ascx");
            ViewState["controlname"] = "detail.ascx";


I look forward to receiving your test results.

Zong-Qing Li
Microsoft Online Community Support
Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as
Answer” if a marked post does not actually answer your question.
12/10/2008 3:22:15 AM

Similar Artilces:

Dynamically loaded user control with a button doesn't fire its button click event?
I have a datagrid, in which one of the columns is 'button' column called 'Send Email'. When I click on this button, then an email user control is dynamically loaded into a placeholder above the datagrid header area. The email control renders correctly, but when I click on 'Send' button in this user control, then the click event of this 'Send' button doesn't fire. This is even though the 'Handles.btnSend.Click' is there for the click method of the 'Send' button. Can someone please tell me why the event is not firing and what do I need to do to make it fire?    sun21170 you have...

User Control Button Click Event Does Not Fire With Postback, when Loaded Dynamically
My application consists of a web form (MainForm.aspx) with a PlaceHolder (phMain) with several user controls that are loaded and removed dynamically based on the context. Two of these controls are LoginUC.aspx and DataEntryUC.ascx. Each of these controls have a 'Submit' button.When the application is launched, LoginUC is loaded to the PlaceHolder. Users enter an Id and Password, then upon authentication an event named  LoginCompleted.is raised in LoginUC. This event (LoginCompleted) is consumed in the parent form (MainForm.aspx) wherein the LoginUC control is removed...

Dynamically loading user control to a gridview
Hello,I know there are many posts about loading user control dynamically and events which not fire. I tried  everything suggested but nothing helped. The difference in my example is that I am loading the user control into a specific line in a grid view after the user clicked on a button.So, the containing object in this case is DataControlFieldCell but it is a difference one on every click of the user.I tried to to set the ID of the user control on the page load of the control (this.ID = "MyControl1";) and I am loading the control on post back in the page_load of the parent pa...

Button click event not getting fired for a custom webpart where controls are added dynamically.
 We are migrating from ASP.NET1.1/Sharepoint 2003 to ASP.NET 2.0/Moss 2007. We have a webpart on which controls are getting added dynamically. We have a button which is also dynamically added and a click event handler is also attached to it. However when we click on this button the page gets posted but the event is not fired. We analysed and found that the code to add the button in the webparts controls collection is executed but the code to add the button to it's container (that is a table) is not getting executed. Hence the event is not getting handled. If we forcefully add the bu...

how to create button controls dynamically .. then i need that click event of buttons
how to create button controls  dynamically .. then i need that click event of buttons  urgent ???????Vino  Add dynamic controls with eventsWebsite Design Darlington - http://mdssolutions.co.ukhttp://lessthandot.com - Experts, Information, Ideas & Knowledgehttp://aspnetlibrary.com - An online resource for professional ASP.NET developersPlease remember to click "Mark as Answer" on this post if it helped you refer below snippet..public partial class _Default : System.Web.UI.Page {    protected void Page_Load(object sender, EventArgs e)  ...

Dynamically add controls to user control based on Parent page button click event
Hi, I have a page which contains a button, a dropdownlist and a user control.  The user control is added to the parent page during page_load.    If the user clicks on the button, the value in the dropdownlist is passed to the usercontrol via the usercontrols public properties and is stored in its viewstate. The user control is dynamically populated with fields based on a database id passed to it from the parent page.  I gave the user control a public property which hooked into the ViewState bag of the user control so that I could pass it the database id at r...

Dynamically loaded Web User Control .ascx button click event handler not called on second time click
I have an ascx control (that performs actions for an entity) being added dynamically to accordion panes, and then the panes are added to an ajax accordion control, so that the accordion can contain multiple panes with each pane containing one ascx control. The ascx control contains a delete button which removes the entity from the database. Once the entity is removed, I need to reflect this deletion in the parent page by removing the accordion pane from the accordion. To do this I created a delegate function and which is called from the parent page to rebind the accordion and hence not d...

Web User Control - Button Click
I have a web user control which is has one Button control on it. It has a event for handling the click... if I put this user control ( button ) at the top  of the page, the click event does not get fired just page is refreshed...but where as if I move to the bottom of the page, ButtonClick is fired ???? I am completely clueless why is this change of behavior with respect to the position of the control?? Thanks. could you show us the code that you have problem with ...   Here is the code ( i have taken out the unwanted part of the page ) Please note that the bSave does ...

Error when Loading Dynamic UserControl from current User Control's Button Click event Calling Page Event
Hi,I have a panel that I load user Control in no problem.  The problemarrises when I do a post back on one of these user controls.  I havebutton it does a click event.  In this click event I will do somedatabase updates, then I will use reflection to call a method in theparent (this.page) to load the new user control.  Its at this pointafter it loads the new user control that it throws the error "Thecontrol must be placed inside a form tag with runat=server".Now, my methods that managers the user control works.  I am able toclick on the parent page menu and...

Cannot get a click event fired for a dynamic button...
Hello all, I am pulling my hair out trying all sorts of different ways to do what seems so simple.I have been searching numerous forums seeking the answer but none have worked. I am not completely new to programming but relatively new to .Net.I have created a user control that generates a file upload panel in code. It is a panel that dynamically creates its controls at run-time. The reason I generate controls in code is that i need to change the file-upload display depending upon which page calls it.Here is the entrie code for analysis: Partial Class fileLoader    Inherit...

Button Click event dont fire in a User Control
ok this is my case.  ive an usercontrol and i put a button and a dropdownlist with autopostback.  the dropdown works Fine but the Button i click several times and do nothing!!.  the same happend with a <A and LinkButtons nothing works.   By the way, im not sure is this is the reason but the user control is used for a popup using Ajax ModalPopupExtender.  <PANEL><USERCONTROL></PANEL> the panel is attached to the modalpopupextender.. ANY HELP!???+Jose Manuel Peña+Dominican Republic+Developer Can you post your code?If everything happens for...

Button Click not firing in Dynamically loaded user control
    Hi,I realize this is a common problem but i seem to be having a unique variation.I have a page that is dynamically loading user controls into a placeholder depending on user selection from a dropdown list on the page. The controls get loaded on page postback using a case statement to determine which control to load.These controls have buttons on them that are wired up properly - if I put the control on the page declaratively everything works fine.However if I load them dynamically when the button is clicked the page gets posted back, the control gets re-loaded, and the cli...

button click events not firing within user control in placeholder
I have a remove item button button in a user control that I place a number of them in a placeholder in the aspx page. For some reason the button does not respond to the click event (redirect does not work and the debug step through doesn't stop on the break). I have placed another button in the control just in case there was somthing messed up with the code on the first one, but still nothing heppens when I press it. The event control is declared withEvents and the event is properly tied to the sub (using VS so it is hard to mess this part up). I have created another project and se...

Dynamically adding User Control instances on Button click event
Hi All:  I have a user control which has three text boxes in it. Now I have a page where there is a place holder and a button. On the click event of the button I add an instance of the user control to the place holder. It works fine the first time and the user control is added to the place holder. However when I click again, a new user control instance is added and the first instance is not there... same thing is repeated if I click again...new instance is added and none of the previous ones are ther ein the place holder. The code for the web page is as follows:int iTextBoxUserControlC...

Web resources about - Button in dynamic user control need to be clicked twice to fire event? - asp.net.getting-started

Dynamics - Wikipedia, the free encyclopedia
Brownian dynamics , the occurrence of Langevin dynamics in the motion of particles in solution (e.g. a grain in water, as was first seen by Brown); ...

Datacenter Dynamics
Datacenterdynamics for data center information, conferences, news, videos, whitepapers and reports globally

Dynamic Chiropractic - chiropractic, news, articles, research & information for chiropractors - Find ...
Dynamic Chiropractic - chiropractic, news, articles, research & information for chiropractors - Find a Chiropractor

Paul Melville: a dynamic talent taken before his time
What might have become of a popular young cricketer with the world at his feet?

Lenovo and Apple: PCs’ Dynamic Duo, Gobbling Up Share in Downbeat Market
... in units sold, with gains in market share for top vendor Lenovo Group ( 0992HK ), and for Apple ( AAPL ), who turned out to be the Dynamic Duo ...

Seattle Councilmember O'Brien: Power dynamic between Uber, drivers is out of balance
Seattle is the first city in the U.S. to give Uber drivers the right to form a union, but whether that actually happens might be up to the courts. ...

Rise of the Tomb Raider director leaves Crystal Dynamics for Infinity Ward
Tomb Raider Senior Art Director and Rise of the Tomb Raider Game Director Brian Horton has left Crystal Dynamics, the developer announced via ...

Boston Dynamics wishes you a terrifyingly happy holiday
Whether you think holiday greeting cards are touching or annoying, Boston Dynamics is celebrating the season just a little differently with this ...

IBM's Dynamic Pricing Helps Retailers Navigate Online Price Wars
IBM Dynamic Pricing enables retailers to intelligently adjust prices while driving customer loyalty and product demand.

Are Boston Dynamics' Robot Reindeer The Ghosts Of Christmas Future?
Either that, or the nightmare before Christmas. The Google-owned robotics company Boston Dynamics delivered a unique holiday greeting this week: ...

Resources last updated: 1/20/2016 4:43:04 PM