如何通过从数据库中检索其值来检查和突出显示单选按钮? [英] How do i get a radio button checked and highlighted by retrieving its value from a database?

查看:68
本文介绍了如何通过从数据库中检索其值来检查和突出显示单选按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个单选按钮列表,当我从数据库中检索其值并突出显示它时,我想要检查正确的单选按钮。这是我用来检索单选按钮值但我们没有检查它们的代码,但我希望它将所有值检索到单选按钮列表中,并检查我喜欢的一个单选按钮值。



我就是这样做的:



I have a radio button list, and i want the correct radio button to be checked when i retrieve its value from the database and also highlight it. This is the code i use to retrieve the radio button values but it doesn''t check them, but i want it to retrieve all the values into the radio button list and also check the one radio button i preferred value.

This is how i do it:

For i = 0 To trnum
               cmd.CommandText = "select * from test where Serial=" & a(i)
               adp = New SqlDataAdapter(cmd.CommandText, cnn)
               adp.Fill(ds, "test")
           Next




           cnn.Close() 'connection closed


           dt = New DataTable("Answered")
           dt.Columns.Add("Serial", GetType(Integer))
           dt.Columns.Add("question", GetType(String))
           dt.Columns.Add("choice1", GetType(String))
           dt.Columns.Add("choice2", GetType(String))
           dt.Columns.Add("choice3", GetType(String))
           dt.Columns.Add("choice4", GetType(String))
           dt.Columns.Add("correct", GetType(String))
           dt.Columns.Add("selected", GetType(Integer))
           dt.Columns.Add("explain", GetType(String))


           Dim r As DataRow

           For Each r In ds.Tables("test").Rows
               dr = dt.NewRow
               dr("Serial") = dt.Rows.Count + 1
               dr("question") = r.Item("question")
               dr("choice1") = r.Item("choice1")
               dr("choice2") = r.Item("choice2")
               dr("choice3") = r.Item("choice3")
               dr("choice4") = r.Item("choice4")
               dr("correct") = r.Item("correct")
               dr("explain") = r.Item("explain")
               dr("selected") = -1
               dt.Rows.Add(dr)

           Next

           Session("Answered") = dt

           Call show()










Sub show()
       dt = Session("Answered")
       Dim v As View = Me.View1


       Dim l As Label
       l = CType(v.FindControl("Label1"), Label)
       l.Text = dt.Rows(ctr).Item("Serial") & "."
       l = CType(v.FindControl("Label2"), Label)
       l.Text = dt.Rows(ctr).Item("question")



       Dim r As RadioButtonList
       r = CType(v.FindControl("RadioButtonList1"), RadioButtonList)
       r.Items.Clear()
       r.Items.Add(dt.Rows(ctr).Item("choice1"))
       r.Items.Add(dt.Rows(ctr).Item("choice2"))
       r.Items.Add(dt.Rows(ctr).Item("choice3"))
       r.Items.Add(dt.Rows(ctr).Item("choice4"))
       r.SelectedIndex = dt.Rows(ctr).Item("selected"











请帮我提供正确的代码或帮助链接,谢谢


)



Please help me with a correct code or a link to help, thanks

推荐答案

当你从数据库中返回值时要么你将保存文本或值。



如果您正在为数据库保存价值,请使用此项



When you return the values from the database Either you will be saving Text or value.

use this if you are saving value to database

RadioButtonList1.Items.FindByValue("your string value from database").Selected = True





如果要保存文本,请使用:





if you are saving text use this:

RadioButtonList1.Items.FindByText("your string value from database").Selected = True





试试这个。它的作品



Try this.it works


r.Items.Add(dt.Rows(ctr).Item("choice1"))
       r.Items.Add(dt.Rows(ctr).Item("choice2"))
       r.Items.Add(dt.Rows(ctr).Item("choice3"))
       r.Items.Add(dt.Rows(ctr).Item("choice4"))

< br $>


在你的代码下面添加以下一行



Add one of these line below your code

r.Items.FindByValue(dt.Rows(ctr).Item("correct").ToString()).Selected = True
or
r.SelectedValue = dt.Rows(ctr).Item("correct").ToString()





如果你想用选定的列作为索引那么





if you want to use selected column as index then

r.SelectedIndex = Convert.ToInt32(dt.Rows(ctr).Item("selected"))


这是我需要添加的内容,让它自动选择或检查正确答案:



This was what i needed to add to let it select or check the right answer automatically:

Me.RadioButtonList1.SelectedIndex = dt.Rows(ctr).Item("correct") - 1





感谢Code Project的优秀程序员成为好老师



Thank you great programmers on Code Project for being good teachers


这篇关于如何通过从数据库中检索其值来检查和突出显示单选按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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