364,033 Members | 4774 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

fire event in asp.net user control inside another asp.net user control

Mar Anthony
P: 6
Hi,

I have an existing asp.net user control A and I need to use it inside another asp.net user control B. The problem here is that I cannot edit the codes inside the user control A.

My user control A contains a button click event function.

I have a button inside my user control B. I want to fire the button click event from user control A when I click on the button from user control B.

The reason I want to fire the event from user control A when I click button inside user control B is because I want to make additional functions after the event is fired from user control A.

Anyone who tried this situation? Any help will be much appreciated.

Thank you
Jan 25 '12 #1

✓ answered by Frinavale

I'm not sure what you're doing wrong so I created a simple project with 2 user controls in it and a WebForm.

Here is UserControlA.ascx:
(ASP Code)
Expand|Select|Wrap|Line Numbers
  1. <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="UserControlA.ascx.cs"
  2.     Inherits="WebApplication1.UserControlA" %>
  3. <asp:Panel ID="PersonInfoContent" runat="server" Style="margin: 2em 0 0 0;">
  4.     <fieldset>
  5.         <legend>Person Information (User Control A)</legend>
  6.         <asp:Label ID="PersonInfo" runat="server" Style="float: right; display: block;" />
  7.         <label for="<%=Name.ClientID %>">First Name:</label>
  8.         <asp:TextBox ID="Name" runat="server" />
  9.         <br />
  10.         <label for="<%=LastName.ClientID %>">Last Name:</label>
  11.         <asp:TextBox ID="LastName" runat="server" />
  12.         <br />
  13.         <asp:Button ID="UpdatePersonInfo" runat="server" Text="Update Person" OnClick="UpdatePersonInfo_Click" />
  14.     </fieldset>
  15. </asp:Panel>
  16.  
(C# code behind)
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7.  
  8. namespace WebApplication1
  9. {
  10.     public partial class UserControlA : System.Web.UI.UserControl
  11.     {
  12.         protected void Page_Load(object sender, EventArgs e)
  13.         {}
  14.  
  15.         protected void UpdatePersonInfo_Click(object sender, EventArgs e)
  16.         {
  17.             DisplayPersonInfo();
  18.         }
  19.         public void DisplayPersonInfo()
  20.         {
  21.             PersonInfo.Text = String.Format("{0} {1} {2}","Updated name:", Name.Text, LastName.Text);
  22.         }
  23.     }
  24. }
Here's UserControlB.ascx:
(ASP Code)
Expand|Select|Wrap|Line Numbers
  1. <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="UserControlB.ascx.cs" Inherits="WebApplication1.UserControlB" %>
  2. <%@ Register Src="UserControlA.ascx" TagName="UserControlA" TagPrefix="uc1" %>
  3.  
  4. <fieldset style="margin: 0 auto; width: 50%">
  5.     <legend>Person And Phone Information (User Control B)</legend>
  6.     <uc1:UserControlA ID="UserControlAInstance" runat="server" />
  7.     <asp:Panel ID="PhoneNumberContent" runat="server" Style="margin: 2em 0 0 0;">
  8.         <asp:Label ID="PhoneDetails" runat="server" Style="display: block; float: right;" />
  9.         <label for="<%=PhoneNumber.ClientID %>">Phone Number:</label>
  10.         <asp:TextBox ID="PhoneNumber" runat="server" />
  11.     </asp:Panel>
  12.     <asp:Button ID="UpdateALL" runat="server" Text="Update Person And Phone Number" OnClick="UpdateALL_Click" />
  13. </fieldset>
(C# code behind)
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7.  
  8. namespace WebApplication1
  9. {
  10.     public partial class UserControlB : System.Web.UI.UserControl
  11.     {
  12.         protected void Page_Load(object sender, EventArgs e)
  13.         {}
  14.  
  15.         protected void UpdateALL_Click(object sender, EventArgs e)
  16.         {
  17.             UserControlAInstance.DisplayPersonInfo();
  18.             PhoneDetails.Text = String.Format("{0} {1}","Updated Phone Number:", PhoneNumber.Text);
  19.         }
  20.     }
  21. }
And here's my WebForm1.aspx (I have no code behind for it)
Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
  2. <%@ Register src="UserControlB.ascx" tagname="UserControlB" tagprefix="uc1" %>
  3.  
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  5.  
  6. <html xmlns="http://www.w3.org/1999/xhtml">
  7. <head runat="server">
  8.     <title></title>
  9. </head>
  10. <body>
  11.     <form id="form1" runat="server">
  12.     <div>
  13.         <uc1:UserControlB ID="UserControlB" runat="server" />
  14.         <asp:Button ID="btn" runat="server" Text="Button in Form" />
  15.     </div>
  16.     </form>
  17. </body>
  18. </html>
You'll notice that if I click the button in UserControlA, the name details label is updated with the first and last name provided.

If I click the button in UserControlB, the name details label in UserControlA is updated and the phone number details label in UserControlB are updated.

If I click the button in the WebForm, neither of the user control's are updated (to see this you'll have to make changes to them before hitting the button).

-Frinny
Share this Question
Share on Google+
6 Replies


Frinavale
Expert Mod 5K+
P: 7,034
Use Events to solve your problem :)

