Pre-set formatting on asp.net pages. - c#

I am newish to Asp.net. I have inherited a web application project. When I create a new page it already has some formatting on the top even though I don't use any master page when creating the page. This I can see when I build the page. I am using ASP.Net 4.0, C# language.
For the life of me I cant figure out as in the underlying code for the file there is no reference to any css or any elements. The code for page.aspx is as follows:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Test" %>
<!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>
</body>
</html>
The code behind is as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
Thanks in advance for any help I can get.

Oh Actually when you create a page in Visual studio. He create some by default code file and a project structure. Just why you show different type of file on solutions.
See on your aspx page header its define cs code file as CodeFile="Test.aspx.cs" and also Inherits test.
Also .csproj file that contains assembly references
.projRefs.sln file that contains the current project and all referenced projects

I figured it out. There was a theme applied which was adding reference to a stylesheet when compiling the page. I added disable theming tag which removed this reference for the particular page.

Related

Parser error copying a single aspx page with code-behind

When I just copy (without deploying) an aspx page with code-behind to a web site I get a Parser Error Message when I try to use the page.
Let us assume this is just for testing purposes that I want to develop a single page in Visual Studio then copy (or FTP) it to a web site.
The complete error message is:
Parser Error Message: Could not load type 'SimpleSite.WebForm2'.
The WebForm2.aspx file is:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs"
Inherits="SimpleSite.WebForm2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
</div>
</form>
</body>
The WebForm2.aspx.cs file is:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace SimpleSite
{
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
TextBox2.Text = TextBox1.Text;
}
}
}
I am not sure if the namespace is the problem, it appears to be. I have used Expression Web to create an ASP.Net file with code-behind that I can just FTP
but that does not have a namespace (yes, I know; it is in the global namespace).
If I want to make one or two aspx files with code-behind just for educational purposes to be copied without deploying, can I do that using a Visual Studio Web Project or Web Site and maintain the page such that it still works as a page in the Visual Studio Web Project or Web Site?
Yes, the namespace is a problem.
Visual Studio creates pages for a Web Site without a namespace specified, in other words it creates pages with the code in the global namespace. Therefore simple pages (that do not use additional files) created in a VS Web Site can be copied individually by copying just the aspx file and corresponding aspx.cs file.
Another difference between a Web Project and a Web Site is that VS adds a aspx.designer.cs file for each page. It is not created for Web Sites.

How do you reference HTML/ASP elements with C# objects?

