<TR> custom property

G

Guest

I'm trying to add a custom property to each row of a table. So I created an HtmlTable called tblClass, I created a new class myRow inherited from HtmlTableRow, to which I added a new member myRow

public class myRow : System.Web.UI.HtmlControls.HtmlTableRow

private string _myCustomProp
public myRow() : base()
_myCustomProp=""

public string myCustomProp
get {return _myCustomProp;
set {_myCustomProp = value;


Then in my page class I have
private void Page_Load(object sender, System.EventArgs e

myRow myTR = new myRow()
myTR.BgColor = "#b0c4ae"
myTR.myCustomProp = "A123456"
...
tblClass.Rows.Add(myRow)
....

The problem is that the <TR>'s of the resulting table don't have my custom property myCustomProp. They do have the BgColor though

What am I doing wrong in trying to add this custom property to the <TR>
Thank you.
 
M

Martin Dechev

Hi, Gigi,

You can do the following for example:

override protected void Render(HtmlTextWriter writer)
{
Attributes["myCustomProp"] = _myCustomProp;
base.Render(writer);
}

Greetings
Martin
Gigi said:
I'm trying to add a custom property to each row of a table. So I created
an HtmlTable called tblClass, I created a new class myRow inherited from
HtmlTableRow, to which I added a new member myRow:
public class myRow : System.Web.UI.HtmlControls.HtmlTableRow
{
private string _myCustomProp;
public myRow() : base(){
_myCustomProp="";
}
public string myCustomProp{
get {return _myCustomProp;}
set {_myCustomProp = value;}
}
}
Then in my page class I have:
private void Page_Load(object sender, System.EventArgs e)
{
myRow myTR = new myRow();
myTR.BgColor = "#b0c4ae";
myTR.myCustomProp = "A123456";
....
tblClass.Rows.Add(myRow);
.....
}
The problem is that the <TR>'s of the resulting table don't have my custom
property myCustomProp. They do have the BgColor though.
 
G

Guest

Thank you for your reply first of all
It works if I replace myTR.myCustomProp = "A123456"; with myTR.Attributes["myCustomProp"]="A12345"; , where myTR is a standard HtmlTableRow object. I don't even have to modify the Render method

But I don't really know (and I can't find) how can I do it the way I wanted to do it, using a custom class derived from HtmlTableRow. In your example,to which object does Attributes["myCustomProp"] belong to? I guess to myTR, but how can I reference myTR in Render
How do I make this code really work

Thanks again.
 
M

Martin Dechev

Hi, Gigi,

The method Render is on the instance of the class, so the Attributes
property is on the instance too:

Attributes["a"]="a";

is the same as

this.Attributes["a"]="a";

Anyway, setting a key-value pair in the Attributes property is the only way
to avoid rewriting all of the rendering.

If you want you can rewrite the rendering. Basically you should do the
following:
- write "<tr "
- write the properties - Align, BgColor, BorderColor, etc.
- foreach(string key in Attributes.Keys)
writer.Write(key + "=\"" + Attributes[key] + "\" ")
- write your custom properties
- close the begin tag ">"
- call base.RenderChildren(writer)
- write the closing tag "</tr>".

There is a big chance to make errors though.

Hope this helps
Martin
Gigi said:
Thank you for your reply first of all.
It works if I replace myTR.myCustomProp = "A123456"; with
myTR.Attributes["myCustomProp"]="A12345"; , where myTR is a standard
HtmlTableRow object. I don't even have to modify the Render method.
But I don't really know (and I can't find) how can I do it the way I
wanted to do it, using a custom class derived from HtmlTableRow. In your
example,to which object does Attributes["myCustomProp"] belong to? I guess
to myTR, but how can I reference myTR in Render?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top