Difference between ' (single quote) and “ (double quote) in ASP.NET 4 - asp.net

I want to call MyMethod in code-behind from server control in aspx page like below.
MyPage.aspx
<asp:Label ID="MyLabel" runat="server" Text='<%# MyMethod(Eval("MyColumn")) %>'>
MyPage.aspx.cs
protected void MyMethod(object obj) { ... }
If I use " instead ' in aspx page then it will give me a compilation error The server tag is not well formed. as below.
<asp:Label ID="MyLabel" runat="server" Text='<%# MyMethod(Eval("MyColumn")) %>'> // This line work
<asp:Label ID="MyLabel" runat="server" Text="<%# MyMethod(Eval("MyColumn")) %>"> // This line error
I want to know why I need to use single-quote, is it a rule? How can I use double-quote in my situation?

I want to know why I need to use single-quote, is it a rule? How can I
use double-quote in my situation?
The use of Single quote over Double quote is just to make it clear where the string is ending. You cannot use Text="MyMethod("123")" because the Text start with the M and may end with the ( or the 3 or the last ). By using single and double quote the compiler know when the string end.
Text="MyMethod('123')"
Text='MyMethod("123")'
Your example is about binding but let say that you would like to have a double quote while using a double quote for a non-binding situation. You could have use the HTML entity "
Text="This is my string with " inside "" //This will produce : This is my string with "inside"

Related

formatting and number using Xpath and Xmldatasource

when i try to format number in asp repeater. I am using Xpath and XMl datasource. It Does not say any error, but i just keeps it, without any decimals.
<asp:Repeater ID="rptCvrInfo" OnItemDataBound="rptCvrInfo_ItemDataBound" runat="server" DataSourceID="DataSource1">
<itemtemplate>
<asp:Label ID="lblEquity" Text='<%# FormatAmount( XPath("Equity") )%>' runat="server" />
</itemtemplate>
</asp:Repeater>
My codebehind method
public static string FormatAmount(object In)
{
Decimal amount = Decimal.Parse(In.ToString(), new NumberFormatInfo() { NumberDecimalSeparator = "," });
return amount.ToString();
}
xmlnode from document
<Equity contextRef="ctx37" unitRef="DKK" decimals="-3">101015000
Assuming that 101015000 is the value you want to convert to Decimal, you have to return a formatted string from you method. All you are doing now is convert it to a decimal and then send it back exactly as it was. You can return a formatted decimal as a string.
return string.Format("{0:N2}", amount);
Or inline without a method in code behind
<%# string.Format("{0:N2}", Convert.ToDecimal(XPath("Equity"))) %>
See https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings

Tuncate string in ASP.NET using VB.NET

