Solved

Help with setting text and background color of a textbox and table cell for a certain value

Posted on 2012-04-02
23
3,108 Views
Last Modified: 2012-04-11
Hello,

How do I set the  text and background color of a textbox and table cell for a certain value?
I am using xml as a data source but I don't think it should make a differenece.

Thanks,

Victor
0
Comment
Question by:vcharles
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 13
  • 8
  • 2
23 Comments
 
LVL 36

Expert Comment

by:Miguel Oz
ID: 37798650
Code behind:For textbox use backcolor and text property:
 TextBox1.BackColor = System.Drawing.Color.LavenderBlush;
 TextBox1.Text = "My sample text";


http://asp-net-example.blogspot.com.au/2009/03/how-to-set-change-textbox-backcolor.html
or using js:
http://stackoverflow.com/questions/8329490/how-to-change-background-color-of-textbox-by-javascript

Regarding table cell, is it part of an html table?
0
 

Author Comment

by:vcharles
ID: 37799444
Thanks, it is the table that is available from the toolbox.
0
 

Author Comment

by:vcharles
ID: 37799863
Hi,

Where in the report do I include.

If TextBox1.Text = "My sample text" then
TextBox1.BackColor = System.Drawing.Color.LavenderBlush;
End If

and How would I do the same for a particular cell in a table?

Victor
0
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
LVL 12

Expert Comment

by:Harish Varghese
ID: 37800214
Hello Victor,

You did not mention whether you wanted help with ASP.NET or SQL Server Reports. I guess you are referring to SQL Server Reporting, not ASP.NET.

In SQL Server reports, you can give expressions for Color (for font color) and Background Color properties.

1. Click View Menu > Properties Window, to make sure that your Properties window is open.
2. Select Textbox or Table Cell you want to apply color. Your Property window will reflect the properties of selected object.
3. In case you want to mention a color directly, you can mention it against Color and Background Color properties.
4. In case you want color to be applied based on an expression, you can click on the arrow against the property (please see screenshot attached) and select "Expression".
Example for color expression:
   =iif(Fields!NetAvailableHours.Value < 0.00,"Red", "Black")

Thanks,
Harish
color-property.jpg
0
 

Author Comment

by:vcharles
ID: 37806487
Harish,

I am using ASP.NET with XML datasource, can you please send me an example that would work with xml as the datasource. I am creating tables to copy my xml data.

Thanks,

Victor
0
 
LVL 12

Expert Comment

by:Harish Varghese
ID: 37815535
Hello Victor,

I am not good in ASP.NET. But if you can send me a sample of what you are trying to do, I will try to help.

Thanks,
Harish
0
 

Author Comment

by:vcharles
ID: 37815581
Hi harish,

I got it to work with by putting the code below in the expression section for Background color

= IIF(Fields!BM.Value = "1","Red", "Silver")

Problem is the the expression already contained the code  to change the backgground color for even rows, is there a way to combine both code? I still need the initial code to change to background color of eveb rows.

Thanks,

Victor
0
 

Author Comment

by:vcharles
ID: 37815607
Below is the code that was initially in all the cells of the row
= IIF(RowNumber(Nothing) Mod 2 = 1, "Silver", "Transparent")
I need to somehow keep it while I add the new code for one particular cell.
0
 
LVL 36

Expert Comment

by:Miguel Oz
ID: 37817789
Victor:
Where in the report do I include.
If TextBox1.Text = "My sample text" then
TextBox1.BackColor = System.Drawing.Color.LavenderBlush;
End If
>> You can put it under page_prerender event . I am assuming that your are binding the xml data source in markup.


and How would I do the same for a particular cell in a table?
>> Can you post the table markup and the code you used to fill it in??
>> As an alternative, if you only have two colour choices a more elegant solution is to use css not server code. check "Efficient Alternating Table Row Colors" section of link below:
http://www.somacon.com/p338.php
0
 

Author Comment

by:vcharles
ID: 37818564
I tried the following 2 options below, but it still doesn't work, i could not find the page-prerender event in the report's property. Cab you please send me some more details.

1, Textbox's backcolor property of textbox in my RDLC file.
=iif(Fields!Donor.Value = "Receiver.Text","Red", "Black")

