Question

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) : "" %>'
Was it helpful?

Solution

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.

OTHER TIPS

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).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top