473,396 Members | 1,725 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,396 software developers and data experts.

user control on a web page that uses a master page.

CroCrew
564 Expert 512MB
Hello everyone,

First let me say thanks to anyone that post an answer to my question.

Let me explain the what I am trying to do. The code below is an example and try to keep all answers within the five pages that I have posted. I did not want to post the complex version of my code because I think if this gets solved then others can use the simplest version of this question and apply it to their solutions.
  1. I want a user control (WebUserControl.ascx) on a web page (Home.aspx) that uses a master page (MasterPage.master).
  2. There is a button on the user control (WebUserControl.ascx) that will do a calculation within the code behind of the user control (WebUserControl.ascx.cs).
  3. There is a “Label” on the web page (Home.aspx) that I want updated to show the calculated value from “TextBox3” in the user control (WebUserControl.ascx) after the button in the user control (WebUserControl.ascx) has been clicked.

I have tried and tried but could not find an elegant way of doing this. Any ideas? Below is the code.

**MasterPage.master**
Expand|Select|Wrap|Line Numbers
  1. <%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>
  2. <!DOCTYPE html>
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5.     <title></title>
  6.     <asp:ContentPlaceHolder id="head" runat="server">
  7.     </asp:ContentPlaceHolder>
  8. </head>
  9. <body>
  10.     <form id="form1" runat="server">
  11.     <div>
  12.         Add it up!<br />
  13.         <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
  14.  
  15.         </asp:ContentPlaceHolder>
  16.     </div>
  17.     </form>
  18. </body>
  19. </html>
  20.  
**Home.aspx**
Expand|Select|Wrap|Line Numbers
  1. <%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Home.aspx.cs" Inherits="Home" %>
  2. <%@ Register TagPrefix="Math" TagName="AddControl" Src="~/WebUserControl.ascx" %>
  3. <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
  4.     <asp:Label ID="Label1" runat="server" Text="Label">Display the value from control here!</asp:Label><br />
  5.     <Math:AddControl ID="CoolMath" runat="server" />
  6. </asp:Content>
  7.  
**Home.aspx.cs**
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. public partial class Home : System.Web.UI.Page
  9. {
  10.     protected void Page_Load(object sender, EventArgs e)
  11.     {
  12.  
  13.     }
  14. }
  15.  
**WebUserControl.ascx**
Expand|Select|Wrap|Line Numbers
  1. <%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>
  2. <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
  3. <asp:TextBox ID="TextBox1" runat="server" /> + 
  4. <asp:TextBox ID="TextBox2" runat="server" /> = 
  5. <asp:TextBox ID="TextBox3" runat="server" />
  6.  
**WebUserControl.ascx.cs**
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. public partial class WebUserControl : System.Web.UI.UserControl
  9. {
  10.     protected void Page_Load(object sender, EventArgs e)
  11.     {
  12.  
  13.     }
  14.  
  15.     protected void Button1_Click(object sender, EventArgs e)
  16.     {
  17.         Int32 Val1 = Int32.Parse(TextBox1.Text.ToString());
  18.         Int32 Val2 = Int32.Parse(TextBox2.Text.ToString());
  19.  
  20.         TextBox3.Text = (Val1 + Val2).ToString();
  21.     }
  22. }
  23.  

Please help and thanks you!
CroCrew~
Jul 13 '13 #1

✓ answered by Frinavale

First you need your custom event args:
Expand|Select|Wrap|Line Numbers
  1. public class TCalculationEventArgs : EventArgs
  2. {
  3.   public string Result{get; set;}
  4.   public TCalculationEventArgs(string result){
  5.     this.Result = result;
  6.   }
  7. }
  8.  
Then in your WebUserControl declare a public "event" for the class that can be handled by your main window. In the button click event, do your calculation and raise the new event.

