Figure there is a simple solution to this problem but I have been unable to find it.
I have databinding in an ASP.Net application to a GridView. This gridview is bound to an ObjectDataSource as per standard usage.
The problem I have is that one of my bound fields uses the property DataFormatString="{0:C}" and due to the currency format being displayed when an update is attempted and the object recreated I get a error as such "$13.00 is not a valid value for Decimal."
Clearly this is a result of the column using a FormatString and then attempting to bind it back to a decimal property I have in my object called UnitPrice.
I am assuming there is some markup I can set that can specify how the value is translated back?
Thanks in advance for any help.
For anyone curious the solution ended up looking like this...
<asp:TemplateField>
<HeaderTemplate>
UnitPrice
</HeaderTemplate>
<EditItemTemplate>
<asp:Label ID="lblEditItem" runat="server" Text='<%# Bind("UnitPrice", "{0:#,##0.00}") %>' Enabled="false" ></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label Runat="server" Text='<%# Bind("UnitPrice", "{0:c}") %>' ID="lblUnitPrice"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Do not include the format string in the EditItemTemplate. Just bind the raw value.
Something like this:
<asp:TemplateField SortExpression="UnitPrice" HeaderText="Unit Price">
<EditItemTemplate>
<asp:TextBox ID="editUnitPrice" Runat="server" Text='<%# Bind("UnitPrice", "{0:#,##0.00}") %>' ></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label Runat="server" Text='<%# Bind("UnitPrice", "{0:c}") %>' ID="Label1"> </asp:Label>
</ItemTemplate>
</asp:TemplateField>
Related
This is dataGrid . How to add text(string) after <%# Bind("Value") %>
for example 123.432 and i want after any record to have "$" dolar sign
<asp:TemplateField HeaderText="Стойност">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Value") %>'>
</asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="LabelValue" runat="server" Text='<%# Bind("Value") %>'>
</asp:Label>
</ItemTemplate>
<ControlStyle Width="100px" />
</asp:TemplateField>
<% String.Format("{0}$",Eval("Value")); %>
You can take benefit of Standard Numeric Format String
<asp:Label ID="LabelValue" runat="server"
text='<%# Bind("Value").ToString("C", CultureInfo.CurrentCulture) %>'/>
which is a standard format available with C#.
But you want to show it in the last then you can simply add it in the last.
<ItemTemplate>
<asp:Label ID="LabelValue" runat="server" Text='<%# Bind("Value") %>'>
</asp:Label>
$
</ItemTemplate>
I suggest you to use MaskedEdit extender in ajax toolkit if you already using ajax toolkit in your project. MaskedEdit extender can be use in edit template and it will handle the mask on client side you don't want to worry about value and $ sign when you read the value back.
without using ajax toolkit you can use one label for $ sign and textbox for value field on edit template. In normal template also use two labels for both value and $ sign. then it will be easy to read the values.
I am reading a Date from a Table into a GridView, and it appears formatted in the GridView as:
15/01/2012 00:00:00
How can I read it in so that just the date part appears, minus the 00:00:00?
And when I use UPDATE, how do I store it back to the Table?
<asp:TemplateField HeaderText="Date">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("SDate") %>' >
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("SDate")%>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
If you're using a BoundField to display the date, it has a property called DataFormatString you can set that will format your field to the simple date format you'd like:
<asp:BoundField ... DataFormatString="{0:dd/MM/yyyy}" ... runat="server" />
If you'd doing something with a TemplateField, you can specify the date format in the binding expression:
<asp:TemplateField>
<ItemTemplate>
<asp:Label ...>
<%# Eval(Container.DataItem, "DateProperty", "{0:dd/MM/yyyy}") %>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
Because you're only changing how the data is displayed, the full underlying date is still available to you when you perform your SQL UPDATE, assuming you're retrieving your data via the DataItem property of each GridViewRow.
In an aspx page I am binding the labels like this:
<asp:TemplateField HeaderText="Date of Joining">
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Eval("date_of_joining") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Paid Priviledge Date">
<ItemTemplate>
<asp:Label ID="Label8" runat="server"
Text='<%# Eval("paid_priviledge_date") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
And in the code behind I'm binding the grid view like this :(minimum code is given)
GridView1.DataSource = dt2;
GridView1.DataBind();
But the gridview columns show the date like this :
4/12/2011 12:00:00 AM
4/4/2011 12:00:00 AM
Please suggest how to remove the time stamp part and to display only the date part.
I know how to do this by formatting using ToString and SubString. But I am not able to do this in gridview.
You can specify format strings for the eval statement:
Eval("date_of_joining", "{0:dd/MM/yyyy}")
Create a FormatDate method in your codebehind, and call that from your gridview.
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
http://www.csharp-examples.net/string-format-datetime/
This part will go in your code behind
private object FormatDate(DateTime input)
{
return String.Format("{0:MM/dd/yy}", input);
}
And this bit will go in your markup
<asp:TemplateField HeaderText="Date of Joining">
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# FormatDate(Eval("date_of_joining")) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Paid Priviledge Date">
<ItemTemplate>
<asp:Label ID="Label8" runat="server"
Text='<%# FormatDate(Eval("paid_priviledge_date")) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
This is what I would call a D.R.Y. approach to the problem. If you ever need to modify the formatting in any way. You can simply edit the code behind method and it will rain down sweet love to all of your markup.
Use "{0:d}" for short date format.
Try
Text='<%# Eval("paid_priviledge_date","{0:d}") %>'
and
Text='<%# Eval("date_of_joining", "{0:d}") %>'
You can use the DataFormatString in a bound field the same can be set as below:
<asp:Label ID="Label8" runat="server" Text='<%# Eval("paid_priviledge_date","{0:d}") %>'/>
Text='<%# (Convert.ToDateTime((Eval("date_of_joining")))).ToShortDateString() %>'
This is the simplest way I discovered.
In Dynamics ax Enterprise Portal I have created a templatefield in AxGridView. Seems everything ok, but when i try to enter some value to this textbox (manually or through lookup), it doesnt bind to ReqPo!ItemId field. Checked that with info(strfmt("%1", ReqPo.ItemId))); in validateWrite method on ReqPo dataset - it prints nothing; What i'm missing?
<asp:TemplateField ConvertEmptyStringToNull="False" HeaderText="<%$ AxLabel:#SYS12836 %>" Visible="true">
<EditItemTemplate>
<asp:TextBox runat="server" ID="TextBoxFilterItemId" CssClass="AxInputField"
Columns="<%$ AxDataSet:ReqTransPo.ReqTrans.ReqPo!ItemId.DisplayLength %>"
Enabled="<%$ AxDataSet:ReqTransPo.ReqTrans.ReqPo!ItemId.AllowEdit %>"
MaxLength="<%$ AxDataSet:ReqTransPo.ReqTrans.ReqPo!ItemId.StringSize %>"
Text='<%# Bind("[ReqPo!ItemId]") %>'>
</asp:TextBox>
<dynamics:AxLookup
ID="AxLookup3" runat="server" OnLookup="Item_lookup" TargetControlId="TextBoxFilterItemId"
CssClass="AxLookupButtonBF" HoverCssClass="AxLookupButtonHoverBF" ShowFilter="True">
</dynamics:AxLookup>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="ItemIdLabel" runat="server" Text='<%# Bind("[ReqPo!ItemId]") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
I assume you are using the ItemId field from the table ReqPo. If you locate that field in the Data Dictionary you will discover that the field does not allow editing.
As AxGridView only supports editing, which leads me to the conclusion that you are trying the break the data integrity rules of the application.
if this is the case, it is obviously not possible.
I've got a text box that just disappeared. When I add another templateField anywhere on the page, this one bizarelly disappears. Anyone know what might be up?
<asp:TemplateField HeaderText="Summary" SortExpression="summary">
<ItemTemplate>
<asp:Label ID="lblSummary" runat="server" Text='<%# Bind("summary") %>'></asp:Label>
</ItemTemplate>
<ItemTemplate>
<asp:TextBox ID="txtSummary" TextMode="MultiLine" Width="500" Height="100" runat="server" Text='<%# Bind("summary") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<AnotherEdit>
I hope you don't take this wrong, and I mean no offense, but it seems like you're missing some of the basic concepts and need a good place to learn them.
Please check out the following article so that you fully understand how to use TemplateFields as opposed to a normal BoundField or a Command field. I think once you "get" it, your disappearing item issues will clear up because you'll be able to see it on your own.
http://www.asp.net/learn/data-access/tutorial-12-cs.aspx
</AnotherEdit>
You can't have more than one ItemTemplate in a TemplateField. You can have an EditItemTemplate and an ItemTemplate, though...
Edit - Added
The ItemTemplate shows when you're in normal display mode.
The EditItemTemplate shows when you're in edit mode
InsertItemTemplate shows when you're in Insert mode.
For any column in a GridView (or field in a FormView or field in a DetailsView) there can only be one TemplateField.
Within that TemplateField, there can only be one ItemTemplate, one EditItemTemplate, and one InsertItemTemplate (and not all three are required but all three are recommended.)
If you want the TextBox to show up next to the label in the normal non-edit mode, you would put the text box within the existing ItemTemplate as follows:
<ItemTemplate>
<asp:Label ID="lblSummary" runat="server" Text='<%# Bind("summary") %>'>
</asp:Label>
<asp:TextBox ID="txtSummary" TextMode="MultiLine" Width="500" Height="100" runat="server" Text='<%# Bind("summary") %>'>
</asp:TextBox>
</ItemTemplate>
However, the norm is to have the label when in read mode, and a text box when in edit or update mode like shown below:
<asp:TemplateField HeaderText="Summary" SortExpression="summary">
<ItemTemplate>
<asp:Label ID="lblSummary" runat="server" Text='<%# Bind("summary") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtSummary" TextMode="MultiLine" Width="500" Height="100" runat="server" Text='<%# Bind("summary") %>'>
</asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="txtSummary" TextMode="MultiLine" Width="500" Height="100" runat="server" Text='<%# Bind("summary") %>'>
</asp:TextBox>
</InsertItemTemplate>
</asp:TemplateField>
You have multiple "ItemTemplate" declarations in there. There should be only one.