The $ sign in ASP.NET programming? - asp.net

When does this $ sign come into the picture? It bothers me. What is its significance?
<asp:literal runat="server" text="<%$ Resources:MyResource, StringId %>" />
as seen in this thread:
Using an explicit localization expression for a page title?
Does this kind of .NET syntax have a name? I'm guessing it only works for a certain asp.net version (i.e. asp.net 3.5)? Much thanks.

It means that what comes after the dollar is an expression, see this MSDN article. It works in .net 3 & 4

The example you posted is documented here: ASP.NET Expressions Overview
It's used for expression binding, usually with localized string resources.

The $ expression binding allow us to extract custom application settings, connection string and resource information from configuration and resource files respectively.
The $ expression is a code sequence that processes the expression and replaces it with a string value in the final HTML.

Related

How to use session.contents as a variable in ASP SQLDataSource Visual Studio 2017

I am using Visual Studio 2017 to build an ASP.NET website (using VB) and have set up a few different connection strings in my web config file for different user types (i.e. basic access, admin access, etc.). Each connection type has access to different rights as well as different schemas in the DB. I'm trying to call these different connections via a session contents variable that is generated at the time of a successful login (on the login page) and then used in other pages on the site.
This is an example of the ASP connection string used on one of the other pages:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$
ConnectionStrings:DBSource1 %>" SelectCommand="Stored_Proc"
SelectCommandType="StoredProcedure"></asp:SqlDataSource>
Here's the part where I would like to call via session contents to use as a variable in the SQLDataSource:
ConnectionString="<%$ ConnectionStrings:DBSource1 %>"
Here's the Session content code I'd like to use as a variable:
Session("SqlConstrName").ToString()
I am able to put the session content into a text box in the ASP code and it gives the connection name as it's shown above
ConnectionStrings:DBSource1
I have done some research and know that I cannot use <%= % for server side controls and have been unsuccessful using <%# %, "<%# %", or any other combination type to pass the variable through. I have also tried to bind just the name part and exclude the "ConnectionStrings:" part, but was still unsuccessful.
I know that I can easily create a connection string in my VB code and then bind the data to the objects, but I am attempting to do this on the ASPX side so that my drop-downs, grid-views, etc. stay bound which lowers the amount of VB code needed on the backend and allows me to have more dynamic tables that link between other items on the page without having to write a bunch of on change events. Any suggestions or thoughts is greatly appreciated, thanks!
If your scenario is exactly the same as the drag'n'drop control designer envisaged then you should have no problems.
If you need any deviation from what they created then you are probably better off writing the code yourself.
Either way, there is code somewhere - the difference is between you controlling the code you wrote or you fighting against the code someone else wrote.
IMHO.

What is this the <%$ syntax in ASP.net about? [duplicate]

This question already has answers here:
asp.net <%$ ... %> syntax
(5 answers)
Closed 8 years ago.
For the first time I have come across this asp.net syntax:
<%$ AppSettings:ValueFromConfig %>
OR
<% $AppSettings:ValueX %>
What is this about? It's definitely not application level code because the dollar would have caused an issue. This appears in ascx and/or aspx front end pages.
I found the following:
The basic syntax of an ASP.NET expression is the following:
<%$ expressionPrefix: expressionValue %>
The dollar sign ($) indicates to ASP.NET that an expression follows.
The expression prefix defines the type of expression, such as
AppSettings, ConnectionStrings, or Resources. Following the colon (:)
is the actual expression value that ASP.NET will resolve.
Expression syntax is not bound to any specific .NET language. You can
use the same expression syntax whether you use Visual Basic, C#, or
any other programming language in your ASP.NET pages.
~ http://msdn.microsoft.com/en-us/library/d5bd1tad.aspx
This is one of the hardest questions to search for as most search engines ignore the $ sign in the question.
See also:
Introduction to ASP.NET inline expressions in the .NET Framework: http://support.microsoft.com/kb/976112

asp.net <%$ ... %> syntax