**WebUserControl.ascx.cs**
Expand|Select|Wrap|Line Numbers
  1. using Microsoft.VisualBasic;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Data;
  6. using System.Diagnostics;
  7. using System.Linq;
  8. using System.Web;
  9. using System.Web.UI;
  10. using System.Web.UI.WebControls;
  11.  
  12. public partial class WebUserControl : System.Web.UI.UserControl
  13. {
  14.  
  15.     public event CalculationCompletedEventHandler CalculationCompleted;
  16.     public delegate void CalculationCompletedEventHandler(object sender, TCalculationEventArgs e);
  17.  
  18.     protected void Page_Load(object sender, EventArgs e)
  19.     {
  20.     }
  21.  
  22.     protected void Button1_Click(object sender, EventArgs e)
  23.     {
  24.         Int32 Val1 = Int32.Parse(TextBox1.Text.ToString());
  25.         Int32 Val2 = Int32.Parse(TextBox2.Text.ToString());
  26.  
  27.         TextBox3.Text = (Val1 + Val2).ToString();
  28.  
  29.         if (CalculationCompleted != null) {
  30.             CalculationCompleted(this, new TCalculationEventArgs((Val1 + Val2).ToString()));
  31.         }
  32.     }
  33. }

Then in your home page, handle the event for the User Control.

**Home.aspx.cs**
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. public partial class Home : System.Web.UI.Page
  9. {
  10.     protected void Page_Load(object sender, EventArgs e)
  11.     {
  12.  
  13.     }
  14.  
  15.     private void CoolMath_CalculationCompleted(object s, TCalculationEventArgs e)
  16.     { //make sure to do the necessary hook up to add the handler (I am not great with C# syntax and events)
  17.         Label1.Text = e.Result;
  18.     }
  19. }
  20.  
-Frinny

10 8035
Expand|Select|Wrap|Line Numbers
  1. <%@ PreviousPageType VirtualPath="~/default.aspx" %>
On page written to
Expand|Select|Wrap|Line Numbers
  1. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  2.         If PreviousPage IsNot Nothing AndAlso PreviousPage.IsCrossPagePostBack Then
  3.             Label1.Text = ("Results For : " & PreviousPage.Droplist1.SelectedValue)
  4.         Else
  5.             Response.Redirect("default.aspx")
  6.  
  7.         End If
on the back end side

Hopes this helps
Jul 13 '13 #2
CroCrew
564 Expert 512MB
Thanks for the reply Dan. Can you please describe your solution and how it would work?

Thanks,
CroCrew~
Jul 13 '13 #3
In my form user pick from select combo box and is control to search database, with goes to results page .. Where the select item will read in labeltext ,here with code link to pages together , second is on second page.vb this code permits look up... I know it is not C# but maybe it will give you a ideal ..
Jul 13 '13 #4
CroCrew
564 Expert 512MB
Thanks Dan.

But if I have to hard code in the control a path to a webpage then that takes away from the control being portable.

Any other idea?
Jul 14 '13 #5
CroCrew
564 Expert 512MB
Also I am looking for a solution that will fit within the five pages that I posted above. Answers can be in VB or C#.

Thanks everyone.

CroCrew~
Jul 14 '13 #6
Frinavale
9,735 Expert Mod 8TB
CroCrew,

Just have your user control raise an event once it is finished doing it's calculation. Use a customized EventArgs to pass the result of it's calculation to the code that handles the event.

In your webpage, simply add a handler for the "CalculationCompleted" event that your user control raises. In that code, retrieve the result from the EventArgs, and display it.

One question: how does the master page fit into what you were describing?

The solution will have to be modified a bit if you want to display the result in your master page.

-Frinny
Jul 15 '13 #7
CroCrew
564 Expert 512MB
Frinavale,

Thanks for the reply. In my example the master page is only for adding another level of complexity. Nothing will get updated on the master page.

Can you provide an example of what you are suggesting for the webpage?

Thanks,
CroCrew~
Jul 15 '13 #8
Frinavale
9,735 Expert Mod 8TB
First you need your custom event args:
Expand|Select|Wrap|Line Numbers
  1. public class TCalculationEventArgs : EventArgs
  2. {
  3.   public string Result{get; set;}
  4.   public TCalculationEventArgs(string result){
  5.     this.Result = result;
  6.   }
  7. }
  8.  
