开发者

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, I开发者_StackOverflow社区NamingContainer {}

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>


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