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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • CroCrew
    Recognized Expert Contributor
    • Jan 2008
    • 564

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

    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.mas ter).
    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.ma ster**
    Code:
    <%@ 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**
    Code:
    <%@ 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* *
    Code:
    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)
        {
    
        }
    }
    **WebUserContro l.ascx**
    Code:
    <%@ 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" />
    **WebUserContro l.ascx.cs**
    Code:
    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~
  • Dan Effets
    New Member
    • Oct 2011
    • 17

    #2
    Code:
    <%@ PreviousPageType VirtualPath="~/default.aspx" %>
    On page written to
    Code:
    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

    Comment

    • CroCrew
      Recognized Expert Contributor
      • Jan 2008
      • 564

      #3
      Thanks for the reply Dan. Can you please describe your solution and how it would work?

      Thanks,
      CroCrew~

      Comment

      • Dan Effets
        New Member
        • Oct 2011
        • 17

        #4
        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 ..

        Comment

        • CroCrew
          Recognized Expert Contributor
          • Jan 2008
          • 564

          #5
          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?

          Comment

          • CroCrew
            Recognized Expert Contributor
            • Jan 2008
            • 564

            #6
            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~

            Comment

            • Frinavale
              Recognized Expert Expert
              • Oct 2006
              • 9749

              #7
              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 "CalculationCom pleted" 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
              Last edited by Frinavale; Jul 15 '13, 01:39 PM.

              Comment

              • CroCrew
                Recognized Expert Contributor
                • Jan 2008
                • 564

                #8
                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~

                Comment

                • Frinavale
                  Recognized Expert Expert
                  • Oct 2006
                  • 9749

                  #9
                  First you need your custom event args:
                  Code:
                  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.

                  **WebUserContro l.ascx.cs**
                  Code:
                  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* *
                  Code:
                  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

                  Comment

                  • CroCrew
                    Recognized Expert Contributor
                    • Jan 2008
                    • 564

                    #10
                    Frinavale,

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

                    Thanks,
                    CroCrew~

                    Comment

                    • Frinavale
                      Recognized Expert Expert
                      • Oct 2006
                      • 9749

                      #11
                      No problem :)

                      -Frinny

                      Comment

                      Working...