Set Background Color Using Column Value?

T

TF

Very newbie question and I hope this is the right group. I have a
table on a web page which contains info about paint colors. One column
contains the hex value for the color's RGB value. I want to display
info from the table in a grid or table and use the column value from
the RGBHex column to set each row's back ground color. I can just
about do this in my sleep in old style ASP using <%%> tags but I would
like to implement it in .NET 2. My website has this implemented in
old ASP here: www.paintmatcher.com/tool/tool.asp Any help is greatly
appreciated.
 
G

Guest

the RGBHex column to set each row's back ground color. I can just
about do this in my sleep in old style ASP using <%%> tags

it could be the same as in ASP 3:

bgcolor="#<%=hex_value%>"

where "hex_value" is a variable
 
T

TF

Here's a snippet of the code created by MS Vis Web Developer 05
Express. The data from the table displays just fine. I want to set
the back color of each row based on the value of the "RGBHex" field.

<asp:GridView ID="GridView1" runat="server" AllowSorting="True"
AutoGenerateColumns="False"
DataKeyNames="ID" DataSourceID="AccessDataSource2"
EmptyDataText="There are no data records to
display.">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID"
ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="Brand" HeaderText="Brand"
SortExpression="Brand" />
<asp:BoundField DataField="Colorname"
HeaderText="Colorname" SortExpression="Colorname" />
<asp:BoundField DataField="RGBValue" HeaderText="RGBValue"
SortExpression="RGBValue" />
<asp:BoundField DataField="RGBHex" HeaderText="RGBHex"
SortExpression="RGBHex" />
</Columns>
</asp:GridView>

Could I use ASP 3 <%%> tags inside the BoundField tag? How would I
call the field value using ASP.NET to assign to the variable? Or
maybe there's a better solution than using a GridView. Any other
ideas?

Thanks.
 
G

Guest

Here's a snippet of the code created by MS Vis Web Developer 05
Express. The data from the table displays just fine. I want to set
the back color of each row based on the value of the "RGBHex" field.

<asp:GridView ID="GridView1" runat="server" AllowSorting="True"
AutoGenerateColumns="False"
DataKeyNames="ID" DataSourceID="AccessDataSource2"
EmptyDataText="There are no data records to
display.">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID"
ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="Brand" HeaderText="Brand"
SortExpression="Brand" />
<asp:BoundField DataField="Colorname"
HeaderText="Colorname" SortExpression="Colorname" />
<asp:BoundField DataField="RGBValue" HeaderText="RGBValue"
SortExpression="RGBValue" />
<asp:BoundField DataField="RGBHex" HeaderText="RGBHex"
SortExpression="RGBHex" />
</Columns>
</asp:GridView>

Could I use ASP 3 <%%> tags inside the BoundField tag? How would I
call the field value using ASP.NET to assign to the variable? Or
maybe there's a better solution than using a GridView. Any other
ideas?

In this case you should rewrite a GridView_RowDataBound event.

Change <asp:GridView ID="GridView1"
OnRowDataBound="GridView1_RowDataBound"....

and add the GridView1_RowDataBound event to your code (code-behind)

C#:

protected void GridView1_RowDataBound(object sender,
GridViewRowEventArgs e)
{

if (e.Row.RowType == DataControlRowType.DataRow)
{
string c = e.Row.DataItem["RGBHex"]; // e.g. "#000000"
e.Row.BackColor = System.Drawing.ColorTranslator.FromHtml(c);
}

}

I didn't test it but it should work, I think
 
T

TF

Here's a snippet of the code created by MS Vis Web Developer 05
Express. The data from the table displays just fine. I want to set
the back color of each row based on the value of the "RGBHex" field.
<asp:GridView ID="GridView1" runat="server" AllowSorting="True"
AutoGenerateColumns="False"
DataKeyNames="ID" DataSourceID="AccessDataSource2"
EmptyDataText="There are no data records to
display.">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID"
ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="Brand" HeaderText="Brand"
SortExpression="Brand" />
<asp:BoundField DataField="Colorname"
HeaderText="Colorname" SortExpression="Colorname" />
<asp:BoundField DataField="RGBValue" HeaderText="RGBValue"
SortExpression="RGBValue" />
<asp:BoundField DataField="RGBHex" HeaderText="RGBHex"
SortExpression="RGBHex" />
</Columns>
</asp:GridView>
Could I use ASP 3 <%%> tags inside the BoundField tag? How would I
call the field value using ASP.NET to assign to the variable? Or
maybe there's a better solution than using a GridView. Any other
ideas?

