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. - I want a user control (WebUserControl.ascx) on a web page (Home.aspx) that uses a master page (MasterPage.master).
- 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).
- 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** -
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>
-
<!DOCTYPE html>
-
<html xmlns="http://www.w3.org/1999/xhtml">
-
<head runat="server">
-
<title></title>
-
<asp:ContentPlaceHolder id="head" runat="server">
-
</asp:ContentPlaceHolder>
-
</head>
-
<body>
-
<form id="form1" runat="server">
-
<div>
-
Add it up!<br />
-
<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
-
-
</asp:ContentPlaceHolder>
-
</div>
-
</form>
-
</body>
-
</html>
-
**Home.aspx** -
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Home.aspx.cs" Inherits="Home" %>
-
<%@ Register TagPrefix="Math" TagName="AddControl" Src="~/WebUserControl.ascx" %>
-
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
-
<asp:Label ID="Label1" runat="server" Text="Label">Display the value from control here!</asp:Label><br />
-
<Math:AddControl ID="CoolMath" runat="server" />
-
</asp:Content>
-
**Home.aspx.cs** -
using System;
-
using System.Collections.Generic;
-
using System.Linq;
-
using System.Web;
-
using System.Web.UI;
-
using System.Web.UI.WebControls;
-
-
public partial class Home : System.Web.UI.Page
-
{
-
protected void Page_Load(object sender, EventArgs e)
-
{
-
-
}
-
}
-
**WebUserControl.ascx** -
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>
-
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
-
<asp:TextBox ID="TextBox1" runat="server" /> +
-
<asp:TextBox ID="TextBox2" runat="server" /> =
-
<asp:TextBox ID="TextBox3" runat="server" />
-
**WebUserControl.ascx.cs** -
using System;
-
using System.Collections.Generic;
-
using System.Linq;
-
using System.Web;
-
using System.Web.UI;
-
using System.Web.UI.WebControls;
-
-
public partial class WebUserControl : System.Web.UI.UserControl
-
{
-
protected void Page_Load(object sender, EventArgs e)
-
{
-
-
}
-
-
protected void Button1_Click(object sender, EventArgs e)
-
{
-
Int32 Val1 = Int32.Parse(TextBox1.Text.ToString());
-
Int32 Val2 = Int32.Parse(TextBox2.Text.ToString());
-
-
TextBox3.Text = (Val1 + Val2).ToString();
-
}
-
}
-
Please help and thanks you!
CroCrew~
First you need your custom event args: -
public class TCalculationEventArgs : EventArgs
-
{
-
public string Result{get; set;}
-
public TCalculationEventArgs(string result){
-
this.Result = result;
-
}
-
}
-
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** -
using Microsoft.VisualBasic;
-
using System;
-
using System.Collections;
-
using System.Collections.Generic;
-
using System.Data;
-
using System.Diagnostics;
-
using System.Linq;
-
using System.Web;
-
using System.Web.UI;
-
using System.Web.UI.WebControls;
-
-
public partial class WebUserControl : System.Web.UI.UserControl
-
{
-
-
public event CalculationCompletedEventHandler CalculationCompleted;
-
public delegate void CalculationCompletedEventHandler(object sender, TCalculationEventArgs e);
-
-
protected void Page_Load(object sender, EventArgs e)
-
{
-
}
-
-
protected void Button1_Click(object sender, EventArgs e)
-
{
-
Int32 Val1 = Int32.Parse(TextBox1.Text.ToString());
-
Int32 Val2 = Int32.Parse(TextBox2.Text.ToString());
-
-
TextBox3.Text = (Val1 + Val2).ToString();
-
-
if (CalculationCompleted != null) {
-
CalculationCompleted(this, new TCalculationEventArgs((Val1 + Val2).ToString()));
-
}
-
}
-
}
Then in your home page, handle the event for the User Control.
**Home.aspx.cs** -
using System;
-
using System.Collections.Generic;
-
using System.Linq;
-
using System.Web;
-
using System.Web.UI;
-
using System.Web.UI.WebControls;
-
-
public partial class Home : System.Web.UI.Page
-
{
-
protected void Page_Load(object sender, EventArgs e)
-
{
-
-
}
-
-
private void CoolMath_CalculationCompleted(object s, TCalculationEventArgs e)
-
{ //make sure to do the necessary hook up to add the handler (I am not great with C# syntax and events)
-
Label1.Text = e.Result;
-
}
-
}
-
-Frinny
10 8035 - <%@ PreviousPageType VirtualPath="~/default.aspx" %>
On page written to - Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
-
If PreviousPage IsNot Nothing AndAlso PreviousPage.IsCrossPagePostBack Then
-
Label1.Text = ("Results For : " & PreviousPage.Droplist1.SelectedValue)
-
Else
-
Response.Redirect("default.aspx")
-
-
End If
on the back end side
Hopes this helps
Thanks for the reply Dan. Can you please describe your solution and how it would work?
Thanks,
CroCrew~
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 ..
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?
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~
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
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~
First you need your custom event args: -
public class TCalculationEventArgs : EventArgs
-
{
-
public string Result{get; set;}
-
public TCalculationEventArgs(string result){
-
this.Result = result;
-
}
-
}
-
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** -
using Microsoft.VisualBasic;
-
using System;
-
using System.Collections;
-
using System.Collections.Generic;
-
using System.Data;
-
using System.Diagnostics;
-
using System.Linq;
-
using System.Web;
-
using System.Web.UI;
-
using System.Web.UI.WebControls;
-
-
public partial class WebUserControl : System.Web.UI.UserControl
-
{
-
-
public event CalculationCompletedEventHandler CalculationCompleted;
-
public delegate void CalculationCompletedEventHandler(object sender, TCalculationEventArgs e);
-
-
protected void Page_Load(object sender, EventArgs e)
-
{
-
}
-
-
protected void Button1_Click(object sender, EventArgs e)
-
{
-
Int32 Val1 = Int32.Parse(TextBox1.Text.ToString());
-
Int32 Val2 = Int32.Parse(TextBox2.Text.ToString());
-
-
TextBox3.Text = (Val1 + Val2).ToString();
-
-
if (CalculationCompleted != null) {
-
CalculationCompleted(this, new TCalculationEventArgs((Val1 + Val2).ToString()));
-
}
-
}
-
}
Then in your home page, handle the event for the User Control.
**Home.aspx.cs** -
using System;
-
using System.Collections.Generic;
-
using System.Linq;
-
using System.Web;
-
using System.Web.UI;
-
using System.Web.UI.WebControls;
-
-
public partial class Home : System.Web.UI.Page
-
{
-
protected void Page_Load(object sender, EventArgs e)
-
{
-
-
}
-
-
private void CoolMath_CalculationCompleted(object s, TCalculationEventArgs e)
-
{ //make sure to do the necessary hook up to add the handler (I am not great with C# syntax and events)
-
Label1.Text = e.Result;
-
}
-
}
-
-Frinny
Frinavale,
Thank you very much for your help. Great looking example!
Thanks,
CroCrew~
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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 &...
|
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...
|
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...
|
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...
|
by: Dinu |
last post by:
How to Dynamically program User controls in a Master page from a
content page
Thanks
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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,...
|
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...
|
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: 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...
|
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...
|
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,...
| |