Howto inherit from an exisiting webcontrol in VS 2005

  • Thread starter Guillaume Hanique
  • Start date
G

Guillaume Hanique

Hi,

I feel very stupid. I simply want to derive a control from
system.web.ui.webcontrols.button and use that on my webform, but I just
can't get it done. Can anyone tell me how to do that?

I can create my own usercontrol (Menu: Website > Add new item... > Web
User Control). This creates an aspx and an aspx.vb file in the root
folder, which inherits nicely from system.web.ui.usercontrol. But I want
to inherit system.web.ui.webcontrols.button. I can't change anything in
this aspx and aspx.vb file to make it inherit from the button and make
it work.

The examples I found on the internet simply create a class and derive it
from the webcontrol. But no one says if the save it in the root folder
or in the App_Code folder. They use the <@ register tag to register it
and they fill the assembly and namespace attributes. I tried a lot, but
can't find anything I can put in the assembly and namespace attributes
that makes it work.

What steps do I take in VS 2005 to add a control that is derived from
system.web.ui.webcontrols.button to the solution and use it in my only
webform Default.aspx?

Thanks so much!

Guillaume Hanique
 
V

Van den Driessche Willy

You just create a class (vb sample)

class myClass
inherits system.web.ui.webcontrols.button
end class

AFAIK you can put this class in the app_code folder. A "better" idea is to
create a webcontrol library (a DLL) and put the new control in there. In
you website project you put a reference to this webcontrol library (this
will copy it to the bin directory an VS2005 will make sure changes propagate
upon build)

To use it in your page, you can either use the @register directive in the
aspx markup, or register you control once and for all in the web.config of
your web application.


<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<system.web>
<controls>
<add
assembly="YourAssemblyName" namespace="yourNameSpace"
tagPrefix="XYZ" />
</controls>
</system.web>
</configuration>

afterwards you use
<XYZ:myClass> in your ASPX page.

Hope this helps
 
G

Guillaume Hanique

Thanks for your prompt reply.

I did create a class like that and tried to put it in de App_Code
folder. The problem is I do not know how to use the @Register directive
(which I prefer) or how to add it in web.config.

What is my assembly name and what is my namespace, or how do determine it?

I named my project ppcinstall, but
<%@ Register TagPrefix="XYZ" Assembly="ppcinstall" Namespace="ppcinstall" %>
gives the compile error that it cannot find the assembly ppcinstall.

My Projects directory (C:\Inetpub\wwwroot\ppcinstall) does not have a
dll anywhere, only vb-files, aspx-files, a web.config file, an App_Data
folder and an App_Code folder.

Of course I could create a webcontrol library, but why not have it
simply in my webproject?

If you could help me further, I would very much appreciate it.

Thanks,
Guillaume Hanique
 
G

Guillaume Hanique

Solved it.

- The file has to be stored in the App_Code folder.
- The class has to be in a namespace.
- In the register directive omit the assemblyname.

App_Code\MyButton.vb:

Namespace MyControls
Public Class MyButton
Inherits System.Web.UI.WebControls.Button

End Class
End Namespace

The register-directive will be:

<%@ Register TagPrefix="xyz" Namespace="MyControls" %>

The control can be used with:

<xyz:MyButton id="btnMine" runat="server" text="MyButton" />

Thank you for your help! Your instructions helped me to pinpoint my mistake.

Guillaume Hanique
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top