2. Form event

 Private Sub ReportViewer1_RenderingBegin(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles ReportViewer1.RenderingBegin
        If Receiver.Text = "DNK" Then
            TextBox1.BackColor = System.Drawing.Color.LavenderBlush
        End If

    End Sub

Thanks,

V.
0
 
LVL 12

Expert Comment

by:Harish Varghese
ID: 37831388
Hello Victor,

Sorry, I was on a vacation. We can achieve this by combining the background color expressions for alternate rows and specific values.

Assume that you want all odd rows to be "Silver" and even rows to be "Transparent". In addition, for few cells based on their values (eg. Receiver = "DNK", then Pink background). You can use "Switch" statement to add multiple conditions in the background color property of the cells. No need to add code to form events.

1. Use below expression for background color for all cells in the row:
    =Switch(RowNumber(Nothing) Mod 2 = 1, "Silver",
      RowNumber(Nothing) Mod 2 = 0, "Transparent")

2. Use below expression for background color for special cells where you need color based on their value. Change field name and values as required:
    =Switch(Fields!Receiver.Value = "DNK, "Pink",
      RowNumber(Nothing) Mod 2 = 1, "Silver",
      RowNumber(Nothing) Mod 2 = 0, "Transparent")

    You can add more conditions and values (colors) as you want. You can also think about changing font color for special cells while retaining standard background colors.

Thanks,
Harish
0
 

Author Comment

by:vcharles
ID: 37831542
Harish,

It worked!  Thank You!
What if I wanted to set multiple conditions? For example I am using the code below to change the background color of BM when value is 0, what if I wanted to give a different background color when value is 1?

=Switch(Fields!BM.Value = "0", "Pink",
      RowNumber(Nothing) Mod 2 = 1, "Silver",
      RowNumber(Nothing) Mod 2 = 0, "Transparent")
0
 

Author Comment

by:vcharles
ID: 37831563
Harish,

Forgot an important part of my initial post, I need to do the same what a value from the Textbox on my form, but the code below does not work.

=Switch(Fields!BM.Value = "Textbox1.Text", "Pink",
      RowNumber(Nothing) Mod 2 = 1, "Silver",
      RowNumber(Nothing) Mod 2 = 0, "Transparent")
0
 
LVL 12

Accepted Solution

by:
Harish Varghese earned 500 total points
ID: 37831837
Items that are not part of dataset can be accessed using ReportItems property. Please use below code for BackgroundColor expression for textboxes:

=iif(ReportItems!Textbox34.Value = "1", "LightGreen", "Pink")

Change textbox name, value and colors as required.

Thanks,
Harish
0
 

Author Comment

by:vcharles
ID: 37831997
Hi,

Unfortunately I'm still having problems.

I tried

=iif(ReportItems!Textbox11.Value = "1", "LightGreen", "Pink")

 but I receive the following error:

The BackgroundColor expression for the textbox ‘BM’ refers to the report item ‘Textbox11’.  Report item expressions can only refer to other report items within the same grouping scope or a containing grouping scope.      

I alsoe need to change the colors depending on what the user enters rather then a fix value, for example Textbox11.value = Textbox11.Text but .Text is not recognized.

Thanks.

Victor
0
 

Author Comment

by:vcharles
ID: 37832078
Hi again,

Textbox11 is on my aspx Form not the rdlc page. Perhaps that is what is causing the error?

Victor
0
 
LVL 12

Expert Comment

by:Harish Varghese
ID: 37832120
Victor,

OK.. I was thinking that the textbox was on the report. Let us not mix ASP.NET and Report file. RDLC cannot access your controls on your ASPX page.
Were you able to achieve whatever you wanted with Reports (RDLC file)?

Thanks,
Harish
0
 

Author Comment

by:vcharles
ID: 37832160
Yes.  Thanks. I will close this case.

Are you certain there is no way to pass values entered in an ASPX Form to an RDLC report?

Victor
0
 
LVL 12

Expert Comment

by:Harish Varghese
ID: 37832203
Hello Victor,

What I mentioned is "RDLC cannot access your controls on your ASPX page" directly. But there are means to pass parameters into the report from ASPX page. Please see below thread:
http://social.msdn.microsoft.com/Forums/en-IN/sqlreportingservices/thread/faa618fe-f2c2-486d-b5a5-c6bba4e067b1

I find it very difficult to understand your exact requirement. It is always better to post multiple questions on different threads and your question will get attention from experts in respective group.

If you are not able to resolve your issue still, please explain what you want to pass to the report and how that should be used in the report.
And, is there anything that ASPX page wants to access from the report?

Thanks,
Harish
0
 

Author Comment

by:vcharles
ID: 37832242
Harish,

I wanted to modify the background of certain fields in my report depending of the values users enter in Textboxes  from my ASPX form. I will look at your link and possibly open a new case related to that question.

Your solution for modifying the report depending of fixed values was very helpful. You forgot to answer one of my questions regarding modifying same field depending on different values:

What if I wanted to set multiple conditions? For example I am using the code below to change the background color of BM when value is 0, what if I wanted to give a different background color when value is 1?

=Switch(Fields!BM.Value = "0", "Pink",
      RowNumber(Nothing) Mod 2 = 1, "Silver",
      RowNumber(Nothing) Mod 2 = 0, "Transparent")


Thanks,

Victor
0
 
LVL 12

Expert Comment

by:Harish Varghese
ID: 37832355
Sorry, I did not see that question. But I had mentioned earlier itself that you can add more conditions and colors as you want. See below:

=Switch(Fields!BM.Value = "0", "Pink",
      Fields!BM.Value = "1", "LightGreen",
      RowNumber(Nothing) Mod 2 = 1, "Silver",
      RowNumber(Nothing) Mod 2 = 0, "Transparent")

You can also parameterize the colors. For example, you can add two string parameters to the report - Color1 and Color2, and modify the expression as below:

=Switch(Fields!BM.Value = "0", Parameters!Color1.Value,
      Fields!BM.Value = "1", Parameters!Color2.Value,
      RowNumber(Nothing) Mod 2 = 1, "Silver",
      RowNumber(Nothing) Mod 2 = 0, "Transparent")

You can either use the parameters as Internal and provide default values, OR pass the colors from user input. Please see the link I posted earlier to know how.

Thanks,
Harish
0
 

Author Comment

by:vcharles
ID: 37832556
Harish,

Thanks, I will look at your link. I am assuming that I need to add the colors as shown on the code below

=Switch(Fields!BM.Value = "0", Parameters!Color1.Value, "Red"
      Fields!BM.Value = "1", Parameters!Color2.Value, "LightGreen"
      RowNumber(Nothing) Mod 2 = 1, "Silver",       RowNumber(Nothing) Mod 2 = 0, "Transparent")

which would mean if Value is 0 than background color is Red and if Value is 1 background color is LightGreen right?
0
 
LVL 12

Expert Comment

by:Harish Varghese
ID: 37832779
No.

You can either use:

=Switch(Fields!BM.Value = "0", "Pink",
      Fields!BM.Value = "1", "LightGreen",
      RowNumber(Nothing) Mod 2 = 1, "Silver",
      RowNumber(Nothing) Mod 2 = 0, "Transparent")

OR

=Switch(Fields!BM.Value = "0", Parameters!Color1.Value,
      Fields!BM.Value = "1", Parameters!Color2.Value,
      RowNumber(Nothing) Mod 2 = 1, "Silver",
      RowNumber(Nothing) Mod 2 = 0, "Transparent")

which means, instead of hardcoding "Red", you can use a parameter which has value "Red".
If you are not familiar with parameters in SSRS, please read step 10 in below link:
http://arcanecode.com/2010/07/13/adding-query-parameters-to-sql-server-2008-reporting-services-reports/

You can also read this link to see how to pass parameters to report using code:
http://stackoverflow.com/questions/368766/how-to-pass-parameters-to-ssrs-report-programmatically

Thanks,
Harish
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A recent questions about how to add SSRS named instances, couldn't find any that talks about SQL server 2008, anyway I decided to help by creating some screen shots. The installation is straightforward, you just pop the SQL server 2008 installati…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question