How to add namespace in aspx file? - asp.net

i have added C# code in aspx file, but it is showing error
The type or namespace name 'Mail' does not exist in the class or
namespace 'System.Net' (are you missing an assembly reference?)
How i can add nampespace to aspx file i have tried <%# import namespace="Westwind.Tools"%> but it does not work?

<%# Import Namespace="System.Net.Mail" %>

I'm assuming that it's in a website and that the page doesn't have code behind?
<%# Page Language="C#" %>
<%# Import Namespace="System.Net.Mail"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
System.Net.Mail.SmtpClient client = new SmtpClient();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
</body>
</html>
This seems to work for me.
If it's not in a website and/or it has code behind why do you need to reference the namespace in the aspx file?
Hope this helps

To add the namespace globally instead of page-by-page, just put the namespace in your web.config.
<configuration>
<system.web>
<pages>
<namespaces>
<add namespace="Your.Namespace"/>
</namespaces>
</pages>
</system.web>
</configuration>
You might have to restart Visual Studio for the IntelliSense to kick in.
You can also create a mini web.config in a directory to only import the namespace into ASPX files within that directory and sub-directories instead of applying it globally.

You can import and create an optional alias for the namespace:
<%# Import Namespace="SP=Microsoft.SharePoint.Client" %>
and then use the alias like:
<%= var spContext = new SP.ClientContext("someSharePointUrl"); %>

Related

Referencing a codebehind function from an ASPX page with a Namespace

I have a very unusual problem and would appreciate help. I have a web application in VS 2019.
I am trying to create a page that runs in its own namespace so I can share it with other websites. Below is a simple sample page and code.
<%# Page Language="vb" AutoEventWireup="false" CodeBehind="test1.aspx.vb" Inherits="xxxx.test1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<%= Myfunc1() %>
</div>
</form>
</body>
</html>
Namespace xxxx
Public Class test1
Inherits System.Web.UI.Page
Public Function Myfunc1() As String
Myfunc1 = "Hello"
End Function
End Class
End Namespace
The compilation shows <%= Myfunc1() %> as MyFunc1 not found.
If I remove the "Namespace xxxx" from the code behind and change the Inherits="xxxx.test1" to Inherits "TestWebsite.test1" (TestWebsite is the namespace for the web project and the default setting when you add the test page). This code compiles and works OK.
I would appreciate advice on how I can achive my aim of making the namespace for the page independent of the website project.
Thanks
Paul

Simple ASP.NET with VB.NET - Could not load type

I am a c# guy but I need to get a small vb.net test harness working. For the life of me I can't get this to work, and can't discover why. Here's the aspx:
<%# Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Source Page</title>
</head>
<body>
<form id="form1" runat="server">
</form>
</body>
</html>
And here is the .vb:
Public Class _Default
Inherits System.Web.UI.Page
End Class
Always the error "Could not load type '_Default'." I even turned the folder into a true Application in IIS but it made no difference. What am I missing?
What am I missing?
Try the below two -
If your code-behind contains a namespace then modify your inherits attribute in Page directive as Inherits="namespace._Default".
If it doesn't, simply remove this Inherits attribute from Page Directive.
Changing the Codebehind attributename to CodeFile worked for me:
vb
CodeFile="Default.aspx.vb"
C#
CodeFile="Default.aspx.cs"

ASP.NET Cache contains solution path

