I want to create ASP.NET 4.0 dynamic pages loaded from my MS SQL server. Basically, it's a list of locations with information. For example:
Location1 would have the page www.site.com/location/location1.aspx
Location44 would have the page www.site.com/location/location44.aspx
I don't even know where to start with this, URL rewriting maybe?
Url rewriting addresses a different problem than what you are describing.
You can use an HttpHandler that handles requests to the path location and parse the last segment to get your lookup key then simply pass execution off to an .aspx. Although you are passing execution off to a general page, the url will remain as entered.
I will provide an example. Give it a shot. here is a sample project
LocationHandler.cs
using System.IO;
using System.Web;
namespace DBHandler
{
public class LocationHandler : IHttpHandler
{
#region IHttpHandler Members
public void ProcessRequest(HttpContext context)
{
HttpRequest request = context.Request;
string page = Path.GetFileNameWithoutExtension(request.Url.Segments[request.Url.Segments.Length - 1]);
// for url ~/location/location33.aspx page will be 'location33'
// do something interesting with page, perhaps
context.Server.Execute("~/locations.aspx?locationId=" + context.Server.UrlEncode(page));
}
public bool IsReusable
{
get { return false; }
}
#endregion
}
}
locations.aspx
<%# Page Language="C#" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = Request["locationId"];
}
</script>
<!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:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
web.config excerpt
...
<system.web>
...
<httpHandlers>
<add verb="*" path="location/*.*" type="DBHandler.LocationHandler"/>
</httpHandlers>
</system.web>
...
Related
I have a property x in my code-behind file in a web application project. It gives me compile time error while it does not give any error in website project
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs"
Inherits="test.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<%= x2 %>
<%# x2 %>
</div>
</form>
</body>
</html>
Codebehind file :
using System;
namespace test
{public partial class WebForm1 : System.Web.UI.Page
{
public int x2 = 2;
protected void Page_Load(object sender, EventArgs e)
{
}
}
}
It is working fine when I use the same technique in my website project
Kindly help
Thanks
Check if there are any other errors in your code behind WebForm1.aspx.cs.
like missing reference or any other compile error.
This works for me. Are you sure there's a value in ViewState? On form load add a value to test.
ViewState["FormMode"] = "ViewApplicant";
lblMessage.DataBind();
Also make sure the label has some text to view. It could be showing but is empty.
<asp:Label ID="lblMessage" runat="server"
Text="some text"
Visible='<%# FormMode == "View" || FormMode == "ViewApplicant" %>'>
</asp:Label>
Your code is perfectly okay. While accessing the variable/property initillay, the red underline may appear under the expression indicating the compile error, but when you build the solution, there should be no error.
Your original post has changed so here's a new answer.
Try removing the namespace from the code behind and change 'inherits' on the page to Inherits="WebForm1".
page directive:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs"
Inherits="WebForm1" %>
code behind:
using System;
public partial class WebForm1 : System.Web.UI.Page
{
public int x2 = 2;
protected void Page_Load(object sender, EventArgs e)
{
}
}
'Inherits' must match the partial class name.
I've searched this error in all google results, but I can't find a solution.
I have this WebForm:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="LoTest.aspx.cs" Inherits="-.-.-.LoTest" %>
<%# Register TagPrefix="ajax" Namespace="MagicAjax.UI.Controls" Assembly="MagicAjax" %>
<!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>
<ajax:AjaxPanel ID="ajaxLG" runat="server" AjaxCallConnection="Synchronous">
<asp:Button ID="btnTest" runat="server" CssClass="button" ToolTip="gin." Text="LogIn" OnClick="btnTest_Click"></asp:Button>
</ajax:AjaxPanel>
</div>
</form>
</body>
</html>
Code Behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MagicAjax.UI;
using MagicAjax;
namespace -.-.-
{
public partial class LoTest : AjaxPage
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnTest_Click(object sender, EventArgs e)
{
AjaxCallHelper.WriteAlert("Test msg");
}
}
}
The web.config:
<httpModules>
<add name="MagicAjaxModule" type="MagicAjax.MagicAjaxModule, MagicAjax"/>
</httpModules>
In the Line “AjaxCallHelper.WriteAlert("Test msg");” throws the error:
Using the AjaxCallHelper write methods outside of an AjaxCall is not allowed.
In IE, just add the site to the compatibility view and that´s all.
Seems like if AjaxMagix.dll works with older elements of the explorer.
In Google Chrome doesn´t work.
I have a situation where I need to create multiple code behind files for a single .aspx page in c# asp.net. Actually I have a web form on that huge coding is done and I need multiple developer's working on it simultaneously. How can I achieve the same?
Here is the code snippet I tried
Class 1 MyPartialClass.cs
namespace WebApplication1
{
public partial class Default : System.Web.UI.Page
{
protected void PrintText(string pt)
{
Response.Write(pt);
//lblTest.Text = pt; //Can not access this label in partial class.
}
}
}
Class 2 which is Default.aspx.cs
namespace WebApplication1
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
PrintText("Hello World");
}
}
}
and my HTML source
<%# Page Language="C#" AutoEventWireup="false" CodeBehind="Default.aspx.cs" Inherits="WebApplication1.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblTest" runat="server"></asp:Label>
</div>
</form>
</body>
</html>
ASP.NET will always generate your code behind files as partial classes
namespace WebApplication1
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
}
You can therefor separate the code behind in different files, if you pertain the definition as partial and keep the class under the same namespace.
Edit : The Web Project
//Default.aspx
<%# Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="WebApplication1.Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:Label ID="lblTest" runat="server" Text="Label"></asp:Label>
</asp:Content>
//Default.aspx.cs
namespace WebApplication1
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
PrintText("Hello World");
}
}
}
//MyPartialClass.cs
namespace WebApplication1
{
public partial class Default
{
protected void PrintText(string pt)
{
Response.Write(pt);
lblTest.Text = pt; //lblTest is accessible here
}
}
}
I haven't modified any other generated files. One thing I like to mention is that the Default.aspx.cs file that has been generated was generated with the class name "_Default". I have changed it to Default and Visual Studio refactored the change across all files that contain a definition for that class, except the Default.aspx file, where I had to manually modifiy from Inherits="WebApplication1._Default" to Inherits="WebApplication1.Default".
Edit 2:
I kept searching the internet, and according to http://codeverge.com/asp.net.web-forms/partial-classes-for-code-behind/371053, what you are trying to do is impossible. Same idea is detailed at http://codeverge.com/asp.net.web-forms/using-partial-classes-to-have-multiple-code/377575
If possible, consider converting from Web Site to Web Application, which supports what you are trying to achieve. Here is a walkthrough on how to perform this conversion: http://msdn.microsoft.com/en-us/library/vstudio/aa983476(v=vs.100).aspx
You need to set up a source safe server in order to that, like Team Foundation Server.
You can easily just add partial classes to your code.
http://www.codeproject.com/Articles/709273/Partial-Classes-in-Csharp-With-Real-Example
I'm having a weird issue where a button's click event is not being fired for a while (around 20-30 seconds) and then starts to work as normal. Refresh the page and I have to wait for it to work again.
Please note this: It works when run in debug on the localhost, the behavior described is only found on the server. I'm starting to suspect a server-side issue, but I don't have access to it and would need to pin-point what might be causing this to the people in charge of it (if it's indeed a server-side issue).
Any help to locate the issue is much appreciated! I stripped the page down to the most basics and this is what I have.
ASP:
<%# 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 (http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd)">
<html xmlns="http://www.w3.org/1999/xhtml (http://www.w3.org/1999/xhtml)">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</div>
</form>
</body>
</html>
Code-behind:
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)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("http://www.google.se/ (http://www.google.se/)");
}
}
Config:
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
</system.web>
</configuration>
I have a very simple user control in my web site project which has public property declarations as following
public DateTime StartDate
{
get
{
return _startDate;
}
set
{
_startDate = value;
}
}
public DateTime EndDate
{
get
{
return _endDate;
}
set
{
_endDate = value;
}
}
When i drag the ascx file to one of my aspx page and when i go to code behind of aspx page i can access the controls properties through intelisense, but when i run the project through visual studio i get error "The name 'uctTest1' does not exist in the current context " any suggetions to fix the error?
This is the line where Error shows when i run the project uctTest.StartDate = DateTime.Now;
aspx page markup :
<%# Page Language="C#" AutoEventWireup="true" CodeFile="removetest.aspx.cs" Inherits="removetest" %>
<%# Register src="~/uctTest.ascx" tagname="testCtl" tagprefix="uc1" %>
<!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>
<uc1:testCtl ID="uctTest1" runat="server" />
</div>
</form>
</body>
</html>
aspx page code behind :
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
uctTest1.StartDate = DateTime.Now;
}
}
protected void btnEdit_Click(object sender, EventArgs e)
{
}
It seems that you are missing the #Register directive in the .aspx file referring to path of .ascx control.
http://msdn.microsoft.com/en-us/library/c76dd5k1(v=vs.71).aspx
I had another aspx page with different file name in the website project that another developer had copied from my aspx page which had same <%# Page Language="C#" AutoEventWireup="true" CodeFile="aspxpagename.aspx.cs" Inherits="aspxpageClass" %> directive in that copied page. Even though that copied paged didnt have user control strangely page I was working reported the error. Changing that page directive's codefile,inherits attributes to different values fixed the issue.