开发者

Creating custom control in asp.net

开发者 https://www.devze.com 2023-03-14 03:26 出处:网络
I want to create a custome control in asp.net like this: <my:mycontrol id=\"myid\" runat=\"server\"></my:control>

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

精彩评论

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