Answered by:
Very Basic User Control

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.
{
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