Why does my Cache object contain a single entry containing the following? Shouldn't it be empty? And if not, obviously there is this ugly default value in the Cache object...how do I remove it? I haven't added anything to the Cache object, yet it contains this item below.
__System.Web.WebPages.Deployment__C:\Users\Danny\Documents\Visual Studio 2010\Projects\LearningAOP\WebAppCachingII\WebAppCachingII\***C:\Users\Danny\Documents\Visual Studio 2010\Projects\LearningAOP\WebAppCachingII\WebAppCachingII\
Here is my ASP.NET code-behind, the markup has a single bulleted list control;
using System;
using System.Collections;
using System.Linq;
using System.Web.UI.WebControls;
namespace WebAppCachingII
{
public partial class CachingDemo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
foreach (DictionaryEntry cacheditem in Cache)
{
var concatstring = cacheditem.Key + "***" + cacheditem.Value;
listOfCacheItems.Items.Add(new ListItem(concatstring));
}
}
}
}
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="CachingDemo.aspx.cs" Inherits="WebAppCachingII.CachingDemo" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:BulletedList ID="listOfCacheItems" runat="server" /> </div>
</form>
</body>
</html>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
<caching>
<outputCache enableOutputCache="false" />
</caching>
</system.web>
</configuration>

Simple website doesn't work on server

i have a very simple websote just for experimentation.
On my local machine it works but when i put it on the server i get "Server Error in '/' Application.Runtime Error "
defaul.aspx:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="default.aspx.cs" Inherits="_default"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
default.aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
web.config:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<directoryBrowse enabled="false" />
<defaultDocument>
<files>
<clear />
<add value="default.aspx" />
</files>
</defaultDocument>
</system.webServer>
</configuration>
But when i change the aspx file it works (i've removed:CodeFile="default.aspx.cs" and Inherits="_default")
<%# Page Language="C#" AutoEventWireup="true"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
Turns out that if i remove the System.Linq from my aspx.cs file the site does work.
Is the Framework version correct for the application pool on the server?
You've stated that it works if you remove System.Linq which could point at the Framework version being incorrect on the application pool
The other thing to check is to make sure you're not running sites built on different version of the .Net framework in the same application pool as they will kill each other and you'll get the dreaded "Server Error in '/' Application. Runtime Error"
Please check two dlls must be in your Bin folder on the server
1) System.Web.Extensions.dll
2) System.Web.Extensions.Design.dll

ckeditor in asp.net

I have to use a ckeditor in my application but I dont know how to write the
# Register Assembly="" Namespace="" TagPrefix="" %>
From where I could get the assembly?
In web.config section you can write:
<add tagPrefix="FredCK" namespace="FredCK.CKEditor" assembly="FredCK.CKEditor, Culture=neutral, PublicKeyToken=9ef91de3e191403a" />
Actually, best is not to use the ASP.NET CKEditor control but use CKEditor directly. The control is outdated (version 3.6 instead of 4.1) and not necessary. Basically, use a multiline textbox and make it of the class CKEditor. Don't forget to add the ckEditor.js to the head section:
web.config
<configuration>
<system.web>
<httpRuntime requestValidationMode="2.0"/>
<compilation debug="true" targetFramework="4.0" />
</system.web>
</configuration>
Test.aspx
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>ckeditor test page</title>
<script src="ckeditor/ckeditor.js" type="text/javascript"></script>
</head>
<body>
<form id="form1" runat="server">
<p>
<asp:TextBox class="ckeditor" ID="CkeditorTextBox" runat="server" TextMode="MultiLine" Columns="80" Rows="10">
Hi
</asp:TextBox>
</p>
<p>
<asp:Button ID="SubmitButton" runat="server" onclick="SubmitButton_Click" Text="Submit" />
</p>
</form>
</body>
</html>
Test.aspx.cs
using System;
public partial class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void SubmitButton_Click(object sender, EventArgs e) {
string CkEditorText = CkeditorTextBox.Text;
//add some processing here
}
}
There is a nice asp.net wrapper control for the ckeditor: http://cksource.com/forums/viewtopic.php?f=11&t=15882
The answer will be
scrpt type = "text/javascript" src = "ckeditor/ckeditor.js"....close script
//ckeditor is the folder that you have created in your application.
script type="text/javascript"
window.onload = function()
{
debugger
vartxtDemo = document.getElementByID('<%txtDemo.ClientID%');
CKEDITOR.replace(txtDemo);
}...//close the script
but before that make a ckeditor folder in your application and paste the contents that you have downloaded,

Resources