0

I am using a infragistics component called webdatagrid, inside which row I have got a user control with 48 divs inside, I am making a day scheduler for a company, now after I find the component, I want to check the background color of which of the 48 divs...if white its normal, if gray it means that person is working. so the user control called segments I have this code:

<style type="text/css">
.divttt
{
background-color:#fff;
width:30px;height:32px;
float:left;
border-bottom:solid 1px #C0C0C0 ;
border-top:solid 1px #C0C0C0 ;
border-left:solid 1px #C0C0C0
-moz-border-radius: 5px; /* Firefox */
-webkit-border-radius: 5px; /* Safari, Chrome */
 border-radius: 5px; /* universal */
 }
.divtoleft
{
 
width:30px;
height:32px;
float:left;
}
</style>
 <div runat="server" class="divtoleft"> </div>
 <div  id="div1" runat="server" class="divttt">
 </div>
 <div  id="div2" runat="server" class="divttt">
 </div>
 <div  id="div3" runat="server" class="divttt">
 </div>
 <div  id="div4" runat="server" class="divttt">
 </div>
 <div  id="div5" runat="server" class="divttt">

...and continues up to the div48... when the page load all divs are white, then user select which person is working and how many hours, by changing the color to gray. when the user press save I want to see what is the color of the div...

String[] _arrHoras = new String[] { "00:00", "00:30", "01:00", "01:30", "02:00", 
                                    "02:30", "03:00", "03:30", "04:00", "04:30", 
                                    "05:00", "05:30", "06:00", "06:30", "07:00", 
                                    "07:30", "08:00", "08:30", "09:00", "09:30", 
                                    "10:00", "10:30", "11:00", "11:30", "12:00", 
                                    "12:30", "13:00", "13:30", "14:00", "14:30", 
                                    "15:00", "15:30", "16:00", "16:30", "17:00", 
                                    "17:30", "18:00", "18:30", "19:00", "19:30", 
                                    "20:00", "20:30", "21:00", "21:30", "22:00", 
                                    "22:30", "23:00", "23:30", "24:00" };
    
public string[] getHorasIniciais()
{
    int branco = 0;
    int preto = 0;
    int posarr = 0;
    string[] arrHi= new string[] {""};
    for (int pos = 0; pos < 48; pos++)
    {

        string sControlId = "div" + (pos + 1);
        Control cControlDiv = FindControl(sControlId);
        HtmlGenericControl dDiv = (HtmlGenericControl)cControlDiv;
        string t="";
        div1.Attributes["background-color"] = t;<--the problem is here...
        //if (color.Equals("#909090") == true || color.Equals("rgb(144, 144, 144)") == true)
        //{
        //    if (preto == 0)
        //    {
        //        arrHi.SetValue(_arrHoras[pos], posarr);
        //        posarr++;
        //        preto = 1;
        //    }
        //    if (pos > 47)
        //    {
        //        preto = 0;
        //        arrHi.SetValue(_arrHoras[pos], posarr);
        //    }
        //}
        //else {
        //    if (preto == 1)
        //    {
        //        preto = 0;
        //    }
        //}

    }

    return arrHi;
}

what this code does is to check all of the 48 divs and check the background color, if is white do nothing if is grey, it puts the correspondent time of that block on the array arrHi. The problem is that I can't get the value of background color and compare it...

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Hugo Silva
  • 352
  • 8
  • 26
  • try div1.styles["background-color"]. and if you use css class for backgroun-color you cant get background-color in c#. I think –  Dec 02 '13 at 12:33
  • div1.Style["background-color"]=t; i have tried this and the value of t is null.. – Hugo Silva Dec 02 '13 at 12:36
  • http://stackoverflow.com/questions/18020779/how-to-use-code-behind-to-create-stackpanel-border-background – Monika Dec 02 '13 at 12:37
  • then you must set background-color in inline style not in css class. something like this **
    **
    –  Dec 02 '13 at 12:41
  • did the new background-color coming from a css class? then you can check the css class not the background-color! div1.Attributes["class"] != "divttt" –  Dec 02 '13 at 12:46
  • Raika, ive it doesnt work, the value of t is null... – Hugo Silva Dec 02 '13 at 12:52
  • no i have made the changes that you tell me, put the background color inside a style tag in the div...i am trying your changes now.. – Hugo Silva Dec 02 '13 at 12:55
  • Raika i have put your code inside a if...it tells me that the class is the same... – Hugo Silva Dec 02 '13 at 13:01
  • The problem still maintain, i cant get the current background color...the color can be changed using a jquery func.. – Hugo Silva Dec 04 '13 at 12:07

1 Answers1

0

There is a possibility to get the style property from a HTML element in codebehind:

  • Add the runat="server" tag to all your divs
  • Add the id tag to all your div's
  • In code behind use this code to get the style's attribute content:
var div = (HtmlGenericControl)this.Page.FindControl("MyDiv");    
var style = div.Attributes["style"];

You would then have to parse the style attribute for the background-color. However this does not work with CSS-Classes like you are using. You would have to set the background-color on each div separately in order to be able to read it on codebehind.

RononDex
  • 4,143
  • 22
  • 39
  • Thanks for your aswer, but still it's not working, it is no going to get the current background color...but insteads fetch the style that was defined on the file.. – Hugo Silva Dec 04 '13 at 11:39
  • As I said in the answer, you have to define the background-color directly on the div, instead of using a css class that defines the background-color – RononDex Dec 04 '13 at 11:41
  • but how can i get the new background color, if i change it on runtime using jquery, 1 click goes to grey, another goes to white again... – Hugo Silva Dec 04 '13 at 12:06
  • You can't do that. The server code will only be run when the **client** sends a **request** to the server. You can't access the current html from the server on the client. This is **BASIC** understanding of web technology. Stackoverflow is for coding issues, not to teach newcomers how basic things work. If you want to tell the server that the color has changed without reloading the webpage, you would have to use AJAX requests. – RononDex Dec 04 '13 at 12:08