In UserControlA, declare an event called "ButtonWasClicked" or something. Raise this event in the Button Click event.

Since UserControlB contains an instance of UserControlA, you can handle the event raised by UserControlA within it.

In the method that handles the custom "ButtonWasClicked" event, do the same thing as you would if you clicked the button in UserControlB.

OH wait it goes the other way around...
So you don't need to use events.

In UserControlA create a public method that UserControlB can call during a button click event.


Ok I don't know which way you want to go...I've confused myself but I think you can probably figure out what to do based on what I am saying :)

I hope anyways!

-Frinny
Jan 25 '12 #2

Mar Anthony
P: 6
Thank you for your reply. I tried updating my usercontrolA, I separated the function inside the button click into a public method so I can access it from usercontrolB. I can access the method, but the problem I am getting now is that the controls inside my usercontrolA are all null, for example all my textbox inside usercontrolA becomes null whenever I access the public method on my button click from usercontrolB.

I am wondering why are the controls null when I access it through the public method from the button click event on my parent user control.

Any answer would be great, appreciate your reply.

Thank you
Jan 26 '12 #3

Frinavale
Expert Mod 5K+
P: 7,034
Are you dynamically adding user controls to the page?
If so, are you doing this properly (in the page init event)?
If not...at what point in the page life cycle are you calling the method?

-Frinny
Jan 26 '12 #4

Mar Anthony
P: 6
I added/included the child user control statically on my parent user control.

This is the scenario, my usercontrolA contains a form with textboxes and a button. There is a public method inside usercontrolA which is called and executed when the button inside usercontrolA is clicked.

When I added the usercontrolA inside usercontrolB, I wanted to execute additional codes from my usercontrolB so what I did was I made the button from usercontrolA hidden on page load and added a button to usercontrolB that will call (a)the public method inside my usercontrolA(which was supposedly called when the button inside usercontrolA is clicked) and (b)the additional codes that I added inside my usercontrolB.

So the sequence on my codes should execute first the public method inside my usercontrolA, if this is success, then I will execute my additional codes inside usercontrolB.

The problem I am encountering is that the form fields inside my usercontrolA are all returning null values when I execute the public method from usercontrolA which was called from my event in usercontrolB.

I'm not really sure if there are still simpler ways to do this, I am just trying to avoid updating my codes inside usercontrolA because this was used by another application.
Jan 26 '12 #5

Frinavale
Expert Mod 5K+
P: 7,034
I'm not sure what you're doing wrong so I created a simple project with 2 user controls in it and a WebForm.

Here is UserControlA.ascx:
(ASP Code)
Expand|Select|Wrap|Line Numbers
  1. <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="UserControlA.ascx.cs"
  2.     Inherits="WebApplication1.UserControlA" %>
  3. <asp:Panel ID="PersonInfoContent" runat="server" Style="margin: 2em 0 0 0;">
  4.     <fieldset>
  5.         <legend>Person Information (User Control A)</legend>
  6.         <asp:Label ID="PersonInfo" runat="server" Style="float: right; display: block;" />
  7.         <label for="<%=Name.ClientID %>">First Name:</label>
  8.         <asp:TextBox ID="Name" runat="server" />
  9.         <br />
  10.         <label for="<%=LastName.ClientID %>">Last Name:</label>
  11.         <asp:TextBox ID="LastName" runat="server" />
  12.         <br />
  13.         <asp:Button ID="UpdatePersonInfo" runat="server" Text="Update Person" OnClick="UpdatePersonInfo_Click" />
  14.     </fieldset>
  15. </asp:Panel>
  16.  
