How do you get a server control HTMLAnchor to have href="#". It keeps resolving the "#" to the control path.
<a href="#" runat="server" />
resolves to: <a href="../ControlPath/#">
I can't seem to get a google search to give me the results i want so i figured i'd ask here.
EDIT: Syntax.
Removing the runat server is not an option. It's manipulated in the backend, this was just a simplification.
I had the same problem, here's how I could resolve it:
Original code
User control:
<a id="foo" runat="server">...</a>
Code behind:
foo.Attributes.Add("href", "#");
Output:
<a id="..." href="../Shared/Controls/#">...</a>
Updated code
User control:
<asp:HyperLink id="foo" runat="server">...</asp:HyperLink>
Code behind:
foo.Attributes.Add("href", "#");
Output:
<a id="..." href="#">...</a>
I had a similar issue when rendering the page with PageParser.GetCompiledPageInstance() or when the url was rewritten. For some reason the HtmlAnchor always resolved incorrectly (similar to what you have above).
Ended up just using a HtmlGenericControl, since you are manipulating it server-side anyway this may be a possibility for you.
HtmlGenericControl anchor = new HtmlGenericControl("a");
anchor.Attributes.Add("href", "#");
Brendan Kowitz solution will work, however i was not able to implement it due to the way this control is to operate. I ended up having to hack it as per the following code in the code behind:
lnk.Attributes.Add("href",Page.Request.Url.ToString() + "#");
Where lnk is an HtmlAnchor.
The reason for this issue in the first place is the control is not in the same directory as the page, and .Net goes about "intelligently" fixing your problem for you. The above will work, though if anyone has a better solution i'm all ears.
Originally I had this as a comment but by request I'm adding it as an answer since nobody else has explained why the original behavior is occurring or how to directly prevent it.
The URL rewriting is caused by the method ResolveURL on the Control class. I looked at it in Reflector and found that it will attempt to rewrite anything that it thinks is a relative URL if AppRelativeTemplateSourceDirectory is non-empty.
The simple workaround is to set this variable on the Page object to an empty string at some global level (or at least before Render), although this could be an issue if some other bit of your control structure requires it to be empty.
I suppose a true fix would be to get Microsoft to make UrlPath.IsRelativeUrl() smarter.
I ran into the same thing. If you set this on page load, it will work:
AppRelativeTemplateSourceDirectory = "";
Try removing the "runat" attribute and wrapping what you want to link;
<a href="#" >Your Link Text/Image Here</a>
This should work.
text
This should work.
text
Mine too works fine...I have a user control AnchorTag.ascx:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="AnchorTag.ascx.cs" Inherits="JavascriptScroll.AnchorTag" %>
<a id="A1" href="#" runat="server" >Anchor Tag</a>
And I included it as :
<%# Register src="AnchorTag.ascx" tagname="AnchorTag" tagprefix="uc1" %>
.
.
.
<uc1:AnchorTag ID="AnchorTag1" runat="server" />
.
.
And it renders as expected:
Anchor Tag
Please correct me if I'm doing something which is not expected...
EDIT: Includes nested paths
My test project renders the correct link for me:
http://localhost:2279/WebSite1/Default.aspx#
ASPX:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%# Register src="control/WebUserControl2.ascx" tagname="WebUserControl2" 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">
<uc1:WebUserControl2 ID="WebUserControl21" runat="server" />
</form>
</body>
</html>
Control:
<%# Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl2.ascx.cs" Inherits="WebUserControl2" %>
<a id="A1" href="<%# URLHelper("~/#") %>" runat="server" >here</a>
Control Code-Behind:
protected string URLHelper(string s)
{
return Control.ResolveUrl(s);
}
What about this?
HtmlAnchor errorLink = new HtmlAnchor();
errorLink.InnerText = this.Message;
errorLink.HRef = errorLink.ResolveClientUrl("#" + this.FormControlId);
errorLink.Attributes["rel"] = "form_help";
Works for me but I'm using a Server Control in a Class Library as opposed to a User Control. I think it should work for a User Control as well.
Related
I am pretty new to ASP.NET and C# and am beginning to take over a large project from a previous developer. I've encountered an ASP.NET tag that I cannot find any documentation about through searching online and the previous developer is unavailable for questions.
Does anyone recognize this "blac" tag? It's used in these two ways in the project I'm working with:
<blac:PriorityLabel>
<blac:DBDataSource>
I'm not sure why the tag is "blac:" and not "asp:".
Any guidance is appreciated.
It's a alias to a user control. For example:
<%# Register TagPrefix="scott" TagName="header" Src="Controls/Header.ascx" %>
<%# Register TagPrefix="scott" TagName="footer" Src="Controls/Footer.ascx" %>
<%# Register TagPrefix="ControlVendor" Assembly="ControlVendor" %>
<html>
<body>
<form id="form1" runat="server">
<scott:header ID="MyHeader" runat="server" />
</form>
</body>
</html>
I searched a way to include a file in a web application (like a menu, so I won't have to edit it on all pages when applying changes), but haven't found something as simple as
<?php include "Menu.html"; ?>
Can you please help?
Have you looked into Master Pages? They would certainly help you add the same layout across several pages.
Or perhaps you want a reusable User Control (that you write yourself)?
We don't use "include page" in asp.net, even though it is possible (with a different syntax of course). Instead, have a look at Master page concept.
MasterPages allow you to maintain a parent/child relationship between a master page which contains content that wraps around any number of child content pages.
Similarly, UserControls allow you to re-use whatever content you want on whatever page you want, whether it's a MasterPage or ContentPage:
<%# Page Language="C#" %>
<%# Register TagPrefix="uc" TagName="Spinner"
Src="~/Controls/Spinner.ascx" %>
<html>
<body>
<form runat="server">
<uc:Spinner id="Spinner1"
runat="server"
MinValue="1"
MaxValue="10" />
</form>
</body>
Methods (C#)
Executable code:
Page include
<!--#include file="a.aspx"-->
Execute independently inside a page
<% Server.Execute("a.aspx"); %>
Non-executable code:
<% Response.WriteFile("a.inc"); %>
I believe this is what you are looking for.
<!--#include file="wisdom.aspx"-->
I use C#.net
<% Response.WriteFile("YourPage.aspx"); %>
and this works real well for me!!
I also use your line,
<!--#include file="wisdom.aspx"-->
when I am in HTML mode.
I'm sure this is RTFM, but I just can't figure out which FM I'm supposed to R.
I'm trying to serve a JNLP (Java Web Start) file (which is an XML format), and ASP.Net insists on appending HTML code to the response body.
More detail: I have a .aspx file and an accompanying .aspx.cs file. These were generated with the "new page" wizard. In Page_Load() in the .aspx.cs file, I generate some XML, do Response.ContentType = "application/x-java-jnlp-file", you know the drill.
The .aspx file, however, contains:
<%# Page Language="C#" CodeBehind="MyPage.aspx.cs" Inherits="MyProj.MyPage" EnableSessionState="False" %>
<!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>
</div>
</form>
</body>
</html>
This code is appended to the output.
How do I avoid this? I tried calling Response.End() from Page_Load() but it's reportedly Evil and it throws nasty exceptions. Response.Close() is even worse, and breaks HTTP. I also tried simply removing all the HTML from the body, but ASP then complains about the fact that it needs a <head runat="server"> for something called "Themed CSS" (I'm not sure what that means).
Any leads?
Thanks!
Obligatory Use a Handler.
This gives you all the control necessary over the direct-output of information. The article included even gives an example of outputting an image.
You can turn off themes by adding EnableTheming="false" and Theme="" to Page directive
So your page would become
<%# Page Language="C#" CodeBehind="MyPage.aspx.cs"
Inherits="MyProj.MyPage" EnableSessionState="False"
EnableTheming="false" Theme=""%>
Adding a Response.Clear() before any output should then work as expected. However Brad's comment is spot on, this is perfect for an HTTP Handler
Just have the page as :
<%# Page Language="C#" CodeBehind="MyPage.aspx.cs" Inherits="MyProj.MyPage" EnableSessionState="False" %>
Delete the rest of the HTML in the page and as the poster suggested and do a Response.Clear()..
It is important you delete everything after the end of the
<%# Page Language="C#" CodeBehind="MyPage.aspx.cs" Inherits="MyProj.MyPage" EnableSessionState="False" %> declaration.
With a Response.Clear() or just remove it from the page!
I've got a master page setup with a contentplaceholder control inside the title tag as so:
<head id="head1" runat="server">
<style type="text/css">
body { font-family: Tahoma; font-size: 9pt; }
</style>
<title><asp:contentplaceholder id="title" runat="server" /></title>
</head>
That contentplaceholder is implemented inside a page that uses that masterpage as so:
<asp:content runat="server" contentplaceholderid="title">
Welcome: <%= this.BasketID %>
</asp:content>
I'm trying to then get a copy of the substituted title inside the masterpage body (also tried inside the page - and this doesnt work either) like:
<p>
<strong>Subject:</strong> <%# Page.Title %>
</p>
In all cases Page.Title and Page.Header.Title are "" (I've tried both databinding and using the <%= %> syntax to no avail.
Does anyone know what is going on here and how I can overcome this?
Thanks.
The problem you're getting is because you are 'tricking' the page cycle. You'd better use this in the codebehind of the page:
Master.Title = "Welcome: " + basketId
You could do it this way however; on the masterpage: create a HtmlTextWriter, configure it to write to a MemoryStream. Render the 'title' contentplaceholder to the HtmlTextWriter, attach a StreamReader to the stream to grab the content as a string, and output this to your page. Yet, this is not efficient, and way too much work :-)
I'm very new to ASP.NET, help me please understand MasterPages conception more.
I have Site.master with common header data (css, meta, etc), center form (blank) and footer (copyright info, contact us link, etc).
<%# Master Language="C#" AutoEventWireup="true" CodeFile="Site.master.cs" Inherits="_SiteMaster" %>
<!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="tagHead" runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="styles.css" type="text/css" />
</head>
<body>
<form id="frmMaster" runat="server">
<div>
<asp:ContentPlaceHolder ID="holderForm" runat="server"></asp:ContentPlaceHolder>
<asp:ContentPlaceHolder ID="holderFooter" runat="server">Some footer here</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
and I want to use second master page for a project into sub directory, which would contains SQL query on Page_Load for logging (it isn't necessary for whole site).
<%# Master Language="C#" AutoEventWireup="true" CodeFile="Project.master.cs" Inherits="_ProjectMaster" MasterPageFile="~/Site.master" %>
<asp:Content ContentPlaceHolderID="holderForm" runat="server">
<asp:ContentPlaceHolder ID="holderForm" runat="server" EnableViewState="true"></asp:ContentPlaceHolder>
</asp:Content>
<asp:Content ContentPlaceHolderID="holderFooter" runat="server">
<asp:ContentPlaceHolder ID="holderFooter" runat="server" EnableViewState="true"></asp:ContentPlaceHolder>
</asp:Content>
But I have a problem: footer isn't displayed.
Where is my mistake? Am I right to use second master page as super class for logging?
Project page looks like this:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" MasterPageFile="~/Project.master" %>
<asp:Content ContentPlaceHolderID="holderForm" runat="server">
<p>Hello World!</p>
</asp:Content>
<asp:Content ContentPlaceHolderID="holderFooter" runat="Server">
Some footer content
</asp:Content>
I've been working with nested master pages and have run in to something similar. From what I see where you have "Some footer here" in the Site.Master is where the problem lies and I've had similar problems with having content with-in a contentplaceholder tag. if you try this instead
<asp:ContentPlaceHolder ID="holderFooter" runat="server"/>Some footer here
Then you should be able to see the footer content.
I'm not sure I'd use master pages for this. If it's really just going to do logging, I'd implement IHttpModule, register it in web.config, and then check whether or not to log based on the path of the request. I think of master pages as being about content rather than other processing such as logging.
See the IHttpModule walkthrough on MSDN for an example - in your BeginRequest handler, you'd probably check the request path and log appropriately if it matched.
Apologies if I misunderstood what you're trying to do though.
You should leave your ContentPlaceHolder empty, for it gets substituted by the content of the Content in your actual Page...
When you move the "Some footer here" text to your Content, you will see your lines of text :)
HTH
This link gives a simple explanation on Master pages,
http://waxtadpole.wordpress.com/2009/01/16/master-page-content-not-visible-visual-studio-2008/
The question are you right to use child Master pages in this instance - I would say master pages should be helping you solve issues around building a consistent layout, not for whether or not logging should occur.
The problem is, when the text elements placed inside Default.aspx are put in their relative Content Placeholders, they are written on the placeholders of your Site.master page and not those of Project.master (which have the same names).
You should resolve the naming conflict, by assigning different ContentPlaceHolderIDs to the the placeholders in Project.master (this means you'll also have to change the references in Default.aspx).
This would be your Project.master file:
<%# Master Language="C#" AutoEventWireup="true" CodeFile="Project.master.cs" Inherits="_ProjectMaster" MasterPageFile="~/Site.master" %>
<asp:Content ContentPlaceHolderID="holderForm" runat="server">
<!-- whatever... -->
<asp:ContentPlaceHolder ID="holderFormInternal" runat="server" EnableViewState="true"></asp:ContentPlaceHolder>
<!-- ... -->
</asp:Content>
<asp:Content ContentPlaceHolderID="holderFooter" runat="server">
<asp:ContentPlaceHolder ID="holderFooterInternal" runat="server" EnableViewState="true"></asp:ContentPlaceHolder>
</asp:Content>
And thus, your .aspx pages that use the Project master page instead of the global Page.master must be changed to:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" MasterPageFile="~/Project.master" %>
<asp:Content ContentPlaceHolderID="holderFormInternal" runat="server">
<p>Hello World!</p>
</asp:Content>
<asp:Content ContentPlaceHolderID="holderFooterInternal" runat="Server">
</asp:Content>
If the only reason is to implement loggin why would you mess around with masterpages?
If the logging isent supposed to display any text!?
You either do as Skeet proposed with an IHTTP handler.. Or lazier one would be do have a class that derives from webpage and implement logging in that class and make your pages that need logging dervice from that..
ex:
public class LoggingPage : : System.Web.UI.Page
{
public override void OnLoad()
{
// Do logging
}
}
partial class OneOfTheWebPages : LoggingPage
{
public void onLoad()
{
base.onLoad();
}
}
I may be misunderstanding your problem - but from the code you've posted, there isn't anything in the footer.
In your Project page, the <asp:Content> tag for the holderFooter content place holder doesn't have anything in it.
I have next inheritance tree:
Site.master <-- Page1.aspx
<-- Project.master <-- Page2.aspx
And I don't know why Page2 display only content of itself and it's master page - Project. But doesn't display a content of Site (as Page1 does) Why? What have I to write for doing that?