Change color of Item Template TextBox on Condition

Change color of Item Template TextBox on Condition

Post by Alan Z. Sc » Thu, 28 Aug 2003 04:05:03


1. I wantto change the color of a datagrid ItemTemplate TextBox in all the
grid rows depending on radio button choice.

2. How can I do this in C#?

So far I have:

public void dgHoldings_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(rdoHoldingsDisplay.SelectedItem.Value == "Inactive")
{
TextBox txtBoxHolding = new TextBox();
txtBoxHolding = (TextBox) e.Item.Cells[1].Controls[0];
txtBoxHolding.ForeColor = System.Drawing.Color.Red;
}
}

I get Invalid Cast error, but it doesn't work without cast either.

Any help would be appreciated.

Thanks.

Alan
___________________________
Alan Z. Scharf
GrapeVine Systems
New York City
 
 
 

Change color of Item Template TextBox on Condition

Post by v-lwan » Thu, 28 Aug 2003 11:36:48

Hi Alan,

In DataGird.ItemDataBound method, the first e.Item is the Header of the
Datagrid, the last Item is the Footer of the Datagrid. There is no Textbox
in the Header of the Datagrid. You may use "if((e.Item.ItemType ==
ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))"
to solve this issue. The following is a code snippet to demonstrate this.

void Item_Bound(Object sender, DataGridItemEventArgs e)
{
if((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType ==
ListItemType.AlternatingItem))
{
. . .
}
}

If you still get the "Invalid Cast error, then the problem should be that
the textbox in the Datagrid ItemTemplate is the second control of the cell.
So, you may use "e.Item.Cells[1].Controls[1]" to get the reference of the
textbox.

You may check the following links for your reference.
ItemDataBound Event (.NET Framework Class Library)
< http://www.yqcomputer.com/
/frlrfsystemwebuiwebcontrolsdatagridclassitemdataboundtopic.asp>

DataGrid.UpdateCommand Event
< http://www.yqcomputer.com/
/frlrfsystemwebuiwebcontrolsdatagridclassupdatecommandtopic.asp>

Hope this helps.

Best regards,
Lewis

This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
| Reply-To: "Alan Z. Scharf" < XXXX@XXXXX.COM >
| From: "Alan Z. Scharf" < XXXX@XXXXX.COM >
| Subject: Change color of Item Template TextBox on Condition
| Date: Tue, 26 Aug 2003 15:05:03 -0400
| Lines: 31
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.3718.0
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3718.0
| Message-ID: < XXXX@XXXXX.COM >
| Newsgroups: microsoft.public.dotnet.framework.aspnet.datagridcontrol
| NNTP-Posting-Host: 207-237-240-45.c3-0.nyw-ubr2.nyr-nyw.ny.cable.rcn.com
207.237.240.45
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.aspnet.datagridcontrol:6357
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.datagridcontrol
|
| 1. I wantto change the color of a datagrid ItemTemplate TextBox in all
the
| grid rows depending on radio button choice.
|
| 2. How can I do this in C#?
|
| So far I have:
|
| public void dgHoldings_ItemDataBound(object sender,
| System.Web.UI.WebControls.DataGridItemEventArgs e)
| {
| if(rdoHoldingsDisplay.SelectedItem.Value == "Inactive")
| {
| TextBox txtBoxHolding = new TextBox();
| txtBoxHolding = (TextBox) e.Item.Cells[1].Controls[0];
| txtBoxHolding.ForeColor = System.Drawing.Color.Red;
| }
| }
|
| I get Invalid Cast error, but it doesn't work without cast either.
|
| Any help would be appreciated.
|
| Thanks.
|
| Alan
| ___________________________
| Alan Z. Scharf
| GrapeVine Systems
| New York City
|
|
|

 
 
 

Change color of Item Template TextBox on Condition

Post by Alan Z. Sc » Sun, 31 Aug 2003 04:11:27

ewis,

Thanks again for your reply.

I haven't had a chance to try your suggestion yet, but wanted to acknowledge
your reply.

Regards,

Alan

"Lewis Wang [MSFT]" < XXXX@XXXXX.COM > wrote in message
news: XXXX@XXXXX.COM ...
cell.
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html
rights.