0

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?

3 Answers 3

1

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>
Sign up to request clarification or add additional context in comments.

1 Comment

Ahh, forgot about the web.config way of doing this globally. Good call.
0

Refer to How to: Include a User Control in an ASP.NET Web Page.

Comments

0

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.

1 Comment

Do I need to make it ascx or cs will also work ? I am creating control in code behing using C#

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.