如何将数据库列值绑定到radiobuttonlist [英] how to bind database column value to radiobuttonlist

查看:84
本文介绍了如何将数据库列值绑定到radiobuttonlist的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请告诉我如何将所有列值绑定到数据库中的radiobuttonlist,就像我在4列数据库表中有4个选项一样。我想阅读选项列到radiobuttonlist选择。我有代码,但有错误,它只读取第一列值到单选按钮列表,但我需要访问所有列值,如列ABCDE ro radiobuttonlist。请给我解决方案.....





pls some one tell that how to bind all column value to radiobuttonlist from database like i have 4 options in 4 columns of database table. i want to read option column to radiobuttonlist to selection. i have code but there is error it reads only 1st column value to radio button list but i need to access all column value like column ABCDE ro radiobuttonlist. pls give me solution.....


SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            //cmd.CommandText = "select id,Questions,A,B,C,D from Question where Id='" + t + "'";
            cmd.CommandText = "select id,Questions,A,B,C,D from Question";
            //cmd.Parameters.Add("@Id", SqlDbType.Float).Value = t;

            string uid = "";
            string Questions = "";
            string A = "";
            string B = "";
            string C = "";
            string D = "";

            cmd.Connection.Open();
            SqlDataReader rdr = cmd.ExecuteReader();

            if (rdr.HasRows)
            {
                //PanUpdate.Visible = true;
                //PanDisplay.Visible = false;

                while (rdr.Read())
                {
                    uid = rdr["Id"].ToString();
                    Questions = rdr["Questions"].ToString();
                    A = rdr["A"].ToString();
                    B = rdr["B"].ToString();
                    C = rdr["C"].ToString();
                    D = rdr["D"].ToString();
                }
                rdr.Close();
                rdr = cmd.ExecuteReader();

                //rdr.Close();
                //RadioButton1.DataBind = A;

                //RadioButton1.DataTextField = "Role_Name";
                //RadioButton1.DataSource = "Role_Name";
                //RadioButton3.DataTextField = "Role_Name";
                //RadioButton4.DataTextField = "Role_Name";
                //RadioButton5.DataTextField = "Role_Name";

                RadioButtonList1.DataSource = A;
                //RadioButtonList1.DataTextField = A;
                //RadioButtonList1.DataValueField = A;
                RadioButtonList1.DataBind();
                //LblUserID.Text = uid;
                //TxtPwdChange.Text = pwd;
                //LbxRole.Items.FindByText(rol).Selected = true;

                cmd.Connection.Close();







pls some one tell ........




pls some one tell........

推荐答案

这些链接可以帮助您

http://stackoverflow.com/questions/ 3917599 / populating-radiobuttonlist-dynamics [ ^ ]

http://forums.asp.net/t/1183321.aspx / 1 [ ^ ]
These links may help you
http://stackoverflow.com/questions/3917599/populating-radiobuttonlist-dynamically[^]
http://forums.asp.net/t/1183321.aspx/1[^]


试试这个(检查值为空,这样如果您选择的选项较少,例如是/否类型问题只有两个选项,将会很有帮助。

Try this (Check value for empty so it will be helpful if you are having less option e.g yes/no type question having only two option)
ListItem li = new ListItem();  
li.Text = A;  
li.Value = A;  
RadioButtonList1.Items.Add(li);  











or

DataTable table = new DataTable();
while (rdr.Read())
{   

table.Columns.Add("Option", typeof(string));
if(A!="")
table.Rows.Add(A);
if(B!="")
table.Rows.Add(B);
if(C!="")
table.Rows.Add(C);
if(D!="")
table.Rows.Add(D);
}
RadioButtonList1.DataTextField = "Option"
RadioButtonList1.DataValueField = "Option"
RadioButtonList1.DataSource = table;
RadioButtonList1.DataBind();


老兄试试这段代码

Dude try this code
RadioButtonList RadioButtonList1 = (RadioButtonList)e.Item.FindControl("RadioButtonList1");
             HiddenField hdn = (HiddenField)e.Item.FindControl("hf1");
             SqlDataAdapter ds = new SqlDataAdapter("select * from Question where QuestionId='" + hdn.Value + "'", con);
             DataTable ds1 = new DataTable();
             ds.Fill(ds1);
             RadioButtonList1.Items.Clear();
             RadioButtonList1.Items.Add(ds1.Rows[0]["OptionA"].ToString());
             RadioButtonList1.Items.Add(ds1.Rows[0]["OptionB"].ToString());
             RadioButtonList1.Items.Add(ds1.Rows[0]["OptionC"].ToString());
             RadioButtonList1.Items.Add(ds1.Rows[0]["OptionD"].ToString());
             RadioButtonList1.DataBind();


这篇关于如何将数据库列值绑定到radiobuttonlist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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