I made a function to truncate a string in the code behind file. But how do i use it in the aspx file?
This is the textbox:
<asp:TemplateField HeaderText="page" HeaderStyle-Wrap="true">
<ItemTemplate>
<a href='<%# makepageURL( Eval("page") )%> '>
<%# Eval("page")%>
</a>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtpage" TextMode="SingleLine" Rows="1" Width="100%" runat="server" Text='<% #Bind("page") %>' />
</EditItemTemplate>
</asp:TemplateField>
And this is my function:
Public Function TrimString(ByVal Value As String, ByVal Length As Integer) As String
If Value.Length > 20 Then
Return Value.Substring(Value.Length - (20 - 3)) + "..."
End If
Return Value
End Function
It's not an issue of how to use it, but actually when to use it?
If you had a regular span, you could do this:
<span><%: TrimString("somestring") %></span>
But this is a TextBox your dealing with (user input).
When should it truncate?
On Form Submit? (that would make sense).
As they type (well then you'd need to use JavaScript).
By the looks of your code snipper, your using a FormView.
So i wouldn't be calling it from the ASPX (which the equivalent of executing code during Page Render), i would be calling it during the Edit/Submit event, server-side event handler.
In other words, truncate the value the user put in, after they have submitted the form and before you persist to the database.

ASP.NET: single and double quotes inside the text property of a Literal

Simple question, I know, but I can't seem to find a way to put both single and double quotes into the string of the text property of a Literal in asp.net
<asp:Literal runat="server" id="Literal1" Text="This is my "text", isn't it pretty" />
For example, in the above code snippet. The string closes on the first double-quote around 'text'. I know I could replace them with single quotes (or use all double quotes and wrap the string in single quotes), but I'm not sure how to use both. Escaping the quotes doesn't seem to work.
Setting the string on the code-behind is an option, of course, where I can escape the double quotes, but I've always thought it best to keep static text on the aspx, rather than cluttering the code-behind.
You can try the HTML enitity for the quotation mark: "
<asp:Literal runat="server" id="Literal1" Text="This is my "text", isn't it pretty" />
You can use:
<asp:Literal id="literal1" runat="server">This is my "text", isn't it pretty</asp:Literal>
This should work for you
you can use double qoutes inside single quotes like this:
<asp:Literal runat="server" id="Literal1" Text='This is my "text", isnt it pretty' />
But if you want to use in text both of them, the best way to do this is in code behind
I would suggest escape characters, but I'm not aware of a way to use those inline. Instead, use code to initialize the value.
<asp:Literal runat="server" id="Literal1" Text="" />
...
Literal1.Text = "This is my \"text\", isn't it pretty?";
Alternatively, you can use HTML encoding as suggested elsewhere.
<asp:Literal runat="server" id="Literal1" Text="Isn't "it" pretty?" />
I would like to suggest string.format...
...
Literal1.Text = string.format("{0}","This is my text, isn't it pretty?";

How do I concatenate 2 resource strings together in an aspx page

I have a localised ASP.net application (.net 2.0). I wish to concatenate 2 strings retrieved from the resource file together into one element, something like this.
Text="<%$ Resources:Resource, lw_name %>" + <%$ Resources:Resource, lw_required %>"
I have tried using Eval without success. Is what I am trying to do the "correct" approach or can I store strings with placeholders in the resource file and interpolate them "on the fly".
I am trying to do this in the aspx file rather than in code-behind.
ASP.NET tag attribute values that use <%$ Something: Something Else %> are of a special syntax called ASP.NET Expressions. Using them as attribute values are pretty much all-or-nothing; there's no way to add any code into the ASPX file to manipulate what those expressions evaluate to. You'll have to do this in the code-behind.
I search for the solution so long
This code works for me:
ToolTip='<%# Resources.Global.Btn_Edit + "/" + Resources.Global.Btn_contact %>'
< asp:HyperLink ToolTip='<%# "Some Text :" + Eval("id").ToString() %>' ....../>
Do you mean something like this.... ToolTip='...' -> Convert your return values to STRING... ( xxxx.ToString() )
Like this it displays: Some Text: 1234 --> on Tooltip
so you should do something like this in your case:
Text="<%$ (Resources:Resource, lw_name).ToString() %>" + <%$ (Resources:Resource, lw_required).ToString() %>"
I don't know if it's going to work but try to conver to ToString().
I know you said you tried eval but what about something like this:
Text='<%# string.Format("{0}{1}",Eval("lw_name"),Eval("lw_required")) %>'
I was having the same issue, and I solved it by using this option instead:
Text="<%= HttpContext.GetGlobalResourceObject("Resource", "lw_name") %> <%= HttpContext.GetGlobalResourceObject("Resource", "lw_required") %>"
For local Resources, use the GetLocalResourceObject method instead of GetGlobalResourceObject
Try
"#(Resources.ResourceString + Resources.ResourceString)"
Use this method to append 2 strings in ASPX.
Text='<%# String.Format("{0} {1}",
Resources.file01.string1,Resources.file01.string2)%>'
This Might Be of Help
<asp:Label ID="Mylabel" runat="server">
<%= "before Message String- "+ Resources.MyResources.Message +" -After Message String " %>
</asp:Label>
Note the concatenation is not on the Text attribute but between the label element
Full post can be found here

How to pass multiple parameters to Eval()?

i have a bit of aspx code that uses Eval to generate a call to a javascript function:
ASP.NET (wrapped for readability):
<asp:LinkButton runat="server"
OnClientClick='<%# Eval(
"NodeGUID",
"return DoStuff(this, \"{0}\");") %>'
Text="Do stuff" />
this generates javascript similar to:
Javascript (wrapped for readability):
return DoStuff(this,
"3F2504E0-4F89-11D3-9A0C-0305E82C3301"
);
Note: i've converted the generated " entities references into quotes for readability.
i now need to add a 3nd parameter to the javascript function call, a caption:
Javascript (wrapped for readability)
return DoStuff(this,
"3F2504E0-4F89-11D3-9A0C-0305E82C3301",
"AllisonAngel.jpg"
);
Note: i've converted the generated " entities references into quotes for readability.
There already exists a function in the code-behind file that is used to return the caption for an item:
C# (code omitted for readability):
protected string GetItemText(MySomething item)
{
...
}
i know that the above function can be called from the aspx file using a syntax similar to:
ASP.NET (wrapped, code omitted, for readability):
<asp:LinkButton ... runat="server"
Text="<%# GetItemText((MySomething)Container.DataItem) %>"
... />
So now i want to use this function to include the 3rd parameter to the javascript function.
Starting from:
<asp:LinkButton runat="server"
OnClientClick='<%# Eval(
"NodeGUID",
"return DoStuff(this, \"{0}\", \"Todo - Insert caption here\");") %>'
Text="Do stuff" />
i need to change: "Todo - Insert caption here"
into a call to: <%# GetItemText((MySomething)Container.DataItem) %>
Blindly trying the obvious:
ASP.NET (wrapped for readability):
<asp:LinkButton runat="server"
OnClientClick='<%# Eval(
"NodeGUID",
GetItemText((MySomething)Container.DataItem),
"return DoStuff(this, \"{0}\", \"{1}\");") %>'
Text="Do stuff" />
But that complains, since Eval() only takes two parameters.
i tried the slightly less obivous:
ASP.NET (wrapped for readability)
<asp:LinkButton runat="server"
OnClientClick='<%# Eval(
"NodeGUID",
"return DoStuff(this,
\"{0}\",
\""+GetItemText((MySomething)Container.DataItem)+"\");") %>'
Text="Do stuff" />
But that doesn't work either.
Related Questions
ASP.NET: How to access repeater generated elements from javascript?
asp.NET: How to access repeater generated elements?
The trick isn't to pass multiple items to an eval, but to pass multiple eval's to whatever you want to use to format the data. You could also have just done it this way - which would have kept the presentation in the aspx file like you wanted...
<asp:LinkButton
runat="server"
OnClientClick='<%# string.Format(
"return DoStuff(this, \"{0}\", \"{1}\");",
Eval("NodeGUID"),
GetItemText((MySomething)Container.DataItem)) %>'
Text="Do stuff" />
My Trick
Eval("FLName", Eval("FLFDID","{0}")+"/{0}")
Eval inside Eval
It Work for me !!
Robert C. Barth gave me the idea that solves the problem:
<asp:LinkButton runat="server"
OnClientClick="<%# GetItemClientClick((MySomething)Container.DataItem) %>"
Text="Do stuff" />
and then the code-behind file contains:
protected string GetItemClientClick(MySomething item)
{
...
String szOnClientClick =
"return DeleteItem(this, "+
Toolkit.QuotedStr(item.NodeGUID.ToString()) + ", "+
Toolkit.QuotedStr(GetItemText(item))+");";
return szOnClientClick;
}
i really would have preferred to keep presentation in the aspx, and business logic in the code-behind - but the real world often doesn't conform to that model.
If the LinkButton is not inside some other thing (like a grid), just set the OnClientClick attribute in the code-behind like normal, using string.Format.
If it IS in a grid (or repeater, or something of that nature), set it in the RowDataBound event using FindControl or e.Row.Cells[] and string.Format. If you use e.Row.Cells[], you can probably dump the LinkButton server control and just output a normal anchor tag.
Continued from Scott Ivey's solution, you can also use this to include if statements in your Eval statement:
For example the below will output an anchor tag if the property Url exists, otherwise it will output a span.
<%# string.Format(Eval("Url") != null ? "{1}" : "<span>{1}</span>", Eval("Url"), Eval("Text")) %>">
you can try this
'<'asp:HyperLink ID="hp" runat="server" CommandArgument='<%# Eval("SysID", "{0}")%>'
NavigateUrl='<%# String.Format("~/ReportViewer.aspx?RevID= {0}
& User={1}", Eval("ID"), Eval("USER")) %>' Target="_blank" %>'>
</asp:HyperLink>
http://afzal-gujrat.blogspot.com/2012/10/pass-more-evals-in-querystring-with.html

Resources