如何通过数据库更改按钮的颜色 [英] how to change the color of the button through the databse

查看:90
本文介绍了如何通过数据库更改按钮的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

protected void Page_Load(object sender, EventArgs e)
{

    {
        string aa = "";
        SqlConnection con = new SqlConnection(connStr);

        SqlCommand sd = new SqlCommand("select max(v)  as a from seats", con);
        con.Open();
        SqlDataReader drr = sd.ExecuteReader();
        if (drr.Read())
        {
            aa = drr["a"].ToString();
        }
        con.Close();
        int d = 0;
        int f = 0;
        f = Convert.ToInt16(aa);
        Label1.Text = f.ToString();


        string[] arr3 = new string[f];
        string[] arr5 = new string[f];
       // string[] arr6 = new string[f];

        SqlCommand cmd = new SqlCommand("select Table_No,Chair_total,status,v from seats", con);
        con.Open();
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.Read())
        {
            arr3[d] = dr["Table_No"].ToString();
            arr5[d] = dr["Chair_total"].ToString();
           // arr6[d] = dr["status"].ToString();
            d++;
        }
        con.Close();

        d = 0;
        for (int i = 1; i <= f; i++)
        {
            Button btnSubmit = new Button();
            btnSubmit.ID = i.ToString();
           // btnSubmit.Text = "Tabel_no :- " + arr3[d] + "\n" + "Chair_total:- " + arr5[d] + "\n" + "status:- " + arr6[d];
            btnSubmit.Text = "Tabel_no :- " + arr3[d] + "\n" + "Chair_total:- " + arr5[d] ;
            btnSubmit.Width = 120;
            btnSubmit.Height = 120;
            // btnSubmit.Click += new System.EventHandler(btnSubmit_click);


            d++;
            Panel1.Controls.Add(btnSubmit);
            Response.Write("\n");



            string c;
            SqlCommand cmd2 = new SqlCommand("select image from seats where Table_No=''" + Tabel_no.Text + "'' and Chair_total=''" + Chait_total.Text + "'' and status=''" + status.Text + "'' ", con);
            con.Open();
            SqlDataReader dr2 = cmd2.ExecuteReader();
            if (dr2.Read())
            {
                c = dr2[0].ToString();
                if (c == "red")
                    btnSubmit.BackColor = System.Drawing.Color.Red;
                else if (c == "yellow")
                    btnSubmit.BackColor = System.Drawing.Color.Yellow;
                else
                    btnSubmit.BackColor = System.Drawing.Color.Green;
                c = "";

            }
            con.Close();
        }
    }
}





我正在使用数据库动态创建按钮,创建了我的按钮,但我也尝试使用数据库中的值更改按钮的颜色,并且还尝试从数据库中获取一些详细信息到我的按钮.但是iam无法执行此操作,因此请检查iam哪里出了错.



1-我想使用数据库值更改按钮的颜色.
2-希望使用数据库值在按钮文本上打印详细信息.





i am creating the button dynamical using the database, my buttons are created but iam also trying to change the color of button using the value in database, and also trying to fetch some details from database to my button . but iam failed to do this, so plz check where iam wrong.



1- i want change the color of button using the database value.
2- want to print the details over the button text using the database value.

推荐答案

hi
看到,在您的代码中,您的控件是Button的"btnSubmit".

但是,当您将其动态放置在表单上时,其ID会根据您的数据库字段值(btnSubmit.ID = i.ToString();)进行更改

所以现在您的按钮ID像1,2,3,4 .......

现在您正在页面上添加此控件.

之后,您将更改在页面上找不到的"btnSubmit"的颜色.

因此,您必须更改1,2,3,4 ........
的颜色
就像

hi
see, In your code your control is "btnSubmit" as Button.

But when you dynamically place it on form it id was change as per your database field value (btnSubmit.ID = i.ToString();)

so now your button id like 1, 2, 3, 4.......

now your are adding this control on page.

and after it you are changing the color of "btnSubmit" which can not find on page.

so, you have to change color of 1,2,3,4.....

like

con.Open();
                SqlDataReader dr2 = cmd2.ExecuteReader();
                if (dr2.Read())
                {
                    c = dr2[0].ToString();
                    if (c == "red")
                     ((Button)this.Control.FindByID["1"]).BackColor = System.Drawing.Color.Red;
                    else if (c == "yellow")
                        ((Button)this.Control.FindByID["2"]).BackColor = System.Drawing.Color.Yellow;
                    else
                        ((Button)this.Control.FindByID["3"]).BackColor = System.Drawing.Color.Green;
                    c = "";

                }
con.Close();



请记住,任何控件的唯一数字名称都会在编码时遇到麻烦.
因此您必须使用"btn" + i.ToString()
这样的前缀来生成它
因此您的控件将被添加为btn1,btn2,btn3,btn4 ........

您可以像
一样更改颜色



And remember that the only numeric name of any control will make trouble in coading.
so you have to generate it with prefix like "btn" + i.ToString()

so your control will be added as btn1, btn2, btn3, btn4........

and you can change color like

con.Open();
                SqlDataReader dr2 = cmd2.ExecuteReader();
                if (dr2.Read())
                {
                    c = dr2[0].ToString();
                    if (c == &quot;red&quot;)
                        ((Button)this.Control.FindByID["btn1"]).BackColor = System.Drawing.Color.Red;
                    else if (c == &quot;yellow&quot;)
                        ((Button)this.Control.FindByID["btn2"]).BackColor = System.Drawing.Color.Yellow;
                    else
                        ((Button)this.Control.FindByID["btn3"]).BackColor = System.Drawing.Color.Green;
                    c = "";

                }
con.Close();


这篇关于如何通过数据库更改按钮的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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