I'm trying to make the switch from Java to .NET.
I've noticed a number of ASP.NET pages have <%$ sometext %> in them. Can someone explain what this does in a couple of sentences, or point me to a reference on the syntax?
It's expression builder syntax, and it's used commonly to access settings in the web.config. Here's an example using expression builder syntax to get a connection string:
ConnectionString="<%$ ConnectionStrings:sqlconnection %>"
Here's a good article that explains all of the inline expressions:
http://support.microsoft.com/kb/976112
The expression builder is used to set values of control properties based on the information that is contained in an application's configuration or resource files. The following is the basic syntax of the expression builder:
<%$ Expression Prefix: Expression Value %>
The dollar sign ($) indicates to ASP.NET that the following expression is an expression builder. The expression prefix defines the kind of expression, such as AppSettings, ConnectionStrings, or Resources. Additionally, you can create and define your own expression builder. The expression value that follows the colon (:) is what ASP.NET will actually use as the value of a certain property.
It references what is called an "Expression Builder". It's just a component that can plug into the parsing mechanism. The expression builder is fed the content of the expression, and it is responsible for returning CodeDOM expressions that describe how to get the actual value.
I've implemented a generic expression builder that lets you put any code in it:
http://weblogs.asp.net/infinitiesloop/archive/2006/08/09/The-CodeExpressionBuilder.aspx
It's inline codebehind.
Here's a link to some more info
http://naspinski.net/post/inline-aspnet-tags-sorting-them-all-out-(3c25242c-3c253d2c-3c252c-3c252c-etc).aspx
<%$ expressionPrefix: expressionValue %> is used for expressions. Where the expressionPrefix is the expression builder it uses, and the expressionValue is the actual expression that gets passed to the expression builder.
An example usage: <%$ AppSettings: greeting %> which would read out the greeting from the application configuration. Various expression builders are supplied by default such as:
AppSettings
Resources
ConnectionStrings
It is also possible to create your own custom expression builder(s).
This page gives a nice overview of various available ASP.NET tags. Although it is missing <%: %> which HTML encodes the supplied contents.
It is called expression and is used for various things including reading from web.config, app settings and resource files for localizations. Resource expressions are probably the most used form of expressions. Instead of putting the static text in the controls, this expression can be used and the ASP.NET runtime would pick the resource file for the current culture and extract the value from it.

ASP.NET 4.0 DropDownList with single quotes in text

We have a asp:DropDownList that we populate server side with
ddlBranch.Items.Add(new ListItem("TEST","This is a's test"));
When this is compiled and run under .NET 3.5 we see the text "This is a's test"
However when this is compiled and run under .NET 4.0 we see the text "This is a's test"
We have added the following to our web.config and there was no change.
<pages controlRenderingCompatibilityVersion="3.5" />
For the time being we have dropped back to .NET 3.5 however we would like to know if there is a way to work around this or if this is a known rendering issue or is by design.
TIA
AJ
Hi All
Thanks for the responses and they led me to look deeper into the code looking for an Encode somewhere. It turns out there that was a:
Server.HtmlEncode(input)
being performed on all controls in a base page class.
Now what I thought was a problem really turns out to be a case of RTFM on my part
From http://www.asp.net/learn/whitepapers/aspnet4/breaking-changes
HtmlEncode and UrlEncode Now Encode Single Quotation Marks
In ASP.NET 4, the HtmlEncode and UrlEncode methods of the HttpUtility and >HttpServerUtility classes have been updated to encode the single quotation mark character >(') as follows:
The HtmlEncode method encodes instances of the single quotation mark as ' .
The UrlEncode method encodes instances of the single quotation mark as %27.
So when I was using .NET3.5 my single quote ( ' ) was being ignored by the HtmlEncode but when switching to .NET 4.0 it was not being ignored by HtmlEncode.
Thanks again for all the responses and work that people put in to this question.
Regards
AJ
When you get the value back you could just HTMLDecode the selected value.
ie. Server.HtmlDecode(ddlBranch.SelectedValue)
Why do you believe this is a problem? ' renders as an apostrophe, and when posted will turn into an apostrophe if that value is selected.

jQuery autocomplete in ASP.NET webforms?

Has anyone used jQuery to populate an autocomplete list on a textbox using ASP.NET webforms? If so, can anyone recommend a good method? From my reading so far, it seems like most people are using delimited lists rather than JSON to bring the items back. I'm open to any ideas that will get me up and running rather quickly.
I made a tutorial to do this with asp.net mvc but it should be almost identical for traditional webforms:
http://blogs.msdn.com/joecar/archive/2009/01/08/autocomplete-with-asp-net-mvc-and-jquery.aspx
There are many, many examples on the web. I've used this one before, and if I recall you only need to create an aspx that will return matching terms as a <BR/> separated list:
http://www.dyve.net/jquery/?autocomplete
The documentation shows php in the example, but there's no difference in the way the plugin itself works and I didn't have to do anything special as a result.
From the documentation:
> $("#input_box").autocomplete("my_autocomplete_backend.php");
In the above example, Autocomplete
expects an input element with the id
"input_box" to exist. When a user
starts typing in the input box, the
autocompleter will request
my_autocomplete_backend.php with a GET
parameter named q that contains the
current value of the input box. Let's
assume that the user has typed
"foo"(without quotes). Autocomplete
will then request
my_autocomplete_backend.php?q=foo.
The backend should output possible
values for the autocompleter, each on
a single line. Output cannot contain
the pipe symbol "|", since that is
considered a separator (more on that
later).
An appropiate simple output would be:
foo
fool
foot
footloose
foo fighters
food fight
I wrote an Asp.Net WebControl and some Asp.Net MVC extension methods wrapping the JQuery UI autocomplete widget.
I wrote documentation as well about how to implement a working resource providing a JSon result.
You can find it at:
http://autocompletedotnet.codeplex.com/
Hope it can help

Resources