Specified cast is not valid

  • Thread starter Thread starter Andy Sutorius via DotNetMonster.com
  • Start date Start date
A

Andy Sutorius via DotNetMonster.com

Hi,

I am attempting to set the CssClass programatically of an asp.net table
row. My html is below. My code behind with the error on line 313 is also
below. I attempted to cast the data from the data reader into a table row
and then set the table row's CssClass but got the error. How can I set the
CssClass of the <tr> programmitcally?

Thanks!

Andy

<tr id="hp_loc_016_da01" runat="server">
<td>Patient Signature Source A</td>
<td><asp:textbox id="txtPatientSignatureSourceA"
Runat="server"></asp:textbox></td>
</tr>


Line 311: while (dtrElecDemoData.Read())
Line 312: {
Line 313: TableRow troHiliteRowID = (TableRow) dtrElecDemoData["hilite"];
Line 314: troHiliteRowID.CssClass = "highlightrowyellow";
Line 315: }


Source File: c:\inetpub\wwwroot\ecui_1\wpclaimhcfaelecdemodata.aspx.cs
Line: 313
 
A DataTable has a DataRows Collection of TableRows. A DataReader, having
only one record in it at a time, does not.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
What You Seek Is What You Get.
 
I should clarify...it is not an asp.net table it's an html table with one
of the rows marked with and id and a runat attribute.
 
You're still trying to cast a DataReader as whatever it is that you say
you're using.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
What You Seek Is What You Get.
 
Well your comment intrigues me. Using intellisense I was able to see that
the HTMLTableRow has a bgcolor property but not cssclass property. I have
hard-coded the rows bgcolor. How can I do this dynamically?

while (dtrHighlightError.Read())
{
// TableRow troHiliteRowID = (TableRow) dtrHighlightError["hilite"];
// troHiliteRowID.CssClass = "highlightrowyellow";

hp_loc_016_da01.BgColor = "#ffff99";

}
 
You cannot access TR from code behind. id="hp_loc_016_da01" will be visible
only for the client script. ASP.NET does not create an object for it. You
can use something like this:

ASP.NET:
<tr class="<%= GetTRClass() %>">
....
</tr>

Code behind:
protected string GetTRClass()
{
return "highlightrowyellow";
}

As an alternative you can use DataGrid or another similar Web control. In
that case you will be able to access TR as an object, but the code will
require more resources.
 
Sorry. I forgot that ASP.NET can convert table tags to objects if you add
runat=server. However you need to add a protected field for your TR to the
code behind class.

ASP.NET:
<tr id="myTr" runat="server">
....
</tr>


Code behind:
protected HtmlTableRow myTr;

private void Page_Load(object sender, System.EventArgs e)
{
myTr.BgColor = "#ffff99";
}
 
Mike,

Thanks for your help. I have the protected in place. 1 more step! How to
get the .bgcolor to be associated with dtrHighlightError["hilite"]
 
Back
Top