In this case you should rewrite a GridView_RowDataBound event.

Change <asp:GridView ID="GridView1"
OnRowDataBound="GridView1_RowDataBound"....

and add the GridView1_RowDataBound event to your code (code-behind)

C#:

protected void GridView1_RowDataBound(object sender,
GridViewRowEventArgs e)
{

if (e.Row.RowType == DataControlRowType.DataRow)
{
string c = e.Row.DataItem["RGBHex"]; // e.g. "#000000"
e.Row.BackColor = System.Drawing.ColorTranslator.FromHtml(c);

}
}

I didn't test it but it should work, I think

OK. Where do I put that C# code snippet?
 
T

TF

Here's a snippet of the code created by MS Vis Web Developer 05
Express. The data from the table displays just fine. I want to set
the back color of each row based on the value of the "RGBHex" field.
<asp:GridView ID="GridView1" runat="server" AllowSorting="True"
AutoGenerateColumns="False"
DataKeyNames="ID" DataSourceID="AccessDataSource2"
EmptyDataText="There are no data records to
display.">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID"
ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="Brand" HeaderText="Brand"
SortExpression="Brand" />
<asp:BoundField DataField="Colorname"
HeaderText="Colorname" SortExpression="Colorname" />
<asp:BoundField DataField="RGBValue" HeaderText="RGBValue"
SortExpression="RGBValue" />
<asp:BoundField DataField="RGBHex" HeaderText="RGBHex"
SortExpression="RGBHex" />
</Columns>
</asp:GridView>
Could I use ASP 3 <%%> tags inside the BoundField tag? How would I
call the field value using ASP.NET to assign to the variable? Or
maybe there's a better solution than using a GridView. Any other
ideas?

In this case you should rewrite a GridView_RowDataBound event.

Change <asp:GridView ID="GridView1"
OnRowDataBound="GridView1_RowDataBound"....

and add the GridView1_RowDataBound event to your code (code-behind)

C#:

protected void GridView1_RowDataBound(object sender,
GridViewRowEventArgs e)
{

if (e.Row.RowType == DataControlRowType.DataRow)
{
string c = e.Row.DataItem["RGBHex"]; // e.g. "#000000"
e.Row.BackColor = System.Drawing.ColorTranslator.FromHtml(c);

}
}

I didn't test it but it should work, I think

OK. Where do I put that C# code snippet?
 
T

TF

Here's a snippet of the code created by MS Vis Web Developer 05
Express. The data from the table displays just fine. I want to set
the back color of each row based on the value of the "RGBHex" field.
<asp:GridView ID="GridView1" runat="server" AllowSorting="True"
AutoGenerateColumns="False"
DataKeyNames="ID" DataSourceID="AccessDataSource2"
EmptyDataText="There are no data records to
display.">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID"
ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="Brand" HeaderText="Brand"
SortExpression="Brand" />
<asp:BoundField DataField="Colorname"
HeaderText="Colorname" SortExpression="Colorname" />
<asp:BoundField DataField="RGBValue" HeaderText="RGBValue"
SortExpression="RGBValue" />
<asp:BoundField DataField="RGBHex" HeaderText="RGBHex"
SortExpression="RGBHex" />
</Columns>
</asp:GridView>
Could I use ASP 3 <%%> tags inside the BoundField tag? How would I
call the field value using ASP.NET to assign to the variable? Or
maybe there's a better solution than using a GridView. Any other
ideas?

In this case you should rewrite a GridView_RowDataBound event.

Change <asp:GridView ID="GridView1"
OnRowDataBound="GridView1_RowDataBound"....

and add the GridView1_RowDataBound event to your code (code-behind)

C#:

protected void GridView1_RowDataBound(object sender,
GridViewRowEventArgs e)
{

if (e.Row.RowType == DataControlRowType.DataRow)
{
string c = e.Row.DataItem["RGBHex"]; // e.g. "#000000"
e.Row.BackColor = System.Drawing.ColorTranslator.FromHtml(c);

}
}

I didn't test it but it should work, I think

OK. Where do I put that C# code snippet?
 
T

TF

