How to change namespace name in an asp.net webform - asp.net

How to change the namespace name in an asp.net webform
For example in my project i have created 3 folders Master,Transaction,Reports in Master folder there are 10 forms by default th namespace comes ProjectName.Master,but when i manually change the name by adding my formname it doesn't work eg:-ProjectName.Master.MasterFormName.And moreover when i double click on a button then it doesn't go in the code behind.

Changing a namespace in c# code alone isn't sufficient.
You also need to tell your .master file what class it inherits from.
Set the new namespace in you .master file and you will be good to go.
<%# Master Language="C#" AutoEventWireup="true" CodeFile="ProjectName.Master.cs"
Inherits="ProjectName.Master.MasterFormName.MasterFormName" %>
Just my personal opinion: putting your class name in the namespace doesn't make a lot of sense.

Related

In an aspx file where does the actual code gets loaded?

So this code is in a file login.aspx which literally has some html mnarkup and the below code, so where does this code get loaded from? Where is the App_Web_login.aspx.d7a6dcf1 file located ?
<%# page language="C#" autoeventwireup="true" inherits="stadm_login,
App_Web_login.aspx.d7a6dcf1" enableeventvalidation="false" theme="Niko" %>
The code is contained in a file that is associated with its page. In Visual Studio if you load the project you will see a caret next to the aspx page. Click that and you will see a file associated with that page. That is the code file, or code behind, for the login page.
When the project is built, the code for each page is wrapped into a dll file or code package for the entire project. What you see for the inherits in the page is the base class for the project and also the inheriting of the page class.
So if my project is called FooProject, then at the top of each page where it says inherits, you will something like this (the _base class .page class):
inherits=_FooProject.Login

namespace of DNN's Language class

I am trying to configure internationalization for my DNN project. I have added two languages, and now I want to access DNN's Language control in order to make it visible on project's pages.
I have found a snippet
<dnn:LANGUAGE runat="server" ID="dnnLANGUAGE" ShowLinks="True" ...
and I tried to use it. Compiler does not recognize dnn: tagPrefix so I have to register it, that is, I have to pass class's namespace inside Register tag in my ascx page. But I can not find the namespace anywhere. I have also browsed www.dnnsoftware.com/dnn-api , but could not find it.
You need to add the following line at the top of your ascx page.
<%# Register TagPrefix="dnn" TagName="LANGUAGE" Src="~/Admin/Skins/Language.ascx" %>

Create a usercontrol instance programmatically in ASP.NET

I have a UserControl that I need to add dynamically. I tried to follow this MSDN article, but I'm not having any success....
http://msdn.microsoft.com/en-us/library/c0az2h86.aspx
The UserControl is basically an image gallery, and it loads some pictures based on an ID. My idea was to make this ID available as a property. Then when I create an instance of the control, I could set this ID and add it to the form.
I added a reference to the control in the .aspx page that will use it, like this:
<%# Reference Control="~/PictureGallery.ascx" %>
And in the UserControl I added a ClassName like this:
<%# Control Language="C#" AutoEventWireup="true" CodeFile="PictureGallery.ascx.cs"
Inherits="PictureGallery" ClassName="PictureGallery" %>
When I try to create an instance in the .aspx.cs like the article suggests, Dim gallery As ASP.PictureGallery, I get an "Type ASP.PictureGallery is not defined".
The article mentions a namespace, ASP, and I tried importing it to the .aspx.cs with no luck.
So, I'm not able to get a reference to the UserControl.
How can it be fixed?
It sounds like you are confusing two separate ways of working with a UserControl.
One way is to register the control on your page so that you can put it into the page at Design Time e.g.
<div>
<asp:PictureGallery id="myGallery" runat="server" MyProperty="SomeValue">
</asp:PictureGallery>
</div>
The second is programmatically (or dynamically) adding it into the page at runtime in your code behind. If so, then you need to use the LoadControl function which is mentioned in the sample. You do not need to register the control in the aspx file if you do this.
e.g.
Dim gallery as PictureGallery = LoadControl("~/PathToControl/gallery.ascx")
gallery.MyProperty = "SomeValue"
placeHolder.controls.add(gallery)
edit
What is the class name of the control in the code behind...something like this:
Partial Public Class MyControlsClassName
Inherits System.Web.UI.UserControl
That is the type you need to use when you declare it. Is it within a namespace perhaps?
I don't think you've placed the control in your code behind. You may well have created the reference, but do you have a tag such as <asp:PictureGalary id="gallary"></asp:PictureGalary> anywhere in your aspx?
The ASP namespace is generated at run time- user controls get "compiled" as they are used by .aspx pages so this is why you get the error message "Type ASP.PictureGallery is not defined".
When adding user controls dynamically you should use the Page.LoadControl method:
Page.LoadControl("~/PictureGallery.ascx")

