I am trying to make a simple user controlthat 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.
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.
brad_bondi
0 Points
17 Posts
Very Basic User Control
May 07, 2008 09:48 PM|LINK
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<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <%@ Register src="WebUserControl.ascx" tagname="WebUserControl" tagprefix="uc1" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title>Untitled Page</title></head><body> <form id="form1" runat="server"> <div> <asp:TextBox ID="TextBox1" runat="server" text="A"> </asp:TextBox> <br /> <asp:TextBox ID="TextBox2" runat="server" text="B"> </asp:TextBox> <br /> <br /> <uc1:WebUserControl ID="WebUserControl1" runat="server" TextBox1='???' TextBox2='???' /> </div> </form></body></html> WebUserControl.ascx<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %><asp:TextBox ID="TextBox3" runat="server"></asp:TextBox> WebUserControl.ascx.csusing System;using System.Collections;using System.Configuration;using System.Data;using System.Linq;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Xml.Linq; public partial class WebUserControl : System.Web.UI.UserControl{
public string TextBox1; public string TextBox2; protected void Page_Load(object sender, EventArgs e) { TextBox3.Text = TextBox1 + " + " + TextBox2;}
}
gbogea
Contributor
4019 Points
576 Posts
Re: Very Basic User Control
May 08, 2008 12:33 AM|LINK
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?
-----------------
Please 'Mark as Answer' the post(s) that helped you
brad_bondi
0 Points
17 Posts
Re: Very Basic User Control
May 09, 2008 07:40 AM|LINK
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
gbogea
Contributor
4019 Points
576 Posts
Re: Very Basic User Control
May 09, 2008 01:08 PM|LINK
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!
-----------------
Please 'Mark as Answer' the post(s) that helped you