I'm new to ASP.NET and C#, I've primarily worked with Java. I want to dynamically add nodes to a tree view. I've followed a few tutorials but whenever I implement them they do not appear to work. I keep getting an error: "The name 'MyTreeView' does not exist in the current context".
Here's the C# code:
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace LocalTest
{
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
MyTreeView.Nodes.Add(new TreeNode("Node1"));
MyTreeView.Nodes[0].ChildNodes.Add(new TreeNode("ChildNode"));
}
}
}
}
And the HTML/ASP:
<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TreeView ID="MyTreeView" Runat="server">
</asp:TreeView>
</div>
</form>
</body>
</html>
"MyTreeView" is the ID of the treeview contained in the Default.aspx file.
The few tutorials I have followed seem to access the treeview directly with its ID as seen above which makes very little sense to me. For instance, if I were to do this in Android I would have to use the findViewById and establish a link between the XML and the Object.
Does anyone have any suggestions? I've bumbled around this site for a bit and I'veonly found similar issues but I cannot get a clear answer.
Thank you.
Looking at the code above, you are using what's known as ASP.Net WebForms where there are "server side" controls (e.g. <asp:TreeView ID="MyTreeView" Runat="server">)
The "page" (html aspx) needs to somehow "wire itself" to the code (c# file). It will need a "server-side" directive like so:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebFormsApplication.WebForm1" %>
<!DOCTYPE html>
//...the rest of the html goes here plus web controls, etc.
At which point, you can see stuff like Inherits="WebFormsApplication.WebForm1" that maps to the Class:
namespace WebFormsApplication
{
public partial class WebForm1 : System.Web.UI.Page
....
found in the WebForm1.aspx.cs file, also in the directive CodeBehind="WebForm1.aspx.cs"
So voila :)
establish a link between the XML and the Object.
Hth...
You do not have TreeView control in default.aspx. It is in .html file and default.aspx.cs can not see TreeView control.
It seems you are missing Page directive in your asp.net page
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

Access HTML page in C#

Basically I have two files:
details.html
details.cs
I would like to use details.cs to write values to details.html but the html textbox still stays the same.
details.html
<%# Page Language="C#"
AutoEventWireup="true"
CodeBehind="details.cs"
Inherits="Details.DetailsHTML" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form>
<input id="txt_details" type="text" name="txt_details" runat="server"/>
</form>
</body>
</html>
details.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;
using System.Data;
namespace Details
{
public partial class DetailsHTML : Page
{
//Declare controls used
protected System.Web.UI.HtmlControls.HtmlInputText txt_details;
protected void Page_Load(object sender, EventArgs e)
{
string strValue = Page.Request.Form ["txt_details"].ToString();
strValue = "test";
}
}
}
You can not access control in .html from .cs file. You are trying to use aspx page as you are inherting from Page class but naming .html, you are problably not using visual studio. Use details.aspx and details.aspx.cs. If you do not have Visual Studio then you can download free express version from here. This link explains how to create web application project using visual studio. The article Creating a Basic Web Page with Code Separation in Visual Studio will help you in creating the web page and access html control on server side.
txt_details.Value = "your value";
txt_details.Value = "test";
This should help
You can acess by a name of control eg:
txt_details.Value = "Test"

IIS aways returns an empty page

I am deploying a site built as a set of projects in a solution which is deployed as a series of websites (one project = one url).
The coding lanague is c# and the site uses a mssql db accessed via linq.
Everything works ok on the development server but at the moment, all pages with dynamically created elements (i.e all <asp> and custom tags) output a blank page with the following source code. Static pages (standard html and aspx) display correctly.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content="text/html; charset=windows-1252" http-equiv=Content-Type></HEAD>
<BODY></BODY></HTML>
I normally work with php on LAMP and this behaviour is unlike anything I've seen in that setup. I need some fresh ideas on causes / resolution of this error (preferably step by step / tutorial links - like I say IIS/c#.net is not my normal enviroment).
Things I have tried with no effect whatsoever:
altering permissions on site folders
altering web.config settings
Redeploying site files via copy / paste and the vb publish option with various settings
Plus a whole ton (over two days so far) of internet research
Thanks for all replies.
edit:
version numbers:
iis: v6.1 (bulid: 7601)
OS: Windows web server 2008 R2 / Service pack 1
.net: v4
second edit:
Sample page:
aspx file:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="dbtest.aspx.cs" Inherits="Rica.Yoodul.dbtest" %>
<!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>
<h1>test</h1>
<asp:Literal ID="Literal1" runat="server"></asp:Literal>
</div>
</form>
</body>
</html>
aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
namespace Rica.Yoodul
{
public partial class dbtest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection myConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["RicaConnectionString"].ConnectionString);
myConnection.Open();
SqlDataReader myReader = null;
SqlCommand myCommand = new SqlCommand("SELECT * FROM dbo.MaritialStatus", myConnection);
myReader = myCommand.ExecuteReader();
while (myReader.Read())
{
Literal1.Text = Literal1.Text + myReader["Name"].ToString();
}
myConnection.Close();
}
}
}
edit:
Something I didn't add: Based on some of the other behaviour the server is displaying, I think the most likely culprit is misconfiguration of the server.
edit:
OK I've narrowed down the cause a little. By taking out everything except the langauge attribute in the first line of the front end file I can get the server to display the static content.#
edit:
OK http://www.iis.net/ConfigReference covers the use of the iis config files. Only I went to the path indicated on the page and I don't appear to have any config files at all. How do I comfirm / fix this?
It sounds like your production server doesn't have ASP.NET correctly installed/registered, so your pages are being treated as simple text documents rather than code. This can sometimes happen if you install .NET before installing IIS, for example.
To confirm this, put together a barebones page that includes a mix of HTML and ASP.NET...
<%# Page Language="C#" %>
<html>
<head>
<title>ASP.NET Hello World</title>
</head>
<body>
<p>Hello from HTML</p>
<p><%= "Hello from ASP.NET" %></p>
</body>
</html>
If you load that up and only get the plain-HTML greeting, use your browser's "View Source" option and see what you're actually getting. If your ASP.NET source-code is shown in the source the browser's receiving, that confirms that IIS didn't treat it as an ASP.NET file - which means ASP.NET isn't installed correctly or is disabled.

how can i create a dynamic page in asp.net (C#)?

I added a page "First.aspx" in my website application. Inside First.aspx page i have a button named btnbutton.
"onclick" Event of "btnbutton" a new dynamic page should open.
how can i do this.?
Please Remember the new dynamic page created, is not in existing in the application. This page should be created at runtime and dynamic also.
please help me out!
If you are asking about generating ASP.NET pages at runtime, that is impossible. The reason is the following:
You need to compile the code of the
ASP.NET page before run it. And that
is impossible after your web
application has started.
However, if you are asking about navigation between pages, then, you could use Response.Redirect:
Response.Redirect("http://www.stackoverflow.com/");
You can create new Dynamic File in ASP.net website (Not in a ASP.net Web Application).
But there is a problem . Once you created a file the whole Website will be restarted for compiling the newly created file. So you Session data will be lost.
Here is the code to create new file.
string fielName = Server.MapPath("~/file.aspx");
//File.Create(fielName);
//File.AppendText(fielName);
// create a writer and open the file
TextWriter tw = new StreamWriter(fielName);
// write a line of text to the file
tw.WriteLine(#"<%# Page Language=""C#"" AutoEventWireup=""true"" CodeFile=""file.aspx.cs"" Inherits=""file"" %>
<!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>
");
// close the stream
tw.Close();
tw = new StreamWriter(fielName + ".cs");
// write a line of text to the file
tw.WriteLine(#"using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
public partial class file : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(""new File "");
}
}
");
// close the stream
tw.Close();

Categories