Then in your WebUserControl declare a public "event" for the class that can be handled by your main window. In the button click event, do your calculation and raise the new event.

**WebUserControl.ascx.cs**
Expand|Select|Wrap|Line Numbers
  1. using Microsoft.VisualBasic;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Data;
  6. using System.Diagnostics;
  7. using System.Linq;
  8. using System.Web;
  9. using System.Web.UI;
  10. using System.Web.UI.WebControls;
  11.  
  12. public partial class WebUserControl : System.Web.UI.UserControl
  13. {
  14.  
  15.     public event CalculationCompletedEventHandler CalculationCompleted;
  16.     public delegate void CalculationCompletedEventHandler(object sender, TCalculationEventArgs e);
  17.  
  18.     protected void Page_Load(object sender, EventArgs e)
  19.     {
  20.     }
  21.  
  22.     protected void Button1_Click(object sender, EventArgs e)
  23.     {
  24.         Int32 Val1 = Int32.Parse(TextBox1.Text.ToString());
  25.         Int32 Val2 = Int32.Parse(TextBox2.Text.ToString());
  26.  
  27.         TextBox3.Text = (Val1 + Val2).ToString();
  28.  
  29.         if (CalculationCompleted != null) {
  30.             CalculationCompleted(this, new TCalculationEventArgs((Val1 + Val2).ToString()));
  31.         }
  32.     }
  33. }

Then in your home page, handle the event for the User Control.

**Home.aspx.cs**
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. public partial class Home : System.Web.UI.Page
  9. {
  10.     protected void Page_Load(object sender, EventArgs e)
  11.     {
  12.  
  13.     }
  14.  
  15.     private void CoolMath_CalculationCompleted(object s, TCalculationEventArgs e)
  16.     { //make sure to do the necessary hook up to add the handler (I am not great with C# syntax and events)
  17.         Label1.Text = e.Result;
  18.     }
  19. }
  20.  
-Frinny
Jul 15 '13 #9
CroCrew
564 Expert 512MB
Frinavale,

Thank you very much for your help. Great looking example!

Thanks,
CroCrew~
Jul 15 '13 #10
Frinavale
9,735 Expert Mod 8TB
No problem :)

-Frinny
Jul 15 '13 #11

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

Similar topics

2
by: Wee Bubba | last post by:
my user control (usercontrol1.ascx) is added dynamically into a placeholder on page_load. within usercontrol1.ascx there is a button. When a user presses this button I want the page to reload with...
1
by: Stan Sainte-Rose | last post by:
Hi guys, How to use a vb user control with a C# page. I've added the vb user control into the reference section but when I launch to the page, I get an error saying something like this (I...
1
by: Bennett Haselton | last post by:
Are there any samples of how to create a user control that uses templates, like the <ItemTemplate> and <HeaderTemplate> of the Repeater control? I tried searching for user control samples on the...
2
by: vineetbatta | last post by:
Hi all, i have a user control which allows the user to enter Name& Address in text boxes. I use the same user control in the main page... Is there a simple way of accessing the Name &...
2
by: Italian Pete | last post by:
Hi, I have a search bar as a User Control sitting on a Page. The asp web control search button has its OnClick event set to a method "Search" which sets a bool isSearch to true. When I try to...
1
by: lior | last post by:
Hello, I have a treeview control that I use as a menu & navigation control within a master page. The nodes for this control are loaded from a database. Is there any simple method of...
0
by: robgallen | last post by:
I have 2 user controls within a master page, and I would like one of them to call a function in the other. All the examples I have seen involve a page communicating with the Master page, or with...
1
by: Dinu | last post by:
How to Dynamically program User controls in a Master page from a content page Thanks
3
by: Stimp | last post by:
I have a few master page files on a large site. I want them all the share the same footer, but I don't want to re-enter the footer text on all of them. So I logically thought I could add a web...
3
by: Cirene | last post by:
When you set the value of a user control in a master page, do you have to reference it differently? Also, do they have to be given different names in the templates? For example, this is what I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.