I have a ListView and in it there is a field with some date coming from the database. Now when I click on the Edit button, in the next Edit Page I want to show the exact date in the particular date field.
See the following screenshots:
Main View:
After clicking:
I have written the following code to bind the date:
<asp:TextBox runat="server" Text='<%#Bind("FirstVisitDate", "{0:MM/dd/yyyy}") %>' ID="FirstVisitDate" CssClass="form-control" TextMode="date" />
But this is not working.
Use this one instead of yours:
<asp:TextBox runat="server"
Text='<%# String.Format("{0:MM/dd/yyyy}", Eval("FirstVisitDate")) %>'
ID="FirstVisitDate" CssClass="form-control" TextMode="date" />
If you are still getting error you can convert it to DateTime see if it works:
Text='<%# String.Format("{0:MM/dd/yyyy}", Convert.ToDateTime(Eval("FirstVisitDate")) %>'
also we don't have TextMode="date" ?? remove that one as well.
You could also try this way, which is way better than Eval in terms of performance:
Text='<%# String.Format("{0:MM/dd/yyyy}", ((System.Data.DataRowView)Container.DataItem)["FirstVisitDate"] %>'
if you want to show the format of the date, you should use placeholder instead like this:
also you could use type as a date like below:
<asp:TextBox runat="server" placeholder="mm/dd/yyyy" type="date" ... />
When using TextMode="date" you should always use the format yyyy-MM-dd (W3C standard)
<asp:TextBox Text='<%# Bind("data", "{0:yyyy-MM-dd}")%>' runat="server" ID="dataTextBox" TextMode="Date" CssClass="form-control"/>
Related
I have a problem with the GridView column formatting.
I have a PhoneNumber that is currently coming from database as (###)###-#####
but I want the format to be ############ without spacing and brackets. I have tried every thing like DataFormatString="{0:###-####}" or than converting it into TemplateField and giving it format but not working.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" >
<Columns>
<asp:TemplateField HeaderText="Phone Number">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%#
Bind("PhoneNumber") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server"
Text='<%# Bind("PhoneNumber", "{0:d}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
This is before converting the column to TemplateField:
<asp:BoundField DataField="PhoneNumber" HeaderText="Phone Number Home"
SortExpression="Phone" DataFormatString="{0:######-####}" HtmlEncode="false" />
But nothing is working. I still get the format of phone number like this (123) 123-4567.
If you are dynamically binding the rows in code behind, you should be formatting the columns in the DataBind event of the gridview.
The semantical column formatting works only when you use bind data semantically, for example declare
Try to replace Text property in TemplateField:
Text='<%# Bind("PhoneNumber", "{0:d}") %>'
with:
Text='<%# String.Format("{0:##########}",
Convert.ToInt64(DataBinder.Eval(Container.DataItem, "PhoneNumber")))%>'
Use "#" Custom Specifier.
Formatting is solely additive, not subtractive. To accomplish this, you would need to expose a version of the values which has non-numerics stripped out, so that your client-side formatting can act upon it.
I have a Gridview, with a DateTime column defined like this:
<asp:TemplateField>
<EditItemTemplate>
<asp:TextBox ID="txtDate" Text='<%# Item.Date.ToShortDateString() %>' runat="server" TextMode="Date"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblDate" runat="server" Text='<%# Item.Date.ToShortDateString() %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
The date gets displayed in the grid, and it gets saved correctly when I edit a row from the grid. The only problem is that "txtDate" doesn't display the current value when entering in "edit mode" of a row. It displays "mm/dd/yyyy" instead of the actual value coming from the bind. And I don't know why. Nonetheless, when I remove the property TextMode="Date", the actual value displays correctly.
Any ideas? Thank you!
This occurs in Google Chrome when the browser can't parse the given date. You should be getting a warning about that in the browser's console as well. Google Chrome expects the format to be yyyy-MM-dd, so e.g.:
Item.Date.ToString("yyyy-MM-dd");
I am trying to add date From and date To to my products these values are store in my database as date. These are stored in this format 2013-01-15. The format is not a problem but when I display them on my application the time appears (1/15/2013 12:00:00 AM) how can I remove the time please. Below you can find the method Im databound the data.
<asp:Label ID="Label4" runat="server" Text='<% # Eval("soDateTo") %>' Font-Bold="False" Font-Size="Small"></asp:Label>
Try String Formatting within the Eval statement:
See ASP Forums
There are several ways to format the date.
<asp:label id="DateAddedLabel" runat="server" text='<%#
Eval("DateAdded", "{0:d}") %>'></asp:label>
Try this;
<asp:Label ID="Label4" runat="server" Text='<% # Eval("soDateTo", "{0:dd/MM/yyyy}") %>' Font-Bold="False" Font-Size="Small"></asp:Label>
Very similar to Daniel's solution, but it handles null:
<asp:label id="DateAddedLabel" runat="server" text=
'<%# (String.IsNullOrEmpty(Eval("DateAdded").ToString()))
? "No Date Available" : Eval("DateAdded", "{0:d}") %>'>
</asp:label>
This has been answered just fine, but I used to use a lot more Labels than were necessary and thought I'd offer a way without.
You can ignore the Label all together and put the Eval(...) method by itself.
For example if you are using this inside of a TemplateField
<asp:TemplateField HeaderText="Date To">
<ItemTemplate>
<%# Eval("soDateTo", "{0:MM/dd/yyyy}") %>
</ItemTemplate>
</asp:TemplateField>
You can use this to improve your CSS control a tad, such as
<div id="client_since">
<%# Eval("soDateTo", "{0:MM/dd/yyyy}") %>
</div>
Use DateTime.ToShortDateString Method to get rid of the time part of the date:
http://msdn.microsoft.com/en-us/library/system.datetime.toshortdatestring.aspx
Try this:
> <asp:Label ID="Label4" runat="server" Text='<% # Eval("soDateTo", "{0:d}") %>'
> Font-Bold="False" Font-Size="Small"></asp:Label>
Try this;
<asp:Label ID="lbldate" runat="server" Text='<%# (Convert.ToDateTime(Eval("soDateTo"))).ToShortDateString() %>'></asp:Label>
Can someone please tell me where i'm wrong ?
<asp:TextBox ID="txtComentario_<%# Eval('Id').ToString() %>"
TextMode="MultiLine" runat="server"></asp:TextBox>
Error
Parser Error Message: Make the server is not formed correctly.
try the following
<asp:TextBox ID='<%# Eval("Id","txtComentario_{0}") %>' TextMode="MultiLine" Text="" runat="server"></asp:TextBox>
The ID property of a control can only be set using the ID attribute in the tag
you should use Bind instead of Eval ,The Bind method is typically used with input controls such as the TextBox .
Try like :-
<asp:TextBox ID='txtComentario_<%# Bind("Id") %>' TextMode="MultiLine" runat="server"></asp:TextBox>
If you want to add extra information to your textbox, you can add attribute to it.
TextBox1.Attributes.Add("customAttribute", "value");
Morning all I have frequently used the old
<asp:Label ID="lblWas" runat="server" Text='<%# XPath("FACEVALUE") %>'></asp:Label>
This type of thing. when I first came across it I loved it, i'm using it again today but not quite so simply.
I have a number of extra things I would like to achieve.
Apply formatting to the value. Like Text='<%# string.Format(XPath("FACEVALUE"), "{0:c}") %>'>
<asp:LinkButton ID="lnkBook" runat="server" PostBackUrl='/THEATRE/' + XPath("FACEVALUE")>Book</asp:LinkButton>
For option number 2 the URL is not as I would expect, and for number 1 I cannot get the syntax correct if it's even possible.
I have not been able to find something suitable in google. Hopefully what I am trying to achieve is obvious from the example :)
You can use the TemplateControl.XPath(string xPathExpression, string format) override:
<asp:Label Text='<%# XPath("FACEVALUE", "{0:c}") %>' />
<asp:LinkButton Text="..." PostBackUrl='<%# XPath("FACEVALUE", "/THEATRE/{0}") %>' />
As you can see, you do not need to use string.Format because you can pass the format directly into the XPath method!
I believe for #1, you have messed up the syntax, you want to use
Text='<%# string.Format("{0:c}", XPath("FACEVALUE")) %>'
or Text='<%# XPath("FACEVALUE", "{0:c}") %>'
For #2, you need to use data binding expressions
<asp:LinkButton ID="lnkBook" runat="server" PostBackUrl='<%# "/THEATRE/" + XPath("FACEVALUE")%>'>Book</asp:LinkButton>
For the first thing it must be Text='<%# string.Format( "{0:c}",XPath("FACEVALUE")) %>'>
and for the second one it should be
<asp:LinkButton ID="lnkBook" runat="server" PostBackUrl='/THEATRE/ + <%# XPath("FACEVALUE") %>'>Book</asp:LinkButton>