views:

302

answers:

4

Hi, I'm using a htmlhelper where i give table data id's based on day and month values which are retrieved. The problem is the id is not recognized in the format it is. / seems to not be picked up yet when i replace '/' with '-' it works.

daysRow.AppendFormat("<td id='{0}/{1}'>{0}</td>", day, d1.Month.ToString());

can anyone tell me how to format this please. '/' forward slash in c#.

A: 

Use // iirc

alternativley i think putting @ in front of your string will make it be tret as a literal.

eg

string s = @"\w\e\r\ty";

or

string s = "d\\d";

what you need to use is the string literal

'& # 4 7 ;' without the spaces

instead of the forward slash

John Nicholas
This is unnecessary, a forwardslash in a .NET string is valid.
James
@MrTortoise: `Still, it answers the base of his question` - No, it doesn't. You are thinking of `"\\"` and `@"\"`, which will both produce `\ `. `"//"` will just give you `//`
BlueRaja - Danny Pflughoeft
wow if you are going to ressurect comments after they are deleted at least get the quote right. Especially when you post several minutes after I corrected what i wrote.
John Nicholas
+13  A: 

The problem is not with C#, but rather, with your using a '/' character in HTML. From the section of the HTML 4.0 spec on the id attribute:

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

The '/' violates that rule, which is why you are seeing issues when using that, but not the '-' character.

casperOne
+1 This is what I gathered.
James
not an answer though ... you need '' without the spaces
John Nicholas
will that give a forward slash /
Calibre2010
@MrTortoise: I would disagree. Since the '/' is invalid in the id, he can't use it at all. The need to HTML encode the value is pointless since it would invalidate the HTML.
casperOne
@Calibre2010: The HTML-encoding for the '/' character ('') will give you the forward slash, but since it is invalid in ids, it's pointless to use anyways.
casperOne
could I at all do this with class?
Calibre2010
@Calibre2010: You can use a class, as it uses a list of CDATA types, which allow slashes. However, this violates the spirit of classes, as you are using unique values that identify the row in the class. Classes are like types, the id are like instances, you should respect the naming of each and use them as they were meant to be.
casperOne
A: 

I think you are using an invalid character, certainly according to this SO question it appears that you cant use forward slashes.

Mauro
+1  A: 

You cannot use / for ids in html. See here.

BlueRaja - Danny Pflughoeft