How to form a method to change HTML Elemtn by reference?
Hello, thank you for taking the time to read this. I am totally
stumped on this one.
I have a ton of TD attributes that have to be changed based on the
data in the DB. All TD elements are ID and runat=server.
Changing the attributes of a SINGLE TD is simple:
<td id=thisTD runat=server>
thisTD.Attributes.Clear();
thisTD.Attributes.Add("Class",reader["thisTD_Class"].toString());
Now, say we have 100 TD elements with IDs TD_1 to TD_100
I have not been able to find a way to (essentially) say:
for(int i=0;i<100;i++){
"TD_"+i.Attributes.Clear();
"TD_"+i.Attributes.Add("Class",reader["TDClass"+i].toString());
}
or
public void changeStyle( ________ myTDReference, string classType){
myTDReference.Attrbutes.Add("Class",classType);
}
in J-Script, I could do it by Eval() or about eight ways through the
DOM. It seems crazy to me that if I can access the Attributes of a
specific element directly by ID (thisTD.Attributes), I couldn't also
access it by reference.
But I have been able to find NOTHING to indicate how this may be done.
Obviously, my problem is not limited to a single attribute, or else I
would have just typed them all out by now. Trouble is, there is a lot
of logic that must be applied to the data before setting the
attribute. Without being able to iterate or loop by reference, I will
be typing for ages just to change some freaking classes.
Thanks in advance for any help or information you can provide.
Date:Sat, 21 Jul 2007 06:59:51 -0700
Author:
|
Re: How to form a method to change HTML Elemtn by reference?
Shannon-
In .NET, you have the option of creating a reference to an object (via it's
ID) by instanciating a HtmlTableCell object and looping.
Here's one example. Your HTML looks similar to this. Your HtmlTableRow
has an id (in the DOM) of myRow. Yoru cells are named differently. I hand-entered
it, but they could be auto-generated too... this method focus on knowing
what's in your ROW, not so much focusing on the cells.
<table>
<tr id="myRow" runat="server">
<td id="myCell1" runat="server"></td>
<td id="myCell2" runat="server"></td>
<td id="myCell3" runat="server"></td>
<td id="myCell4" runat="server"></td>
<td id="myCell5" runat="server"></td>
<td id="myCell6" runat="server"></td>
</tr>
</table>
From there, in your code behind, you can call:
foreach (HtmlTableCell cell in myRow.Cells)
{
cell.Attributes.Clear();
cell.InnerText = "Hello!";
// Your code here. :)
cell.Attributes.Add("Class", "myClass");
}
The resulting HTML is generated:
<table>
<tr id="myRow">
<td id="myCell1" Class="myClass">Hello!</td>
<td id="myCell2" Class="myClass">Hello!</td>
<td id="myCell3" Class="myClass">Hello!</td>
<td id="myCell4" Class="myClass">Hello!</td>
<td id="myCell5" Class="myClass">Hello!</td>
<td id="myCell6" Class="myClass">Hello!</td>
</tr>
</table>
Now, if you're interested in your i < 100 example; give this a shot.
for (int i = 1; i < 100; i++)
{
try
{
HtmlTableCell cell = this.Page.FindControl("myCell" + i)
as HtmlTableCell;
cell.Attributes.Clear();
cell.InnerText = "Hello!";
// Your code here. :)
cell.Attributes.Add("Class", "myClass");
}
catch
{
}
}
Does the exact same thing, but you have to know your upper and lower boundaries
and you'll have a failure if "i" is ever out of range. Depends on the situation.
:) You can customize these and refactor the method out (as you mentioned)
to automagically put in the classType or whatever attributes you are assigning,
like below:
protected void ChangeAttribute(HtmlTableRow rowToEdit, string attribute,
string value, bool clearAttributes)
{
foreach (HtmlTableCell cell in rowToEdit.Cells)
{
if (clearAttributes)
{
cell.Attributes.Clear();
}
cell.Attributes.Add(attribute, value);
}
}
}
HTH!
-dl
--
David R. Longnecker
http://blog.tiredstudent.com
> Hello, thank you for taking the time to read this. I am totally
> stumped on this one.
>
> I have a ton of TD attributes that have to be changed based on the
> data in the DB. All TD elements are ID and runat=server.
>
> Changing the attributes of a SINGLE TD is simple:
>
> <td id=thisTD runat=server>
>
> thisTD.Attributes.Clear();
> thisTD.Attributes.Add("Class",reader["thisTD_Class"].toString());
> Now, say we have 100 TD elements with IDs TD_1 to TD_100
>
> I have not been able to find a way to (essentially) say:
>
> for(int i=0;i<100;i++){
> "TD_"+i.Attributes.Clear();
> "TD_"+i.Attributes.Add("Class",reader["TDClass"+i].toString());
> }
> or
>
> public void changeStyle( ________ myTDReference, string classType){
> myTDReference.Attrbutes.Add("Class",classType);
> }
> in J-Script, I could do it by Eval() or about eight ways through the
> DOM. It seems crazy to me that if I can access the Attributes of a
> specific element directly by ID (thisTD.Attributes), I couldn't also
> access it by reference.
>
> But I have been able to find NOTHING to indicate how this may be done.
> Obviously, my problem is not limited to a single attribute, or else I
> would have just typed them all out by now. Trouble is, there is a lot
> of logic that must be applied to the data before setting the
> attribute. Without being able to iterate or loop by reference, I will
> be typing for ages just to change some freaking classes.
>
> Thanks in advance for any help or information you can provide.
>
Date:Mon, 23 Jul 2007 17:19:34 +0000 (UTC)
Author:
|