asp.net webform default routing does not work - asp.net

EDIT: I solved by myself.Cause the stylesheet reference path is /css/style.css not css/style.css,url will not be fixed by asp.net.I found that <link> <meta> and <title> will add as an server control to head when head tagged with runat="server",So these server control will auto fix current reference problem.
!!!BUT,<script> is ignored,One of the solutions is <%= ResolveClientUrl('~/js/jquery.js') %>.But It does not work when you have a theme attched to master page,asp.net canot add stylesheets in App_Themes to head when it contains such <% %> expression,asp.net will throw an exception.
So it seems like the best solution is using <ScriptManager>.
Another important discover is that when you have a <ContentPlaceHolder> in master runat=server head,stylesheet inside <Content> of child page using this master page will not be treat as a server control.So in <Content> of child page you must use <%= ResolveClientUrl%> to handle url fix.
ORINGIAL:
I'm running asp.net 4.0 on IIS 7 Express. I route "MarketList/{type}" to "~/MarketList.aspx" with default value new {type = 0}. The URL "localhost:4888/MarketList" just works well, I can recieve default value "0".
But I found that the "/" will mess up the stylesheet and javascript references defined in "Main.master" master page. "Main.master" is in the root level with "MarketList.aspx". The stylesheet in "Main.master" is defined as css/style.css. The "css" folder is also at the root level. When I'm accessing by "/MarketList", it works well. But, "/MarketList/1" gives the value "1" to {type}. The URL of the stylesheet in the page becomes "../css/style.css" which points to "/MarketList/css/style.css". This doesn't exists (obviously).
So, I decide to use "-" to split those parts, I route "market-list-{type}" with same setting just like above. But, I found I can not access the default routing URL which I thought would be "localhost:4888/market-list-". "localhost:4888/market-list" does not work either. Only "/market-list-0" will work.
Could someone help me?

Related

Setting file location of PIE.htc

I created another folder for my pie.htc..but when I load my html file in IE8 it does not work..i already tried setting different location to its behavior but still it wont work..
here's my code..
behavior: url(/pie/PIE.htc);
As others have noted elsewhere, and as documented here http://css3pie.com/documentation/known-issues/, the PIE.htc file location must be relative to the page where it's used, not relative to the css file. If you'll need to use PIE from within several different pages, consider adding a reference to it dynamically.
Here's how we handled it in a C# .Net application with a master page:
In the master page's markup between the head tags, place the following line:
<style id="InlinePageStyles" runat="server" type="text/css"></style>
In the Page_Load method of the master page's code behind, place the following line:
//get path to PIE.htc and add it to the page as a style (creates a class called Pie)
InlinePageStyles.InnerHtml += string.Format(".Pie {{ behavior: url({0}PIE.htc); }}", ConvertRelativeUrlToAbsoluteUrl(this.Request, ResolveUrl("~/")));
Also in the code behind, add this method:
private string ConvertRelativeUrlToAbsoluteUrl(HttpRequest request, string relativeUrl)
{
return string.Format("http{2}://{0}{1}", request.Url.Host, System.Web.VirtualPathUtility.ToAbsolute(relativeUrl), request.IsSecureConnection ? "s" : string.Empty);
}
Next, remove the behavior from your CSS.
Finally, add the "Pie" class to any page elements that need it.
Hope this helps.
behavior: url(../pie/PIE.htc);
".." for folder selection and pie is the folder
...............................
HI now put your pie.htc in root location and
write to css as like this
behavior: url(PIE.htc);
more info

How to refer to main domain without hard-coding its name?

