ASP.NET tags don't expand in OnClientClick - asp.net

I have the following two buttons:
<asp:Button ID="btnVote" runat="server" Text="Vote!" PostBackUrl="<%$RouteUrl:id=2, routename=Results%>"/><br />
<asp:Button ID="btnResults" runat="server" Text="Results ->" OnClientClick="location.href='<%$RouteUrl:id=2, routename=Results%>'"/>
The first <%$ %> expands as intended, while the second (identical!) one gets used as typed ( = not expanded). I am very new to ASP.NET, coming from PHP, and this is from my learning/test site.
What am I doing wrong and how can I fix it?

The ASP.NET expression syntax (<%$ ... %>) can only be used to directly assign values to properties of server controls. I think the problem you're having is that the expression syntax is embedded within a string, and not directly bound to the "OnClientClick" attribute.
Trying changing your second button to
<asp:Button ID="btnResults" runat="server" Text="Results ->" OnClientClick="<%$RouteUrl:id=2, routename=Results%>" />
If that works, you may need to modify your expression to return that extra text you need. Or, create another route that returns said info (the current value wrapped in the "location.href" attribute).
You can find more information about these expressions here: ASP.NET Expressions Overview.

Related

Data binding simple variable not working

This is the page code for the control:
<asp:TextBox id="someID" maxlength="10" columns="10" runat="server" Text="<%# work %>" />
This is how I set up and populate the variable before page is rendered, it is in the code behind, I have tried I think all the variations available for the variable declaration, e.g. public, shared, protected, etc.:
Public work As String
work = "987654321"
The textbox always comes up blank. referred to this ms kb page for how this work and it has a specific example.
However, it didn't explain anywhere if there is some special way of declaring the variables used in the binding or some special way to set the value of the variable, or is there something needed to allow the <%# syntax to work?
Binding doesn't happen automatically. You have to call DataBind() either from Page control or the text control. Try put it in Page_Load() method.

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") +" );" %>'

Why does databound property of ASP.NET usercontrol work only without quotes?

I have a custom ASP.NET user control implemented fully in code-behind. The control has one string property and its declarative markup looks like this:
<uc:MyControl ImageUrl="/Content/images/" runat="server" />
Most likely the actual declaration will use data binding syntax like this:
<uc:MyControl ImageUrl="<%# PageInfo.ImageUrl %>" runat="server" />
Now here's the odd part pertaining to my question. If I use the above syntax, the data binding does not work and the value of ImageUrl at run-time is the string literal of whatever is between quotes. However, if I remove the double quotes, it works as expected:
<uc:MyControl ImageUrl=<%# PageInfo.ImageUrl %> runat="server" />
The same behavior occurs with both double and single quotes. I am puzzled by this and although the code is working it's really not optimal as putting values in quotes is the norm and the approach to make the data binding work is decidedly unorthodox.
Anyone have any ideas on why this only works without quotes?
I found the problem...it was something to do with the file (probably encoding, but not sure). On a whim, I copied the code to a new file and deleted the original. Now the data binding works with the quotes as it should.

asp.net button use javascript return function

I have an asp:button control and am using the "CommandName" and "CommandArgument" parameters in the .aspx file. The CommandName is found with <%# Eval("Name") %>, but the CommandArgument has to be found via a javascript function. However, I'm not quite sure how to do this inline. The function returns a number (integer), which is to be used as the value of the CommandArgument. Is it possible to do this?
Thanks
EDITED TO ADD CODE
I've added an example code (don't have access to the real code atm). However, basically, the CommandArgument should be the value returned by the function CalculateLength().
function CalculateLength(a,b) {
return a*b;
}
<asp:Button ID="btnUpdate" runat="server" Text="Update" CommandName=<%# Eval("Name") %> CommandArgument= ??? //Should be result of CalculateLength(a,b).
I am not sure how you are going to get the javascript to work because the aspx code is running server side and building the output for your button. By the time the javascript runs the page code has already been built and the button's html and attached javascript as well.
Is there anyway to calculate the function server side and then just do:
CommandArgument="<%= CalculateLengthServerSide() %>"
You don't need to just use the data your binding to, you can call any server side function.
Edit: Try switching your label that stores the quantity to a textbox and making it read only so uses will not mess with it. After the button is clicked, you should be able to find the textbox control and read out the posted value.

referencing appSettings from usercontrols

In my .ascx usercontrol i'm trying to dynamically generate links using a value i've stored in web.config.
link
and when i try running, i get a parser error
Literal expressions like '<%$appSettings.MYPATH %>' are not allowed. Use <asp:Literal runat="server" Text="<%$appSettings.MYPATH%>" /> instead.
I know i'm probably missing something relatively minor.
<%= ConfigurationManager.AppSettings["myKey"] %>
EDIT:Don't forget the =
link
should work (it at least does on the IIS server I use). (Unfortunately it's more verbose)
Try this instead
.ascx
<asp:Literal ID="Literal1" runat="server"></asp:Literal>
in code behind
Literal1.Text = "link"
More accurate answer will be next:
Link
Use a colon instead of a dot and add runat="server":
link
The documentation isn't very clear on this point, but ASP.Net Expressions are for use within server tags. Thus, if you want to use one in a plain html tag, you must add runat="server" so that the tag is processed on the server where the expression will be evaluated.

Resources