Loading user control with <%# Register NameSpace %> won't load user control .ascx file but .ascx.cs file instead

I'm trying to load User Control from another project, And I like to do it with it's name space:
<%# Register TagPrefix="IPGostar" Namespace="IPGostarPorject" Assembly="IPGostarPorject" %>
but this way when I use this server tag:
<IPGostar:DataGrid runat="server" ID="DataGrid1"></IPGostar:DataGrid>
the Page_Load of the DataGrid.ascx.cs will load but the content of ascx file won't load.
It's like this tag only calls the cs file and not the ascx file at the first place..
In this case, is it okay to load a .ascx user control file inside a cs file? (for example on Page_Load function we render the ascx file) and if it is okay how can I do that?
Second am I on the wrong path here?
Modfiy register tag to something like
<%# Register TagPrefix="IPGostar" TagName="IPGostar" Src="DataGrid.ascx" %>
If you are using a user control you have to specfiy the Source file. Your syntax holds good for custom controls.
You need to look into loading the control using Page.LoadControl().
At runtime, the ascx inherits from the class defined in the ascx.cs. They are not the same thing. The control defined in the ascx is a subclass of that in the ascx.cs.
If you want to distribute a control entirely as a binary, you will need to rewrite it as a ServerControl rather than a UserControl with an ascx.
You can't render the ASCX from the Page_Load of the class in the code behind. That won't work how you are thinking it might.

Add custom user control to published web project

I am beginning to wonder if this is even possible. It just seems so simple.
I have a published web project. I want to add some .ascx files (with .cs & designer.cs files) to that published web site. These are simple custom user controls that access methods already part of the original application.
Question? Is it possible to just drop these in the published web project without building the entire solution? If not why?
When I drop these files in and run my application I get the error:
"Parse Error: Could not load type 'the name of my custom controls namespace'".
There is not a lot of code to show so this is all I have.
Default.aspx
<%# Page Language="C#" MasterPageFile="~/MasterPages/TwoColumn.master" AutoEventWireup="true"
Inherits="ApplicationName.Web.Default" CodeBehind="Default.aspx.cs" %>
<%# Register TagPrefix="uc1" TagName="CustomControl" Src="~/Controls/Custom/CustomControl.ascx" %>
<asp:Content ID="content" contentplaceholder="cph" runat="Server">
<uc1:CustomControl ID="cc1" runat="server" CustomProperty="Hello World" />
</asp:Content>
CustomControl.ascx
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="CustomControl.ascx.cs"
Inherits="ApplicationName.Web.Controls.Custom.CustomControl" %>
<asp:PlaceHolder ID="ph1" runat="server></asp:PlaceHolder>
CustomControl.ascx.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ApplicationName.Web.Controls.Custom
{
public partial class CustomControl : System.Web.UI.UserControl
{
///My logic
}
}
Again it seems so easy. What am I missing? Or is this not possible? Thanks.
UPDATE:
I figured this out. The above scenario is possible. The problem is not with the name-space as the error message suggests. Rather it is the code-behind declaration. Code-behind files for any type of file are compiled when the application is published. I am still confused as to why it appears to be editable when you browse through a web directory, I would think it would be stored in a .dll file or something. Maybe someone can shed some light on this.
Anyways, replacing code-behind with code-file rectifies the problem as code-files are not compiled and are therefore readable at application run-time.
Some links that were helpful can be found here and here.
It is possible but you still have to compile your user control and drop that dll into the proper bin directory for your app. That's usually the cause of the type loading error you described.
This approach could be sloppy, you are basically either
1) Creating the User Control in the wrong project
2) Trying to add the same User Control to two projects
Have you thought about a cleaner approach and just creating a class that inherits from System.Web.Ui.Control and then adding this in a .common project? Then pulling this into the corrct project? The problem with your approach is on precompilation and deployment you could end up trying to put two user controls into the same folder which will break the build....
The alternate approach (and the microsoft way) would be like this...
The code - write a custom control
namespace MyProject.Common.Controls
{
public class PolicyTab : System.Web.UI.Control
{
protected override void CreateChildControls()
{
base.CreateChildControls();
HtmlGenericControl policyTab = new HtmlGenericControl();
policyTab.InnerHtml = "<strong> Some policy code here! </strong>";
this.Controls.Add(policyTab);
}
}
}
The page reference - how to reference it in your UI project
<CommonControls:PolicyTab runat="server" ID="temp"></CommonControls:PolicyTab>
Web.config - what you need to import this control into all of your UI pages
<add tagPrefix="CommonControls" namespace="MyProject.Common.Controls" assembly="MyProject.Common"/>

Resources