String.Format in .aspx not showing rest of text after - asp.net

<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

Related

use appsetting key in mvc view with string

I am trying to assign appsetting key value + string in MVC view.
I tried #System.Configuration.ConfigurationManager.AppSettings["myKey"].ToString()+"Custom/CustomerProfile.aspx", but did not worked.
Done
This is how I would do it, assuming you want to use that string in your href attribute of your link:
#{
var href = System.Configuration.ConfigurationManager.AppSettings["myKey"].ToString() + "Custom/CustomerProfile.aspx";
}
Done
But, it looks like you are not using Razor, because your link ends in .aspx, not .cshtml. Is that right?
Put parenthesis around the code to make it an explicit expression:
<a href="#(
System.Configuration.ConfigurationManager.AppSettings["myKey"].ToString()+"Custom/CustomerProfile.aspx"
)" id="doneLink" class="btn btn-primary">Done</a>
Below is a handy quick reference from Phil Haack:
http://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx
This works for me. What specific error are you getting?
<a href="#System.Configuration.ConfigurationManager.AppSettings["key"]" />

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.

how to escape characters when using server side delimiters

So, currently, within an asp gridview, I have the following
<span id="btnEdit" runat="server" onclick="ShowEditCriteriaFilterDialog('<%#Eval("intSMCID")%>', '<%#Eval("strDescription")%>')" class="linkText">Edit</span>
What I'm essentially looking for is the syntax for quotes/double-quotes to actually accomplish this properly, as what I have above doesn't properly work.
First of all, if i encapsulate the entire onclick with single quotes, and not put any other quotes inside, it works for rendering purposes, but when i actually click the link at runtime, nothing happens.
If I encapsulate the entire onclick with double-quotes, like most properties of an ASPX element, it doesn't render properly, and everything after the comma which is after the first <%#Eval %> statement shows up as actual text on the screen. This leads me to believe there needs to be some escaping done to prevent it from thinking the click handler ends somewhere in the middle of that <%#Eval %> statement.
note that if I take away runat="server" and just encapsulate it in double-quotes, that seems to work better...but i need the span to be a server-side control for alot of other functionality I have in the page's code behind where I need to access the control through FindControl
The Eval function needs double-quotes, so you have to wrap the attribute value in single quotes.
You can't mix static content and <%# ... %> blocks in a single property value for a control with runat="server", so you need to build the entire string within a single <%# ... %> block.
Within a string in a <%# ... %> block, you can use &apos; to insert a single quote:
EDIT That only works in .NET 4.0; in 2.0, it generates &apos;, which won't work. Use double-quotes instead:
onclick='<%# string.Format(
"ShowEditCriteriaFilterDialog(\"{0}\", \"{1}\")",
Eval("intSMCID"), Eval("strDescription")) %>'
Depending on your data, you might also need to encode the string value for JavaScript:
onclick='<%# string.Format(
"ShowEditCriteriaFilterDialog(\"{0}\", \"{1}\")",
Eval("intSMCID"),
HttpUtility.JavaScriptStringEncode(Eval("strDescription", "{0}"))) %>'
(Code wrapped for readability - it needs to be on a single line.)
I think the solution is
onclick='<%# "ShowEditCriteriaFilterDialog(" +Eval("intSMCID") + ","+ Eval("strDescription") +" );" %>'

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 bind Request.QueryString[""] to Eval() in 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.

Resources