Here's a snippet of the code created by MS Vis Web Developer 05
Express. The data from the table displays just fine. I want to set
the back color of each row based on the value of the "RGBHex" field.
<asp:GridView ID="GridView1" runat="server" AllowSorting="True"
AutoGenerateColumns="False"
DataKeyNames="ID" DataSourceID="AccessDataSource2"
EmptyDataText="There are no data records to
display.">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID"
ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="Brand" HeaderText="Brand"
SortExpression="Brand" />
<asp:BoundField DataField="Colorname"
HeaderText="Colorname" SortExpression="Colorname" />
<asp:BoundField DataField="RGBValue" HeaderText="RGBValue"
SortExpression="RGBValue" />
<asp:BoundField DataField="RGBHex" HeaderText="RGBHex"
SortExpression="RGBHex" />
</Columns>
</asp:GridView>
Could I use ASP 3 <%%> tags inside the BoundField tag? How would I
call the field value using ASP.NET to assign to the variable? Or
maybe there's a better solution than using a GridView. Any other
ideas?

In this case you should rewrite a GridView_RowDataBound event.

Change <asp:GridView ID="GridView1"
OnRowDataBound="GridView1_RowDataBound"....

and add the GridView1_RowDataBound event to your code (code-behind)

C#:

protected void GridView1_RowDataBound(object sender,
GridViewRowEventArgs e)
{

if (e.Row.RowType == DataControlRowType.DataRow)
{
string c = e.Row.DataItem["RGBHex"]; // e.g. "#000000"
e.Row.BackColor = System.Drawing.ColorTranslator.FromHtml(c);

}
}

I didn't test it but it should work, I think

OK. I may be getting this now. I made this the first line of my
default.aspx.

<%@ Page Language="C#" AutoEventWireup="false"
CodeFile="Default.aspx.cs" Inherits="_Default" %>


And this is the contents of the default.aspx.cs file. Renamed my grid
GridView2 by the way.


protected void GridView2_RowDataBound(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string c = e.Row.DataItem["RGBHex"];
e.Row.BackColor = System.Drawing.ColorTranslator.FromHtml(c);
}
}

Getting these compilation errors.

c:\...Default.aspx.cs(1,11): error CS1518: Expected class, delegate,
enum, interface, or struct
c:\...Default.aspx.cs(6,27): error CS1001: Identifier expected
c:\...Default.aspx.cs(9,1): error CS1022: Type or namespace
definition, or end-of-file expected

I feel like I'm getting close but I just can't get it. Thank you for
your help.

TF
 
G

Guest

In this case you should rewrite a GridView_RowDataBound event.
Change <asp:GridView ID="GridView1"
OnRowDataBound="GridView1_RowDataBound"....
and add the GridView1_RowDataBound event to your code (code-behind)

protected void GridView1_RowDataBound(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string c = e.Row.DataItem["RGBHex"]; // e.g. "#000000"
e.Row.BackColor = System.Drawing.ColorTranslator.FromHtml(c);

I didn't test it but it should work, I think

OK. I may be getting this now. I made this the first line of my
default.aspx.

<%@ Page Language="C#" AutoEventWireup="false"
CodeFile="Default.aspx.cs" Inherits="_Default" %>

And this is the contents of the default.aspx.cs file. Renamed my grid
GridView2 by the way.

protected void GridView2_RowDataBound(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string c = e.Row.DataItem["RGBHex"];
e.Row.BackColor = System.Drawing.ColorTranslator.FromHtml(c);

}
}

Getting these compilation errors.

c:\...Default.aspx.cs(1,11): error CS1518: Expected class, delegate,
enum, interface, or struct
c:\...Default.aspx.cs(6,27): error CS1001: Identifier expected
c:\...Default.aspx.cs(9,1): error CS1022: Type or namespace
definition, or end-of-file expected

I feel like I'm getting close but I just can't get it. Thank you for
your help.

TF- Hide quoted text -

- Show quoted text -

Mate, you have to start from the basic.

It told you that you miss a class, that is you have to define the
_Default

Remember, you said <%@ Page .... Inherits="_Default"
<!--------------- This is the name of your class

So, the code-behind should be like this:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}

...................code here...................

}
 
T

TF

Very newbie question and I hope this is the right group. I have a
table on a web page which contains info about paint colors. One column
contains the hex value for the color's RGB value. I want to display
info from the table in a grid or table and use the column value from
the RGBHex column to set each row's back ground color. I can just
about do this in my sleep in old style ASP using <%%> tags but I would
like to implement it in .NET 2. My website has this implemented in
old ASP here: www.paintmatcher.com/tool/tool.asp Any help is greatly
appreciated.

Got it to work now. Thank you all very much!
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top