I have a domain name: TestSite.com. I create several subdomains for this site and refer to them as first.TestSite.com, second.TestSite.com, etc.
How do I refer to TestSite.com relatively without having to hard code its name in an html or aspx file in first.TestSite.com? What I mean is (using folders as example) if I have a folder TestSite and a sub folder first
TestSite/first
, then from first I can refer to its parent TestSite folder by
../
What do I use to refer to TestSite.com from first.TestSite.com? Thanks.
There's no way using pure relative links. You have to program it as a string manipulation.
Something like:
var host = location.host;
var lastPeriod = host.lastIndexOf(".");
var remainder = host.substring(0, lastPeriod);
var afterSecondLastPeriod = remainder.lastIndexOf('.') + 1
var baseDomain = host.substring(afterSecondLastPeriod);
console.log(baseDomain);
EDIT: Shorter version using regex:
var baseDomain = host.match(/[^.]*\.[^.]*$/)[0]
This is general, so it will always return the last part. Regardless of whether it's a.TestSite.com, b.a.TestSite.com, etc. it will return TestSite.com.
You will have to modify it if this assumption is not correct.
If you want to keep your relative links, you can use the base element.
[The base element's href] attribute specifies an absolute URI that acts as the base
URI for resolving relative URIs.
So, add a base element, specify the href you'd like, and all the relative URIs on the page now use the URI you've specified as the base URI.
<head>
   <base href="http://testsite.com" />
</head>
That's all you need. However, if you'd like to make things a little cleaner, you can pull that URI from the web.config using System.Configuration.ConfigurationManager.AppSettings. Here's an aspx snippet:
<head>
   <base href="<%= ConfigurationManager.AppSettings["rootdomain"] %>" />
</head>
And the web.config:
<configuration>
<appSettings>
<add name="rootdomain" value="http://testsite.com" />
</appSettings>
</configuration>
This method allows you to affect many elements from one and its value can be driven from the web.config.
Since you tagged your question ASP.NET, I'm going to assume you want to do this server-side, so you are not relying on JavaScript on the client-side to generate HTML links.
First of all, make sure you have a web.config file in your subdomain sites, and add this to the configuration:
<configuration>
<appSettings>
<add name="MainDomain" value="example.com" />
</appSettings>
</configuration>
Using this setting in your ASPX pages, hyperlinks on one of your subdomains' pages would look like this:
Go to main domain home page
Go to products page on the main domain
The setting with name="MainDomain" will be read from the configuration file, and inserted into the page where it says <%$ AppSettings: MainDomain %>. This way, you are creating the required HTML on the server-side, with no hard-coding required.
If you need to change the name, simply update the configuration file, and all pages will automatically use the new domain name.
An alternate solution would be to use the original domain name for the live server, but point it to your development environment.
You can override DNS lookups in Windows by adding lines to this file.
C:\Windows\System32\drivers\etc\hosts
Add the following using a text editor.
127.0.0.1 www.livesite.com
127.0.0.1 first.livesite.com
127.0.0.1 second.livesite.com
These domains will now map to the locahost. So there won't be a need to hack HTML for testing.

is there a simple way to remove the "ct100" prefix from a .NET user control?

Long story short, dozens of pages use no master page. For a new module I created a master page with a menu control (menu control exists already) so I can get the same look across the six or so pages I'm creating now. Since the content page uses the master page, the Menu control has its name changed to ct100_Menu1 instead of just Menu1. Wouldn't be an issue except someone decided to use the exact name of the control for the CSS that styles the menu, by its exact ID (e.g. CSS is Menu1 a { /* stuff */ }). So the menu won't render properly because I'm using a Master Page and not just copying the code.
I cannot change the CSS code in the menu file as it could break something, so is there any way that I can change the control to not display that pesky ct100 without having to add any tools or mess with creating my own custom control (as I can't replace the Menu.ascx control, although I might be able to modify it to add CSS classes) or is my only choice to either not use a master page or copy the menu CSS into another file and set it properly?
Feel kind of stuck between a rock and a hard place because the code was deliberately written so you cannot use Master Pages and nobody ever went back to change it.
You should set the ClientIdMode to Static. Here's more information from MSDN. Note: This is .NET 4.0 only.
In earlier versions, I would recommend styling off of classes as you can't really control what the name will be everywhere that you use it (as you found out).
If you are on ASP.net 4.0, you can set the ClientID property of the controls.
Otherwise, you're in for a world of hurt as in: Custom Control, ASP.net Literals or JavaScript to change the IDs.
Are you using .NET 4? If so, you can set this on your control:
<asp:SomeControl ClientIDMode="Static" />
Just add the new name to the CSS - without removing the old (since you said that was an issue):
ctl100_Menu1 a,
Menu1 a { /* stuff */ }
If you are using ASP.NET 4.0, you can override the ClientId rendering mode, either per control, or for all controls. For instance:
<my:Menu runat="server" Id="Menu1" ClientIDMode="Static" />
This will enforce that the value "Menu1" is preserved as the client side Id for the element that is rendered. (see here).
What I would recommend though, is apply a CSS class to the menu element, and then adjust the CSS rules around a class. E.g.,:
#Menu1 a {
... to:
#Menu1 a,
div.menu a {
... etc
To "correct" this behavior for your entire web application, look in your web.config for the following tag:
<system.web>
...
<pages ... clientIDMode="*something*">
</pages>
...
</system.web>
Remove the clientIDMode="*something*" property specification. Just take it out.
Yay.

Get bookmark value from URL calling classic ASP

If I'm calling for example, http://www.mysite.asp?p1=2&p2=3#Bookmark Does the browser invoke that #Bookmark after the "classic" ASP generates output? It appears that it's not coming thru, the browser doesn't jump down to the bookmark. I am suspicious it's getting "thrown out" by either ASP or the browser. This acts the same on both FF and IE6. Ideas? Thanks Stackoverflow!
Have you set the anchor name in the HTML markup?
For this #Bookmark to work you must have a link <a name="Bookmark" ... ></a>.
See HTML Links - The name Attribute.
Basic notes:
Tip: If a browser does not find the named anchor specified, it goes to the top of the document. No error occurs.
More on this:
http://thedailyreviewer.com/dotnet/view/bookmark-anchors-and-vbnet-103202803
http://www.velocityreviews.com/forums/t96249-can-you-jump-to-an-anchor-on-postback.html

How do I get the current location of an iframe?

I have built a basic data entry application allowing users to browse external content in iframe and enter data quickly from the same page. One of the data variables is the URL.
Ideally I would like to be able to load the iframes current url into a textbox with javascript. I realize now that this is not going to happen due to security issues.
Has anyone done anything on the server side? or know of any .Net browser in browser controls. The ultimate goal is to just give the user an easy method of extracting the url of the page they are viewing in the iframe It doesn't necessarily HAVE to be an iframe, a browser in the browser would be ideal.
Thanks,
Adam
I did some tests in Firefox 3 comparing the value of .src and .documentWindow.location.href in an iframe. (Note: The documentWindow is called contentDocument in Chrome, so instead of .documentWindow.location.href in Chrome it will be .contentDocument.location.href.)
src is always the last URL that was loaded in the iframe without user interaction. I.e., it contains the first value for the URL, or the last value you set up with Javascript from the containing window doing:
document.getElementById("myiframe").src = 'http://www.google.com/';
If the user navigates inside the iframe, you can't anymore access the value of the URL using src. In the previous example, if the user goes away from www.google.com and you do:
alert(document.getElementById("myiframe").src);
You will still get "http://www.google.com".
documentWindow.location.href is only available if the iframe contains a page in the same domain as the containing window, but if it's available it always contains the right value for the URL, even if the user navigates in the iframe.
If you try to access documentWindow.location.href (or anything under documentWindow) and the iframe is in a page that doesn't belong to the domain of the containing window, it will raise an exception:
document.getElementById("myiframe").src = 'http://www.google.com/';
alert(document.getElementById("myiframe").documentWindow.location.href);
Error: Permission denied to get property Location.href
I have not tested any other browser.
Hope it helps!
document.getElementById('iframeID').contentWindow.location.href
You can't access cross-domain iframe location at all.
I use this.
var iframe = parent.document.getElementById("theiframe");
var innerDoc = iframe.contentDocument || iframe.contentWindow.document;
var currentFrame = innerDoc.location.href;
HTA works like a normal windows application.
You write HTML code, and save it as an .hta file.
However, there are, at least, one drawback: The browser can't open an .hta file; it's handled as a normal .exe program. So, if you place a link to an .hta onto your web page, it will open a download dialog, asking of you want to open or save the HTA file. If its not a problem for you, you can click "Open" and it will open a new window (that have no toolbars, so no Back button, neither address bar, neither menubar).
I needed to do something very similar to what you want, but instead of iframes, I used a real frameset.
The main page need to be a .hta file; the other should be a normal .htm page (or .php or whatever).
Here's an example of a HTA page with 2 frames, where the top one have a button and a text field, that contains the second frame URL; the button updates the field:
frameset.hta
<html>
<head>
<title>HTA Example</title>
<HTA:APPLICATION id="frames" border="thin" caption="yes" icon="http://www.google.com/favicon.ico" showintaskbar="yes" singleinstance="no" sysmenu="yes" navigable="yes" contextmenu="no" innerborder="no" scroll="auto" scrollflat="yes" selection="yes" windowstate="normal"></HTA:APPLICATION>
</head>
<frameset rows="60px, *">
<frame src="topo.htm" name="topo" id="topo" application="yes" />
<frame src="http://www.google.com" name="conteudo" id="conteudo" application="yes" />
</frameset>
</html>
There's an HTA:APPLICATION tag that sets some properties to the file; it's good to have, but it isn't a must.
You NEED to place an application="yes" at the frames' tags. It says they belongs to the program too and should have access to all data (if you don't, the frames will still show the error you had before).
topo.htm
<html>
<head>
<title>Topo</title>
<script type="text/javascript">
function copia_url() {
campo.value = parent.conteudo.location;
}
</script>
</head>
<body style="background: lightBlue;" onload="copia_url()">
<input type="button" value="Copiar URL" onclick="copia_url()" />
<input type="text" size="120" id="campo" />
</body>
</html>
You should notice that I didn't used any getElement function to fetch the field; on HTA file, all elements that have an ID becomes instantly an object
I hope this help you, and others that get to this question. It solved my problem, that looks like to be the same as you have.
You can found more information here: http://www.irt.org/articles/js191/index.htm
Enjoy =]
I like your server side idea, even if my proposed implementation of it sounds a little bit ghetto.
You could set the .innerHTML of the iframe to the HTML contents you grab server side. Depending on how you grab this, you will have to pay attention to relative versus absolute paths.
Plus, depending on how the page you are grabbing interacts with other pages, this could totally not work (cookies being set for the page you are grabbing won't work across domains, maybe state is being tracked in Javascript... Lots of reasons this might not work.)
I don't believe that tracking the current state of the page you are trying to mirror is theoretically possible, but I'm not sure. The site could track all sorts of things server side, you won't have access to this state. Imagine the case where on a page load a variable is set to a random value server-side, how would you capture this state?
Do these ideas help with anything?
-Brian J. Stinar-
Does this help?
http://www.quirksmode.org/js/iframe.html
I only tested this in firefox, but if you have something like this:
<iframe name='myframe' id='myframe' src='http://www.google.com'></iframe>
You can get its address by using:
document.getElementById('myframe').src
Not sure if I understood your question correctly but anyways :)
You can use Ra-Ajax and have an iframe wrapped inside e.g. a Window control. Though in general terms I don't encourage people to use iframes (for anything)
Another alternative is to load the HTML on the server and send it directly into the Window as the content of a Label or something. Check out how this Ajax RSS parser is loading the RSS items in the source which can be downloaded here (Open Source - LGPL)
(Disclaimer; I work with Ra-Ajax...)
Ok, so in this application, there is an iframe in which the user is supplied with links or some capacity that allows that iframe to browse to some external site. You are then looking to capture the URL to which the user has browsed.
Something to keep in mind. Since the URL is to an external source, you will be limited in how much you can interact with this iframe via javascript (or an client side access for that matter), this is known as browser cross-domain security, as apparently you have discovered. There are clever work arounds, as presented here Cross-domain, cross-frame Javascript, although I do not think this work around applies in this case.
About all you can access is the location, as you need.
I would suggest making the code presented more resilitant and less error prone. Try browsing the web sometime with IE or FF configured to show javascript errors. You will be surprised just how many javascript errors are thrown, largely because there is a lot of error prone javascript out there, which just continues to proliferate.
This solution assumes that the iframe in question is the same "window" context where you are running the javascript. (Meaning, it is not embedded within another frame or iframe, in which case, the javascript code gets more involved, and you likely need to recursively search through the window hierarchy.)
<iframe name='frmExternal' id='frmExternal' src='http://www.stackoverflow.com'></frame>
<input type='text' id='txtUrl' />
<input type='button' id='btnGetUrl' value='Get URL' onclick='GetIFrameUrl();' />
<script language='javascript' type='text/javascript'>
function GetIFrameUrl()
{
if (!document.getElementById)
{
return;
}
var frm = document.getElementById("frmExternal");
var txt = document.getElementById("txtUrl");
if (frm == null || txt == null)
{
// not great user feedback but slightly better than obnoxious script errors
alert("There was a problem with this page, please refresh.");
return;
}
txt.value = frm.src;
}
</script>
Hope this helps.
You can access the src property of the iframe but that will only give you the initially loaded URL. If the user is navigating around in the iframe via you'll need to use an HTA to solve the security problem.
http://msdn.microsoft.com/en-us/library/ms536474(VS.85).aspx
Check out the link, using an HTA and setting the "application" property of an iframe will allow you to access the document.href property and parse out all of the information you want, including DOM elements and their values if you so choose.

Resources