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>
Related
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 don't get what I am doing wrong with this code. I think it might have to do with the (" or ')s
<asp:TextBox ID="txtPassportNumber" runat="server" MaxLength="19"
Text="<%# String.Format('{0}{1}','######',((TSAPassenger) Container.DataItem).Passport.DocumentNumber.Text.Remove(0,6)) %>"
Enabled="<%# IsOutsideTenDayCutoff %>"></asp:TextBox>
I get too many character error
Switched to:
<asp:TextBox ID="txtPassportNumber" runat="server"
Text="<%# String.Format("{0}{1}","######",((TSAPassenger) Container.DataItem).Passport.DocumentNumber.Text.Remove(0,6)) %>"
Enabled="<%# IsOutsideTenDayCutoff %>"></asp:TextBox>
And get this error:
Parser Error Message: The server tag is not well formed.
Final Code that worked thanks to good help:
Text='<%# (((TSAPassenger) Container.DataItem).Passport.DocumentNumber != null && ((TSAPassenger) Container.DataItem).Passport.DocumentNumber != "") ? "******" + ((TSAPassenger) Container.DataItem).Passport.DocumentNumber.ToString().Remove(0,6) : "" %>'
You need to make sure you use single quote for the Text property eg Text='yourstuff'.
Then use double quotes inside your bind statement. The code in the bind must be vanilla c#, if it won't compile in a .cs file it won't compile inline either and single quotes mean a char in c#, not a string.
This works:
<asp:TextBox ID="txtPassportNumber" runat="server"
Text='<%# string.Format("{0}{1}", "######", ((TSAPassenger) Container.DataItem).Passport.DocumentNumber.Text.Remove(0,6)) %>'
Enabled="<%# IsOutsideTenDayCutoff %>"></asp:TextBox>
Notice the single and double quotes. You should be able to copy and paste it as is.
you should use double quotation mark instead single
<asp:TextBox ID="txtPassportNumber" runat="server" MaxLength="19" Text="<%# String.Format("{0}{1}","######",((TSAPassenger) Container.DataItem).Passport.DocumentNumber.Text.Remove(0,6)) %>" Enabled="<%# IsOutsideTenDayCutoff %>"></asp:TextBox>
You can't define a string using '. What you are doing now is trying to create a multiple character char which is not doable.
Try changing it to this:
Text='<%# String.Format("{0}{1}","######",((TSAPassenger) Container.DataItem).Passport.DocumentNumber.Text.Remove(0,6)) %>'
You should invert the usage of ' and " in the Text attribute. Instead of:
Text="<%# String.Format('{0}{1}','######',((TSAPassenger) Container.DataItem).Passport.DocumentNumber.Text.Remove(0,6)) %>"
use:
Text='<%# String.Format("{0}{1}","######",((TSAPassenger) Container.DataItem).Passport.DocumentNumber.Text.Remove(0,6)) %>'
You have to provide a valid C# syntax between the <%# ... %> tags. '"{0}{1}"' and '######' are both invalid in C# syntax, as '' can only enclose chars ('a','0' and so on).
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>
I am not able to sent the value of the MachineID to another page using the hyperlink in gridview.
<!-- <asp:TemplateField HeaderText="FailedFiles"
SortExpression="NumFailedFilesOverSLA">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server"
Text='<%#Bind("NumFailedFilesOverSLA") %>'>
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
I have tried putting
DataNavigateUrlFields="MachineID"
DataNavigateUrlFormatString="GetFilesFailed.aspx?id={0}"
but don't know why this is not working??
Please suggest...
thanks
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl='<%# Eval("Inventory_ID", "/default.aspx?ID={0}") %>'
Text="Details"></asp:HyperLink>
</ItemTemplate>
This should solve your problem. This is exactly how I used it.
If this doesn't work, then check that you are actually getting a value back from the DB for MachineID:
<asp:HyperLink ID="HyperLink1" Text='<%# Bind("NumFailedFilesOverSLA") %>'
runat="server" DataNavigateUrlFields="MachineID"
DataNavigateUrlFormatString="GetFilesFailed.aspx?id={0}">
</asp:HyperLink>
First, try to put the default gridview in the page and attach it to the data source, so you can test if there is data to display.
If you are assigning the data source from code behind don't forget to call the DataBind() method after that.