Cannot see new asp.net user controls - asp.net

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" CodeFile="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).

Related

ASP.NET Register WebControl to use in ASPX Page

I have created a WebControl VideoStreaming for video streaming using following link:
Video Streaming with ASPNET SignalR and Html5
VideoStreaming.vb
<Assembly: OwinStartup(GetType(VideoStreaming))>
Namespace Lantern.Demo.Client.CustomControl
Public Class VideoStreaming
Inherits WebControl
'.... All code here
End Class
End Namespace
After writing all code, I declared/added/registered this control in my Web.config file as:
<pages>
<controls>
<add assembly="VideoStreaming" tagPrefix="Web" namespace="Lantern.Demo.Client.CustomControl"/>
</controls>
</pages>
But I am not able to add it in ASPX page as a regular control. This is what I am trying:
<web:VideoStreaming runat="server" ID="video" ClientIDMode="Static" Width="300px" Height="300px"
Interval="100" Source="True" ScalingMode="TargetSize" StreamingMode="Target" TargetClientID="received" OnStreamed="onStreamed"
Style="border: solid 1px black" />
This highlights the following Error:
Element 'VideoStreaming' is not a known element. This can occur if there is a compilation error in Website, or the Web.config file is missing.
I have also tried registering the control using <%Register%> directory.
<%# Register Namespace="Lantern.Demo.Client.CustomControl" TagPrefix="Web" Assembly="VideoStreaming"%>
But this is also not working. What I am missing or what I have to do?
I have seen following links:
How to register User Control and Custom Control
Registering Custom Controls Fail
Please let me know if more information is required. Thanks
EDIT: Just to let you know that this is not an .ascx control. It is WebControl which has no UI.
I am using Visual Studio 2012.

How to disable ViewState for a .aspx page?

I need to completely disable ViewState for a .aspx page inside my web application. I have gone through different blogs and what I understand is that we must set <%# Page EnableViewState="false" ...%>, but this is not working.
Is this enough to disable ViewState for all the controls inside the page? Or should I make any additional modifications? If so, please specify them. I don't want the ViewState enabled for even a single control inside the .aspx page
Disabling View State
Machine Level -
Disabling view state at machine level in machine.config, will disable ViewState of all the applications on the web server.
<Machine.config>
<system.web>
<pages enableViewState="false" />
</system.web>
</Machine.config>
Application Level -
You can disable ViewState for all pages in /web.config file.
<configuration>
<system.web>
<pages enableViewState="false" />
</system.web>
</configuration>
Page Level -
Disabling view state for a specific aspx file at the top.
<%# Page Language="C#" .. EnableViewState="false" .. %>
Control Level - You can disable ViewState for a specific control.
<asp:TextBox EnableViewState="false" ID="Name"
runat="server"></asp:TextBox>
I think the quotes should be:
EnableViewState="false"
Apart from that, if you are still seeing the hidden fields then they are used by ASP.Net. You may see:
Page.EnableViewState Property
Even if EnableViewState is false, the page might contain a hidden view
state field that is used by ASP.NET to detect a postback.
If you truly don't need postback, you can remove the form element from your page, this will remove the viewstate entirely.

ASP.NET error: The page Y.ascx cannot use the user control X.ascx

I am getting the error below when trying to build the web site project in Visual Studio 2010:
The page '/WebSite/controls/C2.ascx' cannot use the user control '/WebSite/controls/C1.ascx', because it is registered in web.config and lives in the same directory as the page.
I have 2 web user controls:
controls/C1.ascx
controls/C2.ascx
The controls have been registered in web.config:
<configuration>
<system.web>
<pages>
<controls>
<add src="~/controls/C1.ascx" tagPrefix="my" tagName="C1"/>
<add src="~/controls/C2.ascx" tagPrefix="my" tagName="C2"/>
</controls>
</pages>
</system.web>
</configuration>
C1.ascx contains just a static HTML, C2.ascx is trying to include C1:
C1.ascx contains just some plain static simple HTML. C2.ascx is trying to include C1.ascx:
<%# Control Language="VB" %>
<my:C1 runat="server" />
<p>Hello from C2</p>
When trying to build the project, I am getting the error message at the top. I realise this issue can be fixed by adding another Register directive to C2.ascx...:
<%# Register Src="~/controls/C1.ascx" TagPrefix="ctl" TagName="C1" %>
...but I'm wondering if there's a cleaner solution and why am I getting the error in the first place?
Your only possible solutions are to:
Move the control out of the directory its currently sharing with outer.ascx, or
Re-register the control inside of the outer.ascx like you already mentioned
Re-write them in code as controls in a separate library
I personally think moving is the easiest, if it will work for your solutions. Second would be re-registering, even though annoying. Abstracting them out into a full code library is probably not worth the effort if this is the only reason you are doing it.
You could also put the controls into different folders. But I don't think this is much cleaner or better.
BTW: this behavior is by design, as you can read on this MSDN page (look for the yellow note almost at the end of the page).

Change UserControl template at runtime

Is it possible to change the ascx file used by a usercontrol at runtime?
For example in my page i have
<ctl:SampleControl runat="server" />
in the web.config I have
<controls>
<add tagPrefix="ctl" tagName="SampleControl" src="~/UserControls/SampleControl.ascx" />
</controls>
At runtime I would like to be able to change the ascx to another path, it would still be inheriting from the same usercontrol codebehind, it would just be a different template.
Probably the best thing to do here is to not encode the filename of the "default" .ascx file in your web.config. It will make your life harder. Always do that determination at runtime, like this for instance:
In your .aspx file:
<asp:PlaceHolder runat="server" ID="samplePH" />
In the code-behind:
string file = "~/UserControls/SampleControl.ascx";
if (condition)
file = "~/UserControls/OtherControl.ascx";
UserControl uc = (UserControl)LoadControl(file); // from System.Web.UI.TemplateControl.
samplePH.Controls.Clear();
samplePH.Controls.Add(uc);
But, be aware that in order for post-backs to work correctly, you need to instantiate the same control that was loaded on the last request, early in the page lifecycle -- typically the Init stage. This will ensure that viewstate is correctly parsed. Then, further down in your event handler, PreRender, etc. lifecycle steps, you can use the above code to load the UserControl for the current request.
If you really do need to encode a default page setting in a configuration file (for cases where end-users may want to change it), consider doing it in an app.config rather than buried away in a <controls> section of a web.config.
Documentation for the TemplateControl.LoadControl(string) method:
http://msdn.microsoft.com/en-us/library/system.web.ui.templatecontrol.loadcontrol.aspx

Specificying a Page directive attribute on an NHaml page

I am working on an MVC site using NHaml for the view engine.
I have a page that needs to submit HTML code as a form value and am getting the System.Web.HttpRequestValidationException thrown at me.
I want to specify the <%# Page validateRequest="false" %> so that this page will allow this data to be submitted but am unsure on how to do this with NHaml generating the pages.
Side note on this:
The editor I was using was TinyMCE and I found that it has an option for encoding the output, that way it doesn't trigger the anti-html validation.
Of course, then your value is encoded so you have to make sure to decode it at the proper time.
See http://wiki.moxiecode.com/index.php/TinyMCE:Configuration/encoding
You may try annotating your controller action with the ValidateInputAttribute:
[ValidateInput(false)]
public ActionResult Index()
{
// ...method body
}
This could also be done in the config file for the whole application:
<configuration>
<system.web>
<pages validateRequest="false" />
</system.web>
</configuration>

Resources