Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
if (!IsPostBack)
       {


           for (int b =23; b < 100;b++ )
           {
               conn.Open();
               cmd = new SqlCommand("select * from tbl_plotdetails where plotdetails_id='" + b + "' and status='vacant' ", conn);
               dr = cmd.ExecuteReader();
               dr.Read();
               if (dr.HasRows)
               {
                   int a = b;
                   string d = "G" + a;  // convert to td id   G23


                   //d.BgColor = System.Drawing.Color.White.ToString(); //cannot accept
                   //d.Style.Add("background-color", "red");

                   G23.Style.Add("border-color", "black");   // accept this
                   dr.Close();

               }
               else
               {

                   G23.Visible = false;

               }
               conn.Close();
           }

       }


how i change a string into id of a particular div
Posted

You need to use Control.FindControl("id") method to access the object by passing the id.
C#
if (!IsPostBack)
        {
            
 
            for (int b =23; b < 100;b++ )
            {
                conn.Open();
                cmd = new SqlCommand("select * from tbl_plotdetails where plotdetails_id='" + b + "' and status='vacant' ", conn);
                dr = cmd.ExecuteReader();
                dr.Read();
                Control control=null;
                if (dr.HasRows)
                {
                    int a = b;
                    string d = "G" + a;  // convert to td id   G23
                    control = Page.FindControl(d);
                    
                    //d.BgColor = System.Drawing.Color.White.ToString(); //cannot accept
                    //d.Style.Add("background-color", "red");
                    
                    control.Style.Add("border-color", "black");   // accept this
                    dr.Close();
                   
                }
                else
                {
                    //G23 object will not be accessible here
                    G23.Visible = false; //can be written as                  control.Visible = false;
 
                }
                conn.Close();
            }
 
        }
 
Share this answer
 
Comments
Arasappan 23-Jul-15 3:17am    
If i add this doest not exist in the current context. Is there any namespace for this sir
Praveen Kumar Upadhyay 23-Jul-15 3:26am    
Namespace for what?
Arasappan 23-Jul-15 3:36am    
if I use
control = Page.FindControl(d);
control does not exist in the current context
Praveen Kumar Upadhyay 23-Jul-15 3:42am    
control is an object defined by us. So it should be accessible. I am not clearly able to understand you. can you mention line where you see a problem.
Arasappan 23-Jul-15 3:48am    
as same u given by it shown control is doesnt exist in the current context.what is the object tye
You need to add runat="server" attribute to the DIV html tag and then can use control.findcontrol method to find the associated div. Check the link below.

Find div tag from code behind[^]
 
Share this answer
 
Comments
Arasappan 23-Jul-15 3:15am    
<td id=g2>

i get string g2 mean
i want to use that string as a id

example

g2.visible=false;

for thid
string x=g2;
x.visible=false
C#
if (!IsPostBack)
        {

            for (int b =23; b < 100;b++ )
            {
                conn.Open();
                cmd = new SqlCommand("select * from tbl_plotdetails where plotdetails_id='" + b + "' and status='vacant' ", conn);
                dr = cmd.ExecuteReader();
                dr.Read();
                Control control=null;
                if (dr.HasRows)
                {
                    int a = b;
                    string d = "G" + a;  // convert to td id   G23
                    HtmlTableCell lbl = (HtmlTableCell)FindControl(d);

                    //d.BgColor = System.Drawing.Color.White.ToString(); //cannot accept
                    lbl.Style.Add("background-color", "red");

                    control.Style.Add("border-color", "black");   // accept this
                    dr.Close();

                }
                else
                {
                    //G23 object will not be accessible here
                    G23.Visible = false; //can be written as                  control.Visible = false;

                }
                conn.Close();
            }

        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900