Converting the template field value.
The default is shown like 01-03-2015 00:00:00
I need in MMM/yyyy format
<asp:TemplateField HeaderText="MonthYear" SortExpression="MonthYear">
<ItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("MonthYear")%>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
Go with this:
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Convert.ToDateTime(Eval("MonthYear")).ToString("MMM yyyy")%>'></asp:TextBox>
Related
What I need: TARGET or ACTUAL on first line and rest on next line.
What I have till now
I have this gridview templatefield in asp.net
<asp:TemplateField HeaderStyle-CssClass="headerStyle" HeaderText="TARGET Approval Date" FooterStyle-CssClass="alternateStyler" ItemStyle-CssClass="alternateStyler" ItemStyle-Width="8%" ItemStyle-VerticalAlign="Top" FooterStyle-VerticalAlign="Top">
<ItemTemplate>
<asp:TextBox ID="dtTgtApprDate" CssClass="Datetext" Width="98%" Height="23px" runat="server" Text='<%# Eval("Phase2")%>'></asp:TextBox>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="dtTgtApprDate2" CssClass="Datetext" Width="98%" Height="23px" runat="server">
</asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-CssClass="headerStyle" HeaderText="ACTUAL Approval Date" FooterStyle-CssClass="alternateStyler" ItemStyle-CssClass="alternateStyler" ItemStyle-Width="8%" ItemStyle-VerticalAlign="Top" FooterStyle-VerticalAlign="Top">
<ItemTemplate>
<asp:TextBox ID="dtActApprDate" CssClass="Datetext" Width="98%" Height="23px" runat="server" Text='<%# Eval("Phase3")%>'></asp:TextBox>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="dtActApprDate2" CssClass="Datetext" Width="98%" Height="23px" runat="server">
</asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
I have put this css for HeaderStyle-CssClass="headerStyle"
.headerStyle {
text-justify:distribute;
}
Since the header is HTML also, you can just put a <br> in it.
<asp:TemplateField HeaderText="TARGET<br>ApprovalDate">
I am having an issue with Visual Studio. I have a page with a GridView. The code behind recognizes the GridView fine, but none of the elements within it. I tried putting a label before the GridView and it was able to recognize it. This would imply there is some error in my aspx code file, but I have looked it over and all looks well to me.
I have tried deleting .designer.cs file and 'Converting to Web Application'. I have tried copying the code, deleting the files, and recreating them. I have also tried numerous cleans and rebuilds and restarting Visual Studio. Still, no success. I would greatly appreciate any guidance.
<%# Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="True" CodeBehind="users.aspx.cs" Inherits="Ticket_System.admin.users" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<asp:GridView ID="userGrid" runat="server" OnRowEditing="userGrid_RowEditing" DataKeyNames="END_USER_ID" AutoGenerateEditButton="True" AutoGenerateColumns="false" OnRowUpdating="userGrid_RowUpdating" OnRowCancelingEdit="userGrid_RowCancelingEdit">
<Columns>
<asp:TemplateField HeaderText="User #">
<ItemTemplate>
<asp:Label ID="idLabel" runat="server" Text='<%# Bind("END_USER_ID") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="idText" runat="server" Enabled="false" Text='<%# Bind("END_USER_ID") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Access">
<ItemTemplate>
<asp:Label ID="accessLabel" runat="server" Text='<%# Bind("ACCESS_ROLE.DESCRIPTION") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="User">
<ItemTemplate>
<asp:Label ID="userLabel" runat="server" Text='<%# Bind("USER_NAME") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="userText" runat="server" Text='<%# Bind("USER_NAME") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Password">
<ItemTemplate>
<asp:Label ID="passLabel" runat="server" Text='<%# Bind("PASSWORD") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="passText" runat="server" Text='<%# Bind("PASSWORD") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="First">
<ItemTemplate>
<asp:Label ID="firstLabel" runat="server" Text='<%# Bind("FIRST_NAME") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="firstText" runat="server" Text='<%# Bind("FIRST_NAME") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last">
<ItemTemplate>
<asp:Label ID="lastLabel" runat="server" Text='<%# Bind("LAST_NAME") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="lastText" runat="server" Text='<%# Bind("LAST_NAME") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Active">
<ItemTemplate>
<asp:Label ID="activeLabel" runat="server" Text='<%# Bind("ACTIVE_FLAG") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="activeText" runat="server" Text='<%# Bind("ACTIVE_FLAG") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</asp:Content>
EDIT: Just wanted to clarify something. What I mean is that the controls are not even recognized at all in the code behind. For example firstLabel.Text gives me an "does not exist in current context" error.
Try using the FindControl() method.
foreach(GridViewRow row in userGrid.Rows) {
if(row.RowType == DataControlRowType.DataRow) {
string idText = (string)row.FindControl("idText");
}
}
Been trying to fix this for hours and I know that the problem is with the '#' but I could not find any solution to this problem. My field name in the database is 'HRTRN#'.
<asp:TemplateField HeaderText="Transaction#">
<EditItemTemplate>
<asp:TextBox ID="TextBox13" runat="server" Text='<%# Bind("HRTRN#") %>' Width="50px" Height="17px" MaxLength="14"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("HRTRN#") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Does anyone know the solution for this to make the field name containing # symbol accessible?
You should change your code from :
Bind("HRTRN#")
To
Bind("[HRTRN#]")
Since it contains special charcters. ( you would do that also for columns with spaces).
How should DataFormatString of BoundField in Gridview look like that values won't have leading zeros?
So far I got this:
<asp:BoundField DataField="NUMBER" HeaderText="Id. number" DataFormatString="{0:d}">
Expected result:
000001 -> 1
002101 ->2101
I tried to figure that problem out with official documentation and this page. So far unsuccessful.
When you want to format something properly in a boundfield, I always suggest converting it to a templatefield. It's much easier to work with than boundfields. Here is an example of how the templatefield will look like once it's converted.
<asp:TemplateField ShowHeader="False" Visible="False">
<EditItemTemplate>
<asp:TextBox ID="tbEditNumber" runat="server" Text='<%# Bind("Number","{0:n}") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:TextBox ID="tbNumber" runat="server" Text='<%# Bind("Number","{0:n}") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
within that template field I placed..
Text='<%# Bind("yourfield","{0:n}") %>'
This should format it into a number and should drop the leading zeros.
EDIT: You might be able to try
Text='<%# String.Format("{0:n}", Eval("Number") ) %>'
Another approach is to use String.Trim function. The following code is how I accomplished what you are trying to do:
<asp:TemplateField>
<ItemTemplate>
<asp:Label runat="server" Text="<%#Eval("NUMBER").ToString().TrimStart('0')%>" ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
The tricky part was using the correct combination of quotes and single quotes. You can use
"
in place of the quote character around your datafield column that you are displaying.
Simplifiying A Kimmels answer, no need to place in a server label, no concern of single or double quotes, no concern of underlying datatype, as it casts the object to string, then performs a string.TrimStart, no need for codebehind
<asp:TemplateField HeaderText="Work Order">
<ItemTemplate>
<%# Eval("WorkOrder").ToString().TrimStart("0".ToCharArray()) %>
</ItemTemplate>
</asp:TemplateField>
0123456 = 123456
0012345 = 12345
if a server label is required, just start and end the text tag with single quotes
<asp:Label ID="lblWorkOrder" runat="server" Text='<%# Eval("WorkOrder").ToString().TrimStart("0".ToCharArray()) %>' />
Try this. Much cleaner this way.
Markup
<asp:TemplateField HeaderText="Id. number">
<EditItemTemplate>
<asp:TextBox ID="tbEditNumber" runat="server" Text='<%# ConvertToDigit(Eval("Number")) %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:TextBox ID="tbNumber" runat="server" Text='<%# ConvertToDigit(Eval("Number")) %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
Code-behind
protected int ConvertToDigit(object oNum)
{
var i = 0;
if (oNum != null) int.TryParse(oNum.ToString(), out i);
return i;
}
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>