Cannot see new asp.net user controls
I am feeling like an idiot right now, but I cannot for the life of me figure out why I cannot seem to call user controls from within my asp.net webpage. I'm still learning asp.net but I can't find any information from searching on google.
I'm trying to load a specific control on the page when the user presses a linkbutton. So I created an empty user control via the right click menu:
<%@ Control Language="C#" AutoEventWireup="true" Co开发者_开发百科deFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>
In other words, I have not touched any part of the created user control. Yet attempting to create this web control and add it to the form seems to not work, as it claims that the WebUserControl class does not exist (I have no other controls in my project):
UserControl blah = new WebUserControls();
produces a "The type or namespace is invalid". Why can none of my asp.net webform pages get the control into scope?
The new control must be added to the site's Web.config
file.
<configuration>
<system.web>
<pages>
<controls>
<add tagPrefix="my"
tagName="WebUserControl"
src="~/WebUserControl.ascx"/>
</controls>
</pages>
</system.web>
</configuration>
Use this to place the new control in an .aspx
page.
<my:WebUserControl runat="server" ID="MyWebUserControl" />
In addition to registering them one-by-one in web.config, you can also use a <%@ Register %>
directive in the source of the markup files that reference the control.
Or, you can move your controls into a DLL and include them all by referencing the assembly from your web.config (takes some extra work, though).
精彩评论