How to bind Request.QueryString[""] to Eval() in ASP.NET - asp.net

ImageUrl='<%#Eval("Name","../Master Pages/DisasterImages/") %>'+'<%#Eval("Request.QueryString["DisID"].ToString()/{0}") %>'
DisID is a folder name.
What I want is to display the images in the directory.
I have done it using a repeater control. The problem is now I want to get the folder name in the QueryString. How can I do this? Above is the code I have tried without achieving the desired outcome...
Any help would appreciated.
Thank you!

use <%= instead of <%#. Example:
<%= Request.QueryString["DisID"] %>
I have noticed another issue with your code, you need to be careful when using quotes inside a string. You need to escape them. Therefore
"Request.QueryString["DisID"].ToString()/{0}"
should look like this
"Request.QueryString[\"DisID\"].ToString()/{0}"
Notice the backslash that serves for escaping characters.

Related

Image Hyperlink in ASP.NET - MVC 4

I try to create a project for the first time in asp.net (mvc4).
and what i try to do is to create a image which is a hyperlink to go to the index page.
i have search a lot of things and it shows very simple to do that.
but i can´t understand why it doesn´t work for me.
someone can give a hand?
Code:
<a href="<%= Url.Action("Index","Home")%><img src="~/Content/imagens/nav-arrow-back.png"/></a>
The Action is "Index" in the controller calls Home.
you miss a quote
<a href="<%=Url.Action("Index","Home")%>"> ...
^
about this quote you missed
For bad request, fix the whole <img> part
<img src="<%=Url.Content("~/Content/imagens/nav-arrow-back.png")%>"/>
First up, as previously noted you're missing a closing quote on that href. Second, MVC 4 doesn't use the <% %> syntax, at least not by default; it should be using Razor v2 which uses #, so your code should look like this:
<img src="~/Content/imagens/nav-arrow-back.png"/>
If you use the old syntax I assume it would try to handle the actual text <%= Url.Action("Index","Home")%> as a URL, which clearly won't work.

CKEditor breaking custom .NET tags by converting single quotes to double quotes

At the client's request, we just upgraded a custom CMS system for a large site from FCKEditor 2.x to CKEditor 3.5.3.
Inside an ItemTemplate I have a custom UserControl tag in which the attributes are populated by DataBinding, like so:
<my:Viewer runat="server">
<ItemTemplate>
<my:CustomTag runat="server"
ImageUrl='<%# DataBinder.Eval(Container.DataItem, "ImageUrl") %>' />
</ItemTemplate>
</my:Viewer>
So, the point is that the above works just fine. However, when the HTML is put into the latest CKEditor, CKEditor changes the ImageUrl attribute to use double-quotes instead of single quotes. Once it's changed to double quotes, it causes a parsing error on the .aspx page. Changing: "ImageUrl" to "ImageUrl" works, but it's not ideal for our client who is going to have to update every page that exists in a very large CMS system. So, I'm asking this question hoping someone might know of a way to toggle CKEditor to use single quotes in HTML attributes by default instead of double quotes to reduce the amount of work my client is going to have to do.
I'm only looking for easy configuration-type changes, not patching the editor, etc.
This should do what you want
Taken from here
http://cksource.com/forums/viewtopic.php?f=11&t=20647&sid=f47526ecfb1f2303ad0b923ceed7aafe&start=10
To avoid CKEditor changing special chars:
switching in source view:
CKEDITOR.instances.TEXT.on( 'mode', function(ev) {
if ( ev.editor.mode == 'source' ) {
var str=ev.editor.getData();
str=str.replace(/&/g, "&").replace(/>/g, ">").replace(/</g, "<").replace(/"/g, "\"");
ev.editor.textarea.setValue(str);
}
});
When save edited document:
var html=CKEDITOR.instances.TEXT.getData()
html=html.replace(/&/g, "&").replace(/>/g, ">").replace(/</g, "<").replace(/"/g, "\"");
I'm going to say that the " solution that I mentioned being too much work is simply the only answer...just to put some closure on this. Or, if I can find a way, I'll withdraw the question. Thanks rqmedes for trying...I'd actually forgotten all about this question until I got your response
:)