(C# code behind)
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7.  
  8. namespace WebApplication1
  9. {
  10.     public partial class UserControlA : System.Web.UI.UserControl
  11.     {
  12.         protected void Page_Load(object sender, EventArgs e)
  13.         {}
  14.  
  15.         protected void UpdatePersonInfo_Click(object sender, EventArgs e)
  16.         {
  17.             DisplayPersonInfo();
  18.         }
  19.         public void DisplayPersonInfo()
  20.         {
  21.             PersonInfo.Text = String.Format("{0} {1} {2}","Updated name:", Name.Text, LastName.Text);
  22.         }
  23.     }
  24. }
Here's UserControlB.ascx:
(ASP Code)
Expand|Select|Wrap|Line Numbers
  1. <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="UserControlB.ascx.cs" Inherits="WebApplication1.UserControlB" %>
  2. <%@ Register Src="UserControlA.ascx" TagName="UserControlA" TagPrefix="uc1" %>
  3.  
  4. <fieldset style="margin: 0 auto; width: 50%">
  5.     <legend>Person And Phone Information (User Control B)</legend>
  6.     <uc1:UserControlA ID="UserControlAInstance" runat="server" />
  7.     <asp:Panel ID="PhoneNumberContent" runat="server" Style="margin: 2em 0 0 0;">
  8.         <asp:Label ID="PhoneDetails" runat="server" Style="display: block; float: right;" />
  9.         <label for="<%=PhoneNumber.ClientID %>">Phone Number:</label>
  10.         <asp:TextBox ID="PhoneNumber" runat="server" />
  11.     </asp:Panel>
  12.     <asp:Button ID="UpdateALL" runat="server" Text="Update Person And Phone Number" OnClick="UpdateALL_Click" />
  13. </fieldset>
(C# code behind)
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7.  
  8. namespace WebApplication1
  9. {
  10.     public partial class UserControlB : System.Web.UI.UserControl
  11.     {
  12.         protected void Page_Load(object sender, EventArgs e)
  13.         {}
  14.  
  15.         protected void UpdateALL_Click(object sender, EventArgs e)
  16.         {
  17.             UserControlAInstance.DisplayPersonInfo();
  18.             PhoneDetails.Text = String.Format("{0} {1}","Updated Phone Number:", PhoneNumber.Text);
  19.         }
  20.     }
  21. }
And here's my WebForm1.aspx (I have no code behind for it)
Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
  2. <%@ Register src="UserControlB.ascx" tagname="UserControlB" tagprefix="uc1" %>
  3.  
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  5.  
  6. <html xmlns="http://www.w3.org/1999/xhtml">
  7. <head runat="server">
  8.     <title></title>
  9. </head>
  10. <body>
  11.     <form id="form1" runat="server">
  12.     <div>
  13.         <uc1:UserControlB ID="UserControlB" runat="server" />
  14.         <asp:Button ID="btn" runat="server" Text="Button in Form" />
  15.     </div>
  16.     </form>
  17. </body>
  18. </html>
You'll notice that if I click the button in UserControlA, the name details label is updated with the first and last name provided.

If I click the button in UserControlB, the name details label in UserControlA is updated and the phone number details label in UserControlB are updated.

If I click the button in the WebForm, neither of the user control's are updated (to see this you'll have to make changes to them before hitting the button).

-Frinny
Jan 26 '12 #6

Mar Anthony
P: 6
Hmmmm... This has also the same concept as what I did. I will check both my codes and your version. Thank you very much Frinny. I really appreciate your help.
Jan 26 '12 #7

Post your reply

Help answer this question



Didn't find the answer to your ASP.NET question?

You can also browse similar questions: ASP.NET asp.net user control button click event event handler user control