o..o
o..o

Reputation: 1921

ASP.NET - define controls in parent abstract usercontrol, but in children's markup?

I have abstract UserControlBase class inheriting from System.Web.UI.UserControl. It only class, without markup, because I it's abstract base class.

Can I somehow define controls such as TextBoxes, DropDownLists etc. in this abstract base class, use them in methods, but markup define in children's usercontrol inherited from UserControlBase?

Pseudocode:

abstract UserControlBase : System.Web.UI.UserControl
{
    private TextBox txt1;
    private DropDownList ddl1;

    private void test()
    {
          txt1.Text = "test";
          ddl1.SelectedIndex = 0;
    }
}

MyUserControl : UserControlBase
(markup):

<asp:TextBox ID="txt1" runat="server" />
<asp:DropDownList ID="ddl1" runat="server" />

Thank you.

Upvotes: 2

Views: 1251

Answers (1)

mayor
mayor

Reputation: 641

Just make the controls in base class protected, not private:

protected TextBox txt1;
protected DropDownList ddl1;

Also I suggest your UserControlBase is simple class (single *.cs file, no markup).

Upvotes: 2

Related Questions