Escape double quotes in asp.net Onclick string

I am trying to insert dynamic data into a onclick property for a control the code looks like this
onclick="openRadWindow('<%#Eval("ID";) %>','<%#Eval("ParentObjectID") %>');"
I cant get it to fire and the trouble seems to be the double quotes, what is the correct method on escaping the quotes so that this fires.
You can do use a format string, like this:
onclick='<%# string.Format("openRadWindow(\"{0}, {1}\");", Eval("ID"), Eval("ParentObjectID")) %>'
Is the event just not firing or are you getting any javascript errors as well. Also, I would look at the HTML after the page has been rendered and make sure that the server tags are being processed correctly. There are certain uses that cause them not to actually be processed and will remain <%# Eval("ID") %>.
Thanks to all I was able to get it working correctly using a different method. in the code behind I created a function and in the function I put the following code
Return String.Format("openRadWindow({0},{1});", photo.ID, photo.ParentObjectID)
and in the aspx I added onclick="<%#MyFunction(DirectCast(Container.DataItem,Photo))%>
First thing I see is that semi-colon after "ID" - I think that might be causing your problems.

How to remove portion of url when using NavigateURL

I am using NavigateURL to dynamically pull in the url of products on a receipt page.
Here is the exact code:
<a class="blue13" href="<%#Eval("Product.NavigateUrl")%>"><%#Eval("Product.Name")%></a>
It is placing "/checkout/~/" in each of the url.
How can I remove or correct this?
Thanks!
The simplest thing would probably be to just call .Replace() and replace the unwanted part with a empty string. But it depends relay. Why is it there to begin with? Where is the data coming from?
I ended up switching from a regular href to an asp:HyperLink and it corrected the /~/ issue.
So, before I was using
<a class="blue13" href="<%#Eval("Product.NavigateUrl")%>" runat="server"><%#Eval("Product.Name")%></a><br/>
And I switched it to:
<asp:HyperLink CssClass="blue13" runat="server" NavigateUrl='<%#Eval("Product.NavigateUrl")%>' Text='<%#Eval("Product.Name")%>'></asp:HyperLink>
Which correct the issue.
Thanks.

String.Format in .aspx not showing rest of text after

<div visible="false" runat="server">Remove all items</div>
when I run this, it doesn't show the querystring portion, just the page.aspx. I don't see why the rest of that string after {0} is being cut off.
The problem with the question mark probably has something to do with using data binding (<%#...%>) instead of simple output (<%=...%>).
String.Format is overkill, as you only want to concatenate two strings:
<a href='<%=String.Concat(this.Page, ".aspx?removeItems=true")%>' >text</a>
Or simply putting the second string in the markup:
<a href='<%=this.Page%>.aspx?removeItems=true' >text</a>
Your string concatenation is unnecessary; have you tried this?
<a href='<%#string.Format("{0}.aspx?removeItems=true", this.Page)%>' >text</a>
Since it's ignoring the ?, try this:
<a href='<%#string.Format("{0}.aspx{1}removeItems=true", this.Page, "?")%>' >text</a>
The inline tag <%# is used for databinding, yet this.
Page isn't a databound property. Switch that out to <%=, which is equivalent to Response.Write & see if that works.
It's hackish, but sometimes that's what it takes in asp.net.
E.g. if you're using StringBuilder to create a javascript string at runtime and you try StringBuilder.AppendFormat, you can't have any other braces besides the Format braces. you can overcome that problem in a similar fashion to my answer using one string.format method and injecting "{" and "}".
The "?" issue may be a problem of codepage error handling withing databinding tags. For more information on this, see: http://support.microsoft.com/kb/893663

Resources