locked
Very Basic User Control RRS feed

  • Question

  • User1858242605 posted

    I am trying to make a simple user control  that uses two text boxes located on the default.aspx to populate a third text box on the user control. I cannot work out what code to use to assign the values in the default.aspx textboxes to the user control statement. See the ??? below as this is where the code should go.

     

    Default.aspx<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p><%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %><o:p> </o:p><%@ Register src="WebUserControl.ascx" tagname="WebUserControl" tagprefix="uc1" %><o:p> </o:p><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><o:p></o:p><html xmlns="http://www.w3.org/1999/xhtml"><o:p></o:p><head runat="server"><o:p></o:p>    <title>Untitled Page</title><o:p></o:p></head><o:p></o:p><body><o:p></o:p>    <form id="form1" runat="server"><o:p></o:p>    <div><o:p></o:p>        <asp:TextBox <o:p></o:p>                  ID="TextBox1" runat="server"  text="A"><o:p></o:p>            </asp:TextBox><o:p></o:p>            <br /><o:p></o:p>      <asp:TextBox <o:p></o:p>            ID="TextBox2" runat="server" text="B"><o:p></o:p>      </asp:TextBox><o:p></o:p>            <br /><o:p></o:p>            <br /><o:p></o:p>            <uc1:WebUserControl <o:p></o:p>                  ID="WebUserControl1" runat="server" <o:p></o:p>                  TextBox1='???'<o:p></o:p>                  TextBox2='???' /><o:p></o:p>    </div><o:p></o:p>    </form><o:p></o:p></body><o:p></o:p></html><o:p></o:p><o:p> </o:p>WebUserControl.ascx<o:p></o:p><%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %><o:p></o:p><asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><o:p></o:p><o:p> </o:p>WebUserControl.ascx.cs<o:p></o:p>using System;<o:p></o:p>using System.Collections;<o:p></o:p>using System.Configuration;<o:p></o:p>using System.Data;<o:p></o:p>using System.Linq;<o:p></o:p>using System.Web;<o:p></o:p>using System.Web.Security;<o:p></o:p>using System.Web.UI;<o:p></o:p>using System.Web.UI.HtmlControls;<o:p></o:p>using System.Web.UI.WebControls;<o:p></o:p>using System.Web.UI.WebControls.WebParts;<o:p></o:p>using System.Xml.Linq;<o:p></o:p><o:p> </o:p>public partial class WebUserControl : System.Web.UI.UserControl<o:p></o:p>

    {

          public string TextBox1;<o:p></o:p>      public string TextBox2;<o:p></o:p><o:p> </o:p>      protected void Page_Load(object sender, EventArgs e)<o:p></o:p>    {<o:p></o:p>            TextBox3.Text = TextBox1 + " + " + TextBox2;<o:p></o:p>

        }

    }

    Wednesday, May 7, 2008 5:48 PM

Answers

  • User853727733 posted

     Here's the code for the page:

        <form id="form1" runat="server">
        <div>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
            <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
            <br />
            <uc1:WUCSimple ID="WUCSimple1" runat="server" />
        </div>
        </form>

    and it's code behind:

    public partial class PageWUCSimple : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            WUCSimple1.Text1 = TextBox1.Text;
            WUCSimple1.Text2 = TextBox2.Text;
            Button1.Click += WUCSimple1.ButtonClick;
        }
    }

    And here's the WebControl:

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="WUCSimple.ascx.cs" Inherits="WUCSimple" %>
    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>

    and it's code behind:

    public partial class WUCSimple : System.Web.UI.UserControl
    {
        public string Text1 { get; set; }
        public string Text2 { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
          
        }

        public void ButtonClick(object sender, EventArgs e)
        {
            TextBox3.Text = Text1 + Text2;
        }
    }

    Hope this helps you. Enjoy! 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 9, 2008 9:08 AM

All replies

  • User853727733 posted

    You declare have to declare the textboxes as properties, like this:

        public string Text1 { get; set; }
        public string Text2 { get; set; }

     Then on the PageLoad of the Default page you can do:

    WebUserControl.Text1 = TextBox1.Text;

    WebUserControl.Text2 = TextBox2.Text;
     

    Is this what you were looking for? 

    Wednesday, May 7, 2008 8:33 PM
  • User1858242605 posted

    Gabriel,

    Yep...that perfect.

    One other question: How much needs to be added to enable text to be entered into TextBox1 & 2 and then a button click to show in the user control (TextBox3). Struggling to do this.

     Also have a hassle with the double refresh OnClick. Seems to be a few solutions but complex to implement.

    Thanks for you assistance.

    Brad

    Friday, May 9, 2008 3:40 AM
  • User853727733 posted

     Here's the code for the page:

        <form id="form1" runat="server">
        <div>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
            <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
            <br />
            <uc1:WUCSimple ID="WUCSimple1" runat="server" />
        </div>
        </form>

    and it's code behind:

    public partial class PageWUCSimple : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            WUCSimple1.Text1 = TextBox1.Text;
            WUCSimple1.Text2 = TextBox2.Text;
            Button1.Click += WUCSimple1.ButtonClick;
        }
    }

    And here's the WebControl:

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="WUCSimple.ascx.cs" Inherits="WUCSimple" %>
    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>

    and it's code behind:

    public partial class WUCSimple : System.Web.UI.UserControl
    {
        public string Text1 { get; set; }
        public string Text2 { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
          
        }

        public void ButtonClick(object sender, EventArgs e)
        {
            TextBox3.Text = Text1 + Text2;
        }
    }

    Hope this helps you. Enjoy! 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 9, 2008 9:08 AM