locked
Button click issue on UpdatePanel's usercontrol RRS feed

  • Question

  • User1489758560 posted

    Hi ,

    I have usercontrol (called A) and update panel in it

    Following are the control in my updatepanel

    1.  gridview with link button

    2. DIV outside the gridview  to load the usercontrol (B) from c#

    when i hit the link button of gridview  i am loading my usercontrol(B)  dynamically from code behind and adding it to the  DIV which is working fine. I have button inside the the usercontrol (B) and if i hit the button it doesn't trigger the action. 

    Not sure why it doesn't trigger the click event. i googled and tried enabling ChildrenAsTriggers="true" though it didn't work. any suggestion please

    Thursday, July 17, 2014 2:52 PM

Answers

  • User1489758560 posted

    Hi kevin,

    Thanks for your reply and here is the logic.

    usercontrol(B):

    we can have just two labels in ascx page and having two public property in .ascx page to bind in the labels.

    this usercontrol(B) wil be loaded in Test.asax (ref : my previous post code) Template_click event.

    Lets assume the usecontrol(B) name as SimpleControl

    here is the Template_click event. FirstName,LastName is the two public properties of the  SimpleControl.

    here is my gridview button click event:

    protected void Template_Click(object sender, EventArgs e)
        {
    usercontrols_SimpleControl ucSimpleControl =
          LoadControl("~/usercontrols/SimpleControl.ascx")
          as usercontrols_SimpleControl;
         
        // Set the Public Properties
        ucSimpleControl.FirstName= "Milind";
        ucSimpleControl.LastName = "Chavan";
     divContainer.Controls.Add(ucSimpleControl);
    }

    but if i add the trigger for my gridview button, it will throw error as 

    A control with ID 'Template' could not be found for the trigger in UpdatePanel 'upList'.

    any help please to make this work

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 18, 2014 7:42 AM

All replies

  • User103196646 posted

    Hello Born2Win! Thanks for your post! 

    I hope this will help you out. Please check out this article "Dynamically Loading Controls in ASP.NET Is A Pain- http://odetocode.com/blogs/scott/archive/2006/07/20/dynamically-loading-controls-in-asp-net-is-a-pain.aspx

    My understanding is: you must re-create dynamic control on every single postback, remember the Page instance is created per request, if you do not re-create the control then it won't exist on PostBack.

    Regards!

    Thursday, July 17, 2014 3:55 PM
  • User1489758560 posted

    i tried to investigate in deeper and found that the update panel causing the issue. If i use the below code on the page load event then it works fine. the button triggers it's action. but when i click the button which is in gridview, it should not post back as it is in update panel, but it postback.

      private void RegisterPostBackControl()
        {
            foreach (GridViewRow row in gvList.Rows)
            {
                LinkButton lnkFull = row.FindControl("Template") as LinkButton;
                ScriptManager.GetCurrent(this.Page).RegisterPostBackControl(lnkFull);
            }
        }

    Here is the skeleton,

    Test.ascx : 

    <asp:UpdatePanel ID="upList" runat="server" ChildrenAsTriggers="true" >
        <ContentTemplate>
     <asp:GridView ID="gvList" AutoGenerateColumns="false" runat="server" Visible="true" Width="95%"
                        OnRowDataBound="gvList" DataKeyNames="Id" GridLines="None"  
                        ShowFooter="false">
                        <Columns>
      <asp:TemplateField>
                                <HeaderTemplate>
                                  
    <asp:label id="lbltext" runat="server" text = "TestLinkbutton"/>
                                   
                                </HeaderTemplate>
                                <ItemTemplate>
                                      <asp:Button ID="Template" OnClick="Template_Click" Text = "Sample" runat="server" Width="150px"/>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>
    
    <div style="width: 500px;" id="divContainer" runat="server"> 
            <br style="clear: left;" />
            </div>
    
       </ContentTemplate>
    </asp:UpdatePanel>

    on Template_Click event i am loading the user control from code behind and adding it to the div[divContainer]

    i am not sure is it possible to do with asyn process or should have to do the full postback which is i am trying to avoid . Any suggestion or correction please

    Thursday, July 17, 2014 9:34 PM
  • User1918509225 posted

    Hi born2win,

    First ,I am not sure how do you to add dynamic user control to your div

    Second, I suggest that you can set updatepanel  trigger control id as the linkbuttonid in your gridview.

    Best Regards,

    Kevin Shen.

    Friday, July 18, 2014 3:22 AM
  • User1489758560 posted

    Hi kevin,

    Thanks for your reply and here is the logic.

    usercontrol(B):

    we can have just two labels in ascx page and having two public property in .ascx page to bind in the labels.

    this usercontrol(B) wil be loaded in Test.asax (ref : my previous post code) Template_click event.

    Lets assume the usecontrol(B) name as SimpleControl

    here is the Template_click event. FirstName,LastName is the two public properties of the  SimpleControl.

    here is my gridview button click event:

    protected void Template_Click(object sender, EventArgs e)
        {
    usercontrols_SimpleControl ucSimpleControl =
          LoadControl("~/usercontrols/SimpleControl.ascx")
          as usercontrols_SimpleControl;
         
        // Set the Public Properties
        ucSimpleControl.FirstName= "Milind";
        ucSimpleControl.LastName = "Chavan";
     divContainer.Controls.Add(ucSimpleControl);
    }

    but if i add the trigger for my gridview button, it will throw error as 

    A control with ID 'Template' could not be found for the trigger in UpdatePanel 'upList'.

    any help please to make this work

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 18, 2014 7:42 AM
  • User1918509225 posted

    Hi born2win,

    I am glad that you have solved your problem.

    If you don't mind,please share your solution for us  which can help other who meet similar question like yours

    Best Regards,

    Kevin Shen.

    Wednesday, July 23, 2014 3:43 AM