ASP.NET Register WebControl to use in ASPX Page - asp.net

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.

Related

UserControl DirectMethod Not firing in Ext.net

HI, I am Loading one UserControl(Which has the GridPanel in it ) into
tabPanel.
But when i click on Gridpanel Edit Command Button the Event at Server
side method not working and showing the Exception like
Exception Details: System.Web.HttpException: The control with ID
'id8b177c82adb2e925' not found.
My Code here is
For the userControl : the .ascx code is
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="GridPanelUserControl.ascx.cs" Inherits="Ext_PracticeExamples.GridPanelUserControl" %>
<ext:GridPanel ID="gpEmployeeList" runat="server" StripeRows="true" Title="Employee List" Layout="FitLayout"
Width="620" Height="400" AutoExpandColumn="FirstName">
<Store>
.....
</Store>
<ColumnModel ID="ColumnModel1" runat="server">
<Columns>
<ext:ImageCommandColumn ID="imgSettings" runat="server" Text="Settings" Resizable="false">
<Commands>
<ext:ImageCommand Icon="BasketEdit" Style="text-align: center" CommandName="Settings">
</ext:ImageCommand>
</Commands>
<Listeners>
<Command Handler=" #{DirectMethods}.fnDisplaySettings(record.data.Company,record.data.Price,record.data.Change);" />
</Listeners>
</ext:ImageCommandColumn>
</Columns>
</ColumnModel>
and the Corresponding .cs Class file method which is not firing.
[DirectMethod]
public void fnDisplaySettings(string name, double X, double Y)
{
// ..some Operation....
Response.Redirect("~/_Default.aspx");
}
while Observing the Source page in webBrowser i find that the Controls have
different Id so that Corresponding function not displaying.If i give
"ClientIdMode=static" it gives me the Unique Id problem when i load
the same control in the page. If i Use this code in Single page
without UserControl it's working fine.
What should i do Invoke the server Side Method.
Thank you
I guess you render a user control on the fly during one request and doesn't recreate it during another request, therefore Ext.NET can't find its DirectMethod, because there is no user control instance on server.
Possible solutions are:
Recreate a user control during each request. Here is a related discussion on the Ext.NET forums.
Setting up a DirectMethod's handler in, for example, a WebService (asmx) or an HTTP handler (ashx), and call it via URL. Such a call won't require a user control instance. Here are examples.
http://examples.ext.net/#/Events/DirectMethods/WebService/
http://examples.ext.net/#/Events/DirectEvents/WebService/
The disadvantage of this approach is the fact that you won't be able to access a user control's controls in a WebService or an HTTP handler. All required things should be submitted from a page as extra parameters of a request.
Placing a DirectMethod to the page.

Where are my Menu Items coming from?

I've taken over this website with no documentation (Yay!). Maybe this will help others to understand the SiteMap functionality. I have an idea how the SiteMenu is being built, but want to check with my buddies here at SO to be sure.
I have a Main.Master which calls for a SiteMenuHorizontal User Control.
<%# Register Src="uc2Menu.ascx" TagPrefix="uc2" TagName="smHorizontal" %>
...
<uc2:smHorizontal id="uc2Menu" runat="server" />
The User Control is pretty simple:
<%# Control Language="vb" AutoEventWireup="false" CodeBehind="uc2Menu.ascx.vb" Inherits="uc2Menu" %>
<asp:Menu id="siteMenu" runat="server" DataSourceID="menuSiteMapDataSource" Orientation="Horizontal" />
<asp:SiteMapDataSource ID="menuSiteMapDataSource" runat="server" ShowStartingNode="false" />
No help from the control's codebehind:
Public Partial Class uc2Menu
Inherits System.Web.UI.UserControl
Protected Sub siteMenu_MenuItemDataBound(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.MenuEventArgs) Handles siteMenu.MenuItemDataBound
e.Item.Target = CType(e.Item.DataItem, SiteMapNode)("Target")
End Sub
End Class
The Web.Config DOES specify a SiteMap:
<siteMap enabled="true" defaultProvider="objSiteMap">
<providers>
<clear/>
<add name="objSiteMap" type="LOC.DLL.objSiteMap" securityTrimmingEnabled="true" expirationInterval="1"/>
</providers>
</siteMap>
LOC.DLL.objSiteMap inherits SiteMapProvider and has overloads/overrides etc. The methods grab a table from a DB and build a SiteMapNode, or SiteMapNodeCollection.
My confusion is: How is this class populating my web page? There are no visible calls to the SiteMap.
My assumption is this:
When the site (or page) is built (or compiled?), my User Control calls its Datasource, Global.System.Web.UI.WebControls.SiteMapDataSource. Somehow, that control references the Web.Config for a reference to LOC.DLL.objSiteMap, and runs the Override/Overload methods to retrieve a SiteMapNode(/Collection), and return it back to my control?
I have a feeling I've got it down, but wanted to get a confirmation and/or clarification, and hopefully help others in the future who take over projects with dynamic site menus.
How is this class populating my web page? There are no visible calls
to the SiteMap.
SiteMapProvider is based on ASP.Net's Provider model, similar to Membership and Role providers.
My assumption is this: When the site (or page) is built (or
compiled?), my User Control calls its Datasource,
Global.System.Web.UI.WebControls. SiteMapDataSource. Somehow, that
control references the Web.Config for a reference to
LOC.DLL.objSiteMap, and runs the Override/Overload methods to retrieve
a SiteMapNode(/Collection), and return it back to my control?
Yes, your assumption is correct.
Your user control calls the default site map provider objSiteMap, and the sitemap is created on every page request dynamically.
FYI: Since you have set objSiteMap as the defaultprovider, you do not need to do anything. However, if you want a provider other than the default, you'll need to set a provider name explicitly.
<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server"
SiteMapProvider="AnotherProvider" />
You're pretty much right on the money with your analysis.
The main connection here that I think you're missing is that the web.config value defaultProvider="objSiteMap" is what causes the SiteMapDataSource in your UserControl to connect to the "objSiteMap" provider in your web.config.
Since it is set as the "default provider", any SiteMap's in your project will be populated using that LOC.DLL.objSiteMap provider (unless you specifically override the default in the code).

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

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

Resources