DataNavigateUrlFields navigating to subfolder instead of correct location. - c#

I have a datagrid that needs one of the fields to hyperlink to a document housed on another server. The path is in this format: \\server\location\file.doc, but when I click on the cell in the data grid it becomes: http://myASPServer/Subfolder/server/location.file.doc. Is there any way that I can force this to go to the correct location? I know that you can prevent this for external websites by adding ftp:// or http://, but this does not seem to work for opening up this server location. Any suggestions?

I believe your answer can be found here on the asp.net forums http://forums.asp.net/t/1140909.aspx/1 - accepted answer from there below for your convenience.
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink Text="TextField" id="myHL" runat="server"
NavigateUrl='<%# "file:///" + DataBinder.Eval(Container.DataItem, "Path").ToString() %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
I believe you need to prefix your links with file:/// for it would be file:///\\server\location\file.doc

Related

error in the gridview control

I have a Gridview and the columns defined like below.
When I run the program I get the error
Literal content is not allowed within a System.Web.UI.WebControls.DataControlFieldCollection
<Columns>
<asp:CommandField ButtonType="Image"
ControlStyle-Height="20"
ControlStyle-Width="30"
SelectImageUrl="tar.png"
SelectText="Select"
ShowSelectButton="true"/>
<asp:TemplateField HeaderText="Target Date">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"
Text='<%# Bind("tar_date") %>'>
</asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lbl1" runat="server"
Text='<%# Bind("tar_date") %>'>
</asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Right" />
</asp:TemplateField>
</Columns>
Can anyone help me solving this?
Nothing seems to be wrong with your markup.
The only thing I would recommend is ending the Label control immediately and trying it again.
<asp:Label ID="lbl1" runat="server" Text='<%# Bind("tar_date") %>' />
// OR
<asp:Label ID="lbl1" runat="server" Text='<%# Bind("tar_date") %>'></asp:Label>
In the past I have seen issues when Tab, or some unintentional characters come in between some of the templated controls. Check if you have any such characters by redoing every line from scratch.
This question is a little old, but for the others who encounter this problem:
This problem can caused by not putting white space between properties. For example:
<asp:TextBox ID="TextBox1" runat="server"Text='<%# Bind("tar_date") %>'> </asp:TextBox> //wrong (no space before Text)
This was such a frustrating error to run into. I wasted about 4 hours on this and there are surprisingly few resources I could find on Google to help me troubleshoot it. I was updating a legacy application, so the intricacies of a GridView were a bit hazy since I haven't created one from scratch in a while.
At the end of it, the fix was a result of a suggestion by Raja to rewrite the control. Visual Studio wasn't highlighting a very important issue and the ambiguous nature of the error message had me looking at the wrong grid columns. Despite the error pointing to an issue with the TemplateField, the issue for me was actually in a BoundField.
During a conversion from Telerik RadGrid to GridView, the BoundField control had an orphaned <ItemStyle> tag nested inside of it, but the BoundField control doesn't allow this.
Visually, you wouldn't know or even suspect this, unless you have recent familiarity with GridView. You couldn't run into it by debugging. Visual Studio and the compiler were not reporting this either. So troubleshooting it was a beast.
The thing that worked was rewriting the grid, line-by-line. Thanks, Raja!
The autocomplete feature in Visual Studio wouldn't let me close the BoundField control to add any other tag/control of any kind. This is when I finally realized where the issue was.
I hope this helps another unlucky Googler. :)

Retrieve Images from ImagePath

I have saved an image path in database like so:
C:\Users\3embed\Documents\Visual Studio 2010\Projects\HeritageWeb\HeritageWeb\Images\startbutton.png
I want to display the image as thumbnail. Right now I'm using this code:
<asp:TemplateField ItemStyle-HorizontalAlign="Center" ItemStyle-Width="7%" HeaderText="Icon">
<ItemTemplate>
<asp:Image ID="ProfImage" ImageUrl='<%#Eval("Thumbnail","/Images/{0}")%>' runat="server" Width="30px" Height="30px"></asp:Image>
</ItemTemplate>
</asp:TemplateField>
And it's giving me the entire path. I just need startbutton.png. However, I need to store the entire path in the database since I need it somewhere else to.
It's a bit messy, but try replacing the following...
<%#Eval("Thumbnail","/Images/{0}")%>
With (untested and updated)...
<%#Eval(System.IO.Path.GetFileName(Container.DataItem["Thumbnail"]),"/Images/{0}")%>
<%#string.Format("/Images/{0}", System.IO.Path.GetFileName(Eval("Thumbnail")))%>

What url rewriting or wildcard subdomain rule be followed in this given case?

