locked
Ho to inherit a webuser control from another webuser control RRS feed

  • Question

  • User-388731848 posted

    I wantto inherit a webuser control from another webuser control and in the base webuser control i should have some functions which will be inherited by the other webuser control. How could i do that? 

    Thanks in advance.

    Thursday, April 23, 2009 2:43 AM

Answers

All replies

  • User2130758966 posted

     Depending on what you want - you could put the master user control inside the child user control and let it maniuplate it that way.

    Thursday, April 23, 2009 3:14 AM
  • User-388731848 posted

    Well actually i need to use some properties, methods and fields of base webusercontrol on several othe child webusercontrols. And i want the child webusercontrols to extend to the base websuercontrol just like polymorphism. And i don't wantto drop base webusercontrol onto chiled ones each time.

    Thursday, April 23, 2009 3:55 AM
  • User2130758966 posted

     Try these resources:


    Or depending on what you are attempting you might find it easier to just put this standard functionality in a helper class that you can call from any usercontrol?
    Thursday, April 23, 2009 7:20 PM
  • User-388731848 posted

    Thanks, however all those links are not really relevant to what i am looking for. The followings are wht i have done so far:

    Base WebUserControl's code behind is:

    public partial class WebUserControlBase : System.Web.UI.UserControl

    {

    //protected void Page_Load(object sender, EventArgs e)

    //{

    //}

    private string Name = "KEMAL";

    public string NAME { get { return Name; } set { Name = value; } }

    public string Metod(string Ad)

    {

    return Ad + " " + "ESMER";

     

    }

     

    }

    Base WebUserControl's HTML is:

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControlBase.ascx.cs" Inherits="WebUserControlBase" ClassName = "MyControls.UstControl" %>

    <table border="0" cellpadding="0" cellspacing="0" style="width: 536px">

    <tr>

    <td>

    </td>

    <td>

    <asp:Label ID="Label1Base" runat="server" Text="Ad-Base"></asp:Label>

    &nbsp;

    </td>

    <td>

    <asp:TextBox ID="TextBox1Base" runat="server"></asp:TextBox></td>

    </tr>

    <tr>

    <td>

    </td>

    <td>

    <asp:Label ID="Label2Base" runat="server" Text="Soyad-Base"></asp:Label></td>

    <td>

    <asp:TextBox ID="TextBox2Base" runat="server"></asp:TextBox>&nbsp;

    </td>

    </tr>

    <tr>

    <td style="height: 19px">

    </td>

    <td style="height: 19px">

    </td>

    <td style="height: 19px">

    <asp:Button ID="Button1Base" runat="server" Text="Button" /></td>

    </tr>

    </table>

     

    Code behind of the Inherited WebUserControl from the base WebUserControl above is:

     

    public partial class WebUserControlDerived : MyControls.UstControl

    {

    protected void Page_Load(object sender, EventArgs e)

    {

    //this.NAME = "Kemal";

     

    }

    protected void Button1Derived_Click(object sender, EventArgs e)

    {

    TextBox1Derived.Text =
    this.NAME;

    TextBox2Derived.Text = this.Metod(this.NAME);

    this.Calendar1.SelectedDate=DateTime.Today ;

    }

    }

     And HTML of this inherited child WebUserControl is:

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControlDerived.ascx.cs" Inherits="WebUserControlDerived" %>

    <table border="0" cellpadding="0" cellspacing="0" style="width: 536px">

    <tr>

    <td>

    </td>

    <td>

    <asp:Label ID="Label1Derived" runat="server" Text="Ad-Derived"></asp:Label>

    &nbsp;

    </td>

    <td>

    <asp:TextBox ID="TextBox1Derived" runat="server"></asp:TextBox></td>

    </tr>

    <tr>

    <td>

    </td>

    <td>

    <asp:Label ID="Label2Derived" runat="server" Text="Soyad-Derived"></asp:Label></td>

    <td>

    <asp:TextBox ID="TextBox2Derived" runat="server"></asp:TextBox></td>

    </tr>

    <tr>

    <td style="height: 19px">

    </td>

    <td style="height: 19px">

    </td>

    <td style="height: 19px">

    <asp:Button ID="Button1Derived" runat="server" Text="Button" OnClick="Button1Derived_Click" />

    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox></td>

    </tr>

    </table>

     

    The problem is that.: Although i say  "public partial class WebUserControlDerived :MyControls.UstControl" there in inherited WebUserControl's code behind, MyControls.UstControl is not recognized by .NET, thus i cannot see the properties, methods and fields of base WebUserConbtrol, when i say "this."  . Intellisense wouldn't list all the inherited metods fileds and properties etc from the base WebUserControl. I couldn't figure out why.

    Thanks...

    Friday, April 24, 2009 3:04 AM
  • User-2106054853 posted

    Hi,

    When inheriting user control the ascx could not be inherited. You have to copy&paste all the code from parent user controls's ascx to the child user control's.

     Or you can use custom control instead.

     

    Friday, April 24, 2009 3:41 AM
  • User-388731848 posted

    Hi,

    When inheriting user control the ascx could not be inherited. You have to copy&paste all the code from parent user controls's ascx to the child user control's.

     Or you can use custom control instead.

     

    Well, i already inherited the codebehind of the master WebUserControl. What do you exactly mean when you say "When inheriting user control the ascx could not be inherited".  Do you mean i cannot inherit the HTML side of the WebUserControl. And what is a custom control?

    Friday, April 24, 2009 4:07 AM
  • User-2106054853 posted

    Do you mean i cannot inherit the HTML side of the WebUserControl.

    Yes.

    And what is a custom control?

    Please refer to:

    http://support.microsoft.com/kb/893667

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 24, 2009 5:28 AM