如何将字符串转换为id [英] how convert a string to id

查看:228
本文介绍了如何将字符串转换为id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 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; //转换为td id G23


//d.BgColor = System.Drawing.Color.White.ToString(); //不能接受
//d.Style.Add(\"background-color,red);

G23.Style.Add(border-color,black); //接受这个
dr.Close();

}
其他
{

G23.Visible = false;

}
conn.Close();
}

}





如何将字符串更改为a的id特殊div

解决方案

您需要使用Control.FindControl(id)方法通过传递id来访问对象。

< pre lang =c#> if (!IsPostBack)
{


for int b = 23 ; b < 100 ; b ++)
{
conn.Open();
cmd = new SqlCommand( select *来自tbl_plotdetails其中plotdetails_id =' + b + '和status ='vacant',conn);
dr = cmd.ExecuteReader();
dr.Read();
控制权控制= null ;
if (dr.HasRows)
{
int a = b;
string d = G + a; // 转换为td id G23
control = Page.FindControl(d);

// d.BgColor = System.Drawing.Color.White.ToString(); //无法接受
// d.Style.Add(background-color, red);

control.Style.Add( border- color black); // 接受此
dr.Close();

}
else
{
// 此处无法访问G23对象
G23.Visible = false ; // 可写为control.Visible = false;

}
conn.Close();
}

}


你需要将runat =server属性添加到DIV html标签然后才能使用control.findcontrol方法查找关联的div。检查下面的链接。



从后面的代码中找到div标签 [ ^ ]


  if (!IsPostBack)
{

for int b = 23 ; b < 100 ; b ++)
{
conn.Open();
cmd = new SqlCommand( select *来自tbl_plotdetails其中plotdetails_id =' + b + '和status ='vacant',conn);
dr = cmd.ExecuteReader();
dr.Read();
控制权控制= null ;
if (dr.HasRows)
{
int a = b;
string d = G + a; // 转换为td id G23
HtmlTableCell lbl =(HtmlTableCell)FindControl(d);

// d.BgColor = System.Drawing.Color.White.ToString(); //无法接受
lbl.Style.Add( background-color red);

control.Style.Add( border-color black); // 接受此
dr.Close();

}
else
{
// 此处无法访问G23对象
G23.Visible = false ; // 可写为control.Visible = false;

}
conn.Close();
}

}


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

解决方案

You need to use Control.FindControl("id") method to access the object by passing the id.

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();
            }
 
        }


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[^]


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();
            }

        }


这篇关于如何将字符串转换为id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