473,386 Members | 1,873 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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

6 9720
Frinavale
9,735 Expert Mod 8TB
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
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
9,735 Expert Mod 8TB
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
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
9,735 Expert Mod 8TB
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
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

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Bruce W.1 | last post by:
I've got a User Control which has a bunch of buttons for navigating. When clicked they do a Server.Transfer to another page in the application. How do I make the clicked nav button look...
3
by: DC Gringo | last post by:
I've got a web user control (a) inside a web user control (b) inside a webform (c). I need the webform to set a label control text value inside the inner of the two web user control. Inside my...
4
by: vatech1993 | last post by:
I'm stumped on this problem. I've created a user control that dynamically creates 5 linkbuttons in the CreateChildControls method. Each of these child controls is linked to a commandeventhandler,...
8
by: tatemononai | last post by:
I had a beautiful script that was running, well, just beautifully. But then I decided to take a button that fired an event and place it inside a <asp:table. The event WILL NOT FIRE INSIDE THE...
0
by: muthu | last post by:
Hi guys, Iam using a repeator control in my page.In wich i have dropdown list as the last column.I have a label in the first column.Now i have binded a datable to the repeator in the page load...
4
by: Sevu | last post by:
I am working with ASP.NET.I am using ReportViwer Control to show my report.I like to add dropdownlist with in the reportviewer control. ( Not top to the control some thing like that).I need to...
0
by: davidr | last post by:
Hi, I been stuck on this for a week or more. So please help! I have a menu were a user can click a button and a user control is loaded into the page. A problem occurs when trying to load a...
2
by: ccbalapatel | last post by:
Hi, We have recently converted our code to ASP.NET 2.0. We have an user control that is embedded inside the page. The user control has a link button and the event handler for the link button is...
1
by: Salim | last post by:
Hi, I'm trying to get UniqueID of a linkbutton. I have 2 web user controls. And a master page. In fisrst web user control there is a datalist. In datalist ItemCreated event, I try to find...
0
by: MasterOfTheDark | last post by:
This one's a hairy one to explain. What I've got is a class called "VistaGroupBox" that I put some nice-looking Vista-style gradients in the background and made to look like the ribbon categories...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.