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");
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.
Please refer to the code below. The template field is a part of a gridview. I have a requirement where I want to pass the string from Boundfield "TriggerEvent" to a method "Alert()" that should do some operation on the string and display it back in the grid. I have encountered error in this which is explainable. How do I achieve this functionality?
<asp:TemplateField HeaderText="TriggerEvent" SortExpression="TriggerEvent" ItemStyle- Wrap="false">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Alert(Eval("TriggerEvent")) %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
As Eval is returning object and Alert is expecting string, you can cast is with as:
Text='<%# Alert(Eval("TriggerEvent") as string) %>'
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"/>
I have following code for gridview :
<asp:TemplateField HeaderText="N/A">
<ItemTemplate>
<asp:Label ID="lblENGNA" runat="server" Text='<%# Bind("N/A") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
When i run this its giving me error for above section that:
Parser Error Message: A call to Bind was not well formatted. Please
refer to documentation for the correct parameters to Bind.
Error Image:
Note: my query(which binds grid) contains field N/A
I added [N/A] in bind and it worked.
Its as follows:
<asp:Label ID="lblENGNA" runat="server" Text='<%# Bind("[N/A]") %>'>
I have a GridView which is editable and is bound to a database. So the datatype of each column in the GridView is Int in the database. Therefore my GridView accepts only integer.
But I want to give a notification to the user if the user enters any other character in the GridView cells other than numbers.
Basically I need to validate and give a error message.
Can someone help me how to go about it?
I have used Boundfields for my GridView.
Therefore suppose I enter any character other than a number it gives me an exception message saying : System.Data.SqlClient.SqlException: Conversion failed when converting the nvarchar value 'a' to data type int.
You best bet is to convert the BoundField into a TemplateField and add the validation control to the EditItemTemplate.
<asp:TemplateField HeaderText="Application" SortExpression="APPName">
<EditItemTemplate>
<asp:TextBox ID="txtApp" runat="server" Text='<%# Bind("APPName") %>'/>
<asp:RequiredFieldValidator runat='server' ID='requiredApp'
ErrorMessage='Application Name Cannot Be Empty' ControlToValidate='txtApp' />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="labelApp" runat="server" Text='<%# Bind("APPName") %>'/>
</ItemTemplate>
</asp:TemplateField>
SOURCE