I am totally new to URL Rewriting and WildCard Subdomain management.
My requirement is that I have a gridview in my ASP.Net 3.5 WebApp from where a hyperlinks navigate url property is dynamically generating the following url.
http://businessbazaar.in/BusinessBazaarAspx/Details.aspx?cid=1&name=Steel_Bird_Fabricators
Note : here _ denotes the white space between the words.
Now what I want is that when a user click on the url ,the browser will rewrite the url to
http://steelbirdfabricators.businessbazaar.in
I want it to achieve this via some dll or web.config. I don't want it on IIS as setting, Is this possible? Then please tell me how? It will be highly appreciated.
I'm assuming that somewhere in your gridview you have something like this:
<asp:TemplateField HeaderText="Url">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server"
Text='<%# Bind("CategoryName") %>'
NavigateUrl='http://businessbazaar.in/BusinessBazaarAspx/Details.aspx?cid=1
&name=<%# Eval("CategoryName") %>' ></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
If you were to change that to something like
<asp:TemplateField HeaderText="Url">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server"
Text='<%# Bind("CategoryName") %>'
NavigateUrl='http://<%# Eval("CategoryName") %>.businessbazaar.in/' >
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
Then that would solve the requested issue.
However this isn't going to deal with the implicit question - which is "How do I dynamically handle subdomains" - which is where your pain is probably going to start:
For this to work in IIS, you'll need a wildcard DNS entry set up, and then a dedicated IP address for the sites in IIS that you can map all these requests on to (it appears that IIS doesn't support wildcard host header entries).
You'll then need to set up something like the UrlRewrite IIS module or similar to handle the requests and work out what it actually needs to send to your application to get the correct information back to the user.
As a point of note, most SEO people will recommend against sub domains for permanent areas of your site as they carry less weight than pages/folders beneath the main domain. So you'd be better off taking the easier option of URLs such as: http://businessbazaar.in/steel-bird-fabricators (Note also the more SEO friendly use of hyphens to separate the words rather than underscores or mashingthemalltogther).

GridView-generated code adds "width='100%'" to the table it creates

My page looks correct in Firefox and IE8. But in IE7, a nested gridview spills into the adjacent cell, much like the issue here.
Looking at it in the developer tools, there is an inline-style associated with the table that ASP.NET generated, and it has a width attribute of 100%. If I remove this, the nested table pops back where it belongs.
Problem is, nowhere is an inline-style set. In fact, if I try to set width='250px', it gets overridden with width='100%'. If I try to remove the width attribute in the code-behind, attrGridView.Attributes["Width"] is null, and calling .Remove() does nothing. But every asp.net-generated gridview-table has an inline style with width='100%' set on it (it's only causing me issues in one place).
Setting table-layout='fixed', as suggested in the article I linked to, did not help.
How do I get ASP.NET to stop setting this property?
Some code:
<asp:TemplateField HeaderText="Attributes" SortExpression="Attributes">
<HeaderStyle CssClass="GridHeaderCell" />
<ItemStyle CssClass="GridTableCell AttrGridCellPadding" />
<ItemTemplate>
<asp:GridView id="attributesGridView" runat="server"
AutoGenerateColumns="false" ShowHeader="false" GridLines="None"
AlternatingRowStyle-BackColor="White" CssClass="StupidGridView" >
<EmptyDataTemplate>
<p class="italic">There are no attributes for this request.</p>
</EmptyDataTemplate>
<Columns>
<asp:TemplateField>
<ItemStyle CssClass="AttrTableCell" />
<ItemTemplate>
<asp:Label id="attributeName" runat="server"
Text='<%# Eval("Name") + ": "+ Eval("Value") %>'
CssClass="AttrGridCell"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
.StupidGridView {
width: 250px;
}
Themes are being applied and overriding control-level settings. Check theme settings on page, web.config, or anywhere else a theme may be set.
Unfortunately, the ASP.NET html rendering is really bad.
Microsoft know that and provided Control adapters in 2006 which allow you to modify control rendering.
Instead of searching on how to override what ASP.NET render, I would advise to use CSSFriendly which provides control adapters for most ASP.NET bad-rendered controls.
If you don't need "pure-css rendering" and CSS afraid you, you can check how they do to create your own adapter.
ScottGu post on this subject from google.

Link loaded into my gridview try to navigate to my local server

I'm loading links into a gridview, but if they aren't appended with http:// it goes to my server. So something like www.yahoo.com when clicked would go to http://localhost:1304/.../controls/www.yahoo.com. How would I make the browser open a new window to whatever is in the link field when clicked besides doing string manipulation.
I've tried both asp:hyperlinkfield and templatefields
<asp:TemplateField HeaderText="Link">
<ItemTemplate>
<asp:HyperLink runat="server" Text='<% #(Eval("Link")) %>' NavigateUrl='<% #Eval("Link") %>' />
</ItemTemplate>
</asp:TemplateField>
<%--<asp:HyperLinkField DataTextField="Link" HeaderText="Link" SortExpression="Link" DataNavigateUrlFormatString="{0}" DataNavigateUrlFields="Link" Target="_blank" />--%>
Here is some source from the page. The comcast link appends itself to a local destination while the yahoo one is fine.
<td>15478963</td><td>test data - comcast</td><td>www.comcast.net</td><td align="right">12/23/2009</td><td>Justen</td>
</tr><tr style="color:Black;background-color:Gainsboro;">
<td>12345678</td><td>Update works!</td><td>http://www.yahoo.com</td><td align="right">12/23/2009</td><td>Justen</td>
This behavior (appending the url to the end of your local server) is caused by the way URL's are interpreted by browsers. To redirect to www.yahoo.com, for example, you have to use an absolute URL, starting with http://.
If your links are pointing to addresses which don't start with http://, then the browser interprets them as relative urls, in relation to the present (local server) location.
So you'll have to do some string manipulation, to check whether the address starts with http:// and add it when it's necessary. Maybe you could do it in the OnRowDataBound event handler for the GridView
Also, to make the links open a new window (or tab) in the browser, you should use the target="_blank" attribute, as pointed out in the previous answer.
You need to add a target="_blank" attribute to the link. This will open any link in a new window.
See the href target attribute documentation.

Categories