-1

I made a simple control with 1 text box.

    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="sTextBox.ascx.cs" Inherits="TestingASPNET.Controls.sTextBox" className="sTextBox"%>
    <asp:Textbox runat="server" ID="tbNothing"/>
    <br />

I call this control as a reference in my default.aspx Here's the simple code.

   <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="default.aspx.cs" Inherits="TestingASPNET._default" %>
   <%@ Reference Control="~/Controls/sTextBox.ascx"%>

   <html xmlns="http://www.w3.org/1999/xhtml">
   <head runat="server">
   </head>
   <body>
     <form id="form1" runat="server">
     <div>
          <asp:PlaceHolder runat="server" id="PlaceHolder1" />
     </div>
     </form>
  </body>
  </html>

In my code behind in default.aspx.cs I have.

   protected void Page_Load(object sender, EventArgs e)
    {
        PlaceHolder1.Controls.Add(LoadControl("~/Controls/sTextBox.ascx"));
        PlaceHolder1.Controls.Add(LoadControl("~/Controls/sTextBox.ascx"));
    }

This adds the 2 sTextBox onto my page.

The problem I'm having is how to I use the control like I would a normal textBox. For example.

   TextBox tb = new TextBox();
   tb.Text = "textbox";
   PlaceHolder1.Controls.Add(tb);

This adds a text box on the page with the text "textbox" in it.

Can Someone give me a way to do EXACTLY this, but with the control sTextBox.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
jderridew
  • 1
  • 2

2 Answers2

0

you can get that behavior by adding properties to your custom control.

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        var ctrl = (sTextBox) Page.LoadControl("~/sTextBox.ascx");
        ctrl.Text = "something";
        placeHolder1.Controls.Add(ctrl);
    }
}

User Control :-

public partial class sTextBox : System.Web.UI.UserControl
{
    public string Text { get; set; }
}
Raj C
  • 628
  • 5
  • 6
  • 1
    I tried this The problem I'm getting it under 'sTextBox' there is an error saying 'The type or namespace could not be found.' That must be the main error. I can't even make an sTextBox object in the code let alone typecast it. – jderridew Sep 11 '14 at 00:29
0

I couldn't get your code to work.

I either had to

var ctrl = (ProjectName.Controls.sTextBox) Page.LoadControl("~/Controls/sTextBox.ascx");

or import the control

 using ProjectName.Controls;

When I did this, it worked.

Also your get set property wasn't work either, I had to change it to.

 public string Text { 
        get
        {
            return tbNothing.Text;
        }
        set
        {
            tbNothing.Text = value;
        }
    }

Afterwards I added 1 more textbox into the control totaling 2. I changed the ID to tb1Text and tb2Text. I then had to get 2 methods for my get sets, which was

   public string tb1Text { 
        get
        {
            return tb1.Text;
        }
        set
        {
            tb1.Text = value;
        }
    }

    public string tb2Text
    {
        get
        {
            return tb2.Text;
        }
        set
        {
            tb2.Text = value;
        }
    }

inside my default code behind, I had to use

        sTextBox ctrl = (sTextBox)Page.LoadControl("~/Controls/sTextBox.ascx");
        ctrl.tb1Text = "something";
        ctrl.tb2Text = "something 2";
        PlaceHolder1.Controls.Add(ctrl);

This worked, now I know how to use 2 textboxes on 1 control :) . Hopefully it's the same with other controls that I have to make :S

jderridew
  • 1
  • 2
  • Awesome !!. The quick sample code I provided had the same namespace for the control & the default page. (which is not the case for you). Also, auto properties are introduced in C# 3.0 (not sure which version you are using) but if you are using C# 3.0 and above you can use get; set; Happy Programming :) – Raj C Sep 11 '14 at 19:03
  • Thanks. Also are you sure you can use get; set;? I'm using framework 4.0. Did I do something wrong above? – jderridew Sep 11 '14 at 20:06
  • What error did you get when you used auto properties. Also, I recommend to check this to get an overview http://stackoverflow.com/questions/6001917/what-are-automatic-properties-in-c-sharp-and-what-is-their-purpose – Raj C Sep 11 '14 at 20:10
  • I didn't get an error. I just didn't get the word "something" in the Textbox until I changed it to my code above. Also if there's 2 textbox in that control, how does the auto property know which text to get or set. – jderridew Sep 11 '14 at 20:20