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
Related
I have fuction to show the total of items in footer of GridView by entering the values from TextBox and its works fine with me but if user forget to enter value and do inserting I have this error
Input string was not in a correct format.
I want to set default value to that TextBox "Zero" so if the user did not enter any value it will take it as default value I mean Zero.
//The fuction code is
<script runat="server">
Dim TotalUnitPrice As Decimal = 0.0
Function GetUnitPrice(ByVal Price As Decimal) As Decimal
TotalUnitPrice += Price
Return Price
End Function
Function GetTotal() As Decimal
Return TotalUnitPrice
End Function
</script>
HTML Code
<asp:TemplateField HeaderText="Creditor" FooterStyle-Font-Bold="True">
<ItemTemplate>
<%# GetUnitPrice(decimal.Parse(Eval("tabDebit").ToString())).ToString() %>
</ItemTemplate>
<FooterTemplate>
<%# GetTotal().ToString()%>
</FooterTemplate>
<FooterStyle Font-Bold="True" />
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
Check out this post: Using '<%# Eval(“item”) %>'; Handling Null Value and showing 0 against
Something like - <%#(String.IsNullOrEmpty(Eval("Data").ToString()) ? "0" : Eval("Data"))%>
I am not an expert in ASP.NET but probably it will be a lot easier if your functions receive a string and return a string
...aspx.vb....
Dim TotalUnitPrice As Decimal = 0.0
Function GetUnitPrice(ByVal Price As String) As String
Dim temp As Decimal = 0.0
if decimal.TryParse(Price, out temp) Then
TotalUnitPrice += temp
End If
Return temp.ToString()
End Function
Function GetTotal() As String
Return TotalUnitPrice.ToString()
End Function
....aspx....
<ItemTemplate>
<%# GetUnitPrice(Eval("tabDebit")) %>
</ItemTemplate>
<FooterTemplate>
<%# GetTotal()%>
</FooterTemplate>
Let see if someone more experienced in ASP.NET points to a better way to do it
I tha the following markup:
<asp:ListView ItemType="MyNamespace.MyEntity" runat="server" >
<listiview tags>
<asp:Label ID="lblDebtAmount" runat="server" Text="<%#:string.Format("
{0:C}",Item.Amount) %>"
</listiview tags>
</asp:ListView>
In the given example i'm trying to apply currency format to the text rendered by a label, but i'm after achieve this for text controls in general (Labels, Literal, Textbox, etc) . The problem is that the above produce the following server error:
"Analizer Error Mesage: Invalid Server Tag"
The same result if i try with the following:
<asp:Label ID="lblDebtAmount" runat="server" Text="<%#:string.Format("
{0:C}",Item.Amount.ToString("C")) %>"
Another approach, this time i get runtime exception:
<%# String.Format("{0:C}", Eval(Item.Amount.ToString() )) %>
DataBinding: 'System.Data.Entity.DynamicProxies... does not contains a
property named '500000,00'.
Another approach that i have tryied is formating the entity's property:
[DisplayFormat(DataFormatString = "{0:C}")]
public decimal Amount { get; set; }
That does not produce any error but neither got the text formated, the label just render the value without format
The only thing that seems to work is using the old way databinding:
<%# String.Format("{0:C}", Eval("Amount")) %>
But if i'm going to use strong typed binding for Webforms i dont want to be using strings to resolve properties value anymore:
I think this has to be dead easy, any thought?
Try switching from double quotes to single quotes for the text property, the way you currently have it ends the string early so the text property is actually just "<%#:string.Format("
<asp:ListView ItemType="MyNamespace.MyEntity" runat="server" >
<listiview tags>
<asp:Label ID="lblDebtAmount" runat="server" Text='<%#:string.Format("
{0:C}",Item.Amount) %>'
</listiview tags>
</asp:ListView>
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"
use a reapeter to display data, but sometimes the data is to big to be displayed in a cell. Can I use a method to allow the reapter to display the first 100 characters of a string and after that a hyperlink to display next page? any help is welcome!
You don't have the implementation details of your code, so this is a shot in the dark. If you are binding an object, create a new property that takes the first 100 characters of the information you want displayed:
class Foo
{
public String PropertyData {get;set;} //your real data;
public String DisplayData //bind the reader to this property instead.
{
get
{
return PropertyData.substring
(0, (PropertyData.Length >= 100) ? 100 : PropertyData.Length);
}
}
}
You can have the property return anything you want, this is just an example of how to get it to display only 100 characters.
You can leave this logic in the view:
<asp:Label runat="server"
Text='<%# DataBinder.Eval(Container.DataItem, "your_text_field").ToString().Substring(0, Math.Min(100, DataBinder.Eval(Container.DataItem, "your_text_field").ToString().Length %>' />
<asp:Hyperlink runat="server" Test='<%# Eval("your_text_field") %>'
Visible='<%# Eval("your_text_field").ToString().Length > 100 %>' />
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.