简体   繁体   中英

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

<div visible="false" runat="server"><a href='<%#string.Format("{0}removeAllItems=true", this.Page)%>' onclick="return confirm('Are you sure you want to remove all items?')">Remove all items</a></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.

Eg 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM