简体   繁体   中英

Creating custom control in asp.net

I want to create a custome control in asp.net like this:

<my:mycontrol id="myid" runat="server"></my:control>

I have created a class like this:

public class mycontrol : Control, INamingContainer {}

but how can i use it like i mentioned above How can I recreate it so that I can declare it as I mentioned above?

You need to register the user control at top of page or in web.config

<%@ Register TagPrefix="my" TagName="mycontrol" Src="~/usercontrols/mycontrol.ascx" %>

for web.config (this means you can use it on any page without re-registering everytime, just add to your pages/controls section in system.web

<?xml version="1.0"?>

<configuration>
  <system.web>
    <pages>
      <controls>
        <add tagPrefix="my" src="~/usercontrols/mycontrol.ascx" tagName="mycontrol"/>
      </controls>
    </pages>
  </system.web>
</configuration>

Once you have created your control, in an ASPX page, have this at the top somewhere:

<%@ Register TagPrefix="my" TagName="mycontrol" Src="Controls/mycontrol.ascx" %>

Make sure the Src path to the ASCX file is correct and you should be OK.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM