Link to home
Start Free TrialLog in
Avatar of pgilfeather
pgilfeather

asked on

How do you programmatilly select a value from a radiobuttonlist?

How do you programmatilly select a value from a radiobuttonlist?

Suppose I have

id=Radiobuttonlist1

how do I automatically make a selection from this???

Regards

Paul G
Avatar of Corey Scheich
Corey Scheich
Flag of United States of America image

radioButton1.PerformClick()

Regards,

Corey
Label1.Text = "You selected: " & RadioButtonList1.SelectedItem.Text

~b
Avatar of pgilfeather
pgilfeather

ASKER

Thanks but, with all due respect  none of the above is really a serious answer to my question
Avatar of Howard Cantrell
Sample code....

Public Class frmRadioButt
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
    Friend WithEvents RadioButton1 As System.Windows.Forms.RadioButton
    Friend WithEvents RadioButton2 As System.Windows.Forms.RadioButton
    Friend WithEvents RadioButton3 As System.Windows.Forms.RadioButton
    Friend WithEvents RadioButton4 As System.Windows.Forms.RadioButton
    Friend WithEvents RadioButton5 As System.Windows.Forms.RadioButton
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.GroupBox1 = New System.Windows.Forms.GroupBox
        Me.RadioButton1 = New System.Windows.Forms.RadioButton
        Me.RadioButton2 = New System.Windows.Forms.RadioButton
        Me.RadioButton3 = New System.Windows.Forms.RadioButton
        Me.RadioButton4 = New System.Windows.Forms.RadioButton
        Me.RadioButton5 = New System.Windows.Forms.RadioButton
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.GroupBox1.SuspendLayout()
        Me.SuspendLayout()
        '
        'GroupBox1
        '
        Me.GroupBox1.Controls.Add(Me.RadioButton5)
        Me.GroupBox1.Controls.Add(Me.RadioButton4)
        Me.GroupBox1.Controls.Add(Me.RadioButton3)
        Me.GroupBox1.Controls.Add(Me.RadioButton2)
        Me.GroupBox1.Controls.Add(Me.RadioButton1)
        Me.GroupBox1.Location = New System.Drawing.Point(12, 24)
        Me.GroupBox1.Name = "GroupBox1"
        Me.GroupBox1.Size = New System.Drawing.Size(244, 196)
        Me.GroupBox1.TabIndex = 0
        Me.GroupBox1.TabStop = False
        Me.GroupBox1.Text = "GroupBox1"
        '
        'RadioButton1
        '
        Me.RadioButton1.Location = New System.Drawing.Point(12, 28)
        Me.RadioButton1.Name = "RadioButton1"
        Me.RadioButton1.Size = New System.Drawing.Size(212, 24)
        Me.RadioButton1.TabIndex = 0
        Me.RadioButton1.Text = "RadioButton1"
        '
        'RadioButton2
        '
        Me.RadioButton2.Location = New System.Drawing.Point(12, 60)
        Me.RadioButton2.Name = "RadioButton2"
        Me.RadioButton2.Size = New System.Drawing.Size(212, 24)
        Me.RadioButton2.TabIndex = 1
        Me.RadioButton2.Text = "RadioButton2"
        '
        'RadioButton3
        '
        Me.RadioButton3.Location = New System.Drawing.Point(12, 92)
        Me.RadioButton3.Name = "RadioButton3"
        Me.RadioButton3.Size = New System.Drawing.Size(212, 24)
        Me.RadioButton3.TabIndex = 2
        Me.RadioButton3.Text = "RadioButton3"
        '
        'RadioButton4
        '
        Me.RadioButton4.Location = New System.Drawing.Point(12, 124)
        Me.RadioButton4.Name = "RadioButton4"
        Me.RadioButton4.Size = New System.Drawing.Size(212, 24)
        Me.RadioButton4.TabIndex = 3
        Me.RadioButton4.Text = "RadioButton4"
        '
        'RadioButton5
        '
        Me.RadioButton5.Location = New System.Drawing.Point(12, 156)
        Me.RadioButton5.Name = "RadioButton5"
        Me.RadioButton5.Size = New System.Drawing.Size(212, 24)
        Me.RadioButton5.TabIndex = 4
        Me.RadioButton5.Text = "RadioButton5"
        '
        'TextBox1
        '
        Me.TextBox1.Location = New System.Drawing.Point(44, 236)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.Size = New System.Drawing.Size(132, 20)
        Me.TextBox1.TabIndex = 1
        Me.TextBox1.Text = ""
        '
        'frmRadioButt
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 273)
        Me.Controls.Add(Me.TextBox1)
        Me.Controls.Add(Me.GroupBox1)
        Me.Name = "frmRadioButt"
        Me.Text = "frmRadioButt"
        Me.GroupBox1.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region


    Private Sub CheckRadio(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged, RadioButton2.CheckedChanged, _
    RadioButton3.CheckedChanged, RadioButton4.CheckedChanged, RadioButton5.CheckedChanged
        TextBox1.Text = sender.text
    End Sub
End Class
ASKER CERTIFIED SOLUTION
Avatar of bramsquad
bramsquad
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
so i think you would want...

RadioButtonList.Items("id").Selected = True
sorry, omit the quotes
Or if you want to figure out which radiobutton has been selected

select case True
   case id(1)


   case id(2)


  case id(3)

End select

or

dim ThisButton as object
Dim SelectedButton as radiobutton

For each Thisbutton in ID
     if thisbutton.value = True then
        set selectedbutton = thisbutton
        exit for
    end if
next

Corey
Hi,
Try this
radiobuttonlist1.item(x).selected = true
GL
sorry about the vb6 set call
Corey2,

This is exactly what I want to do loop through and check which ones have been selected and if not selected then select an incorrect answer

here is how I am generating each question from the datasource

Dim cnn As String = (ConfigurationSettings.AppSettings("mydatasource"))
    Dim objConnection as new sqlConnection(cnn)
      Dim objCommand as new SqlCommand("procGetQuestionAttributes", ObjConnection)
    objCommand.CommandType = CommandType.StoredProcedure
      
      Dim ObjQuestionID1 as new SqlParameter("@QuestionID",SqlDbType.int)
      ObjCommand.Parameters.Add(ObjQuestionID1)
      ObjQuestionID1.Direction=ParameterDirection.Input
      ObjQuestionID1.Value=MyArray(0)
      
      Try
      objConnection.Open()
      RadQ1.DataSource=ObjCommand.ExecuteReader()
      RadQ1.DataTextField="AnswerDescription"
      RadQ1.DataValueField = "CorrectAnswers"
      RadQ1.DataBind()
      
      objConnection.Close()
      
      Catch ex as Exception
      Response.Write("Error:" & ex.Message & "<br/>")
      End Try
GERENTE

what do I put in for x

radiobuttonlist1.item(x).selected = true
X = index of the item

if you want the "first radio button" use.
radiobuttonlist1.item(0).selected = true
GL
Heres the code that is generating the error

The compiler is stopping at the first line and moaning about it being set to a null object.


If RadFSAQ1.SelectedItem.Text = "" or RadFSAQ1.SelectedItem.Value = "" Then
'RadFSAQ1.SelectedItem.Text =NoSelectedText
RadFSAQ1.SelectedItem.Value =NoSelectedValue
End If
GERENTE,

This is the error that occurred with radiobuttonlist1.item(0).selected = true

BC30456: 'item' is not a member of 'System.Web.UI.WebControls.RadioButtonList'.
i hate to do this...but i stated that exact answer before GERENTE did...

also, there is no Item() member, its items() like i said...


--------------------------------
geez, i read it totally wrong.

RadioButtonList.Items(yourIndex).Selected = True
--------------------------------
bramsquad

Im really sorry bram, it wasnt intentional, Im just getting stressed cos I havent solved this problem yet.

I will have another look at your original postings. Sorry.

Regards

Paul
' this code must go into #Region " Web Form Designer Generated Code "
Protected WithEvents RadFSAQ1 As System.Web.UI.WebControls.RadioButtonList


If RadFSAQ1.Items.Count > 0 Then
         If RadFSAQ1.SelectedItem.Text.Equals("") Or RadFSAQ1.SelectedItem.Value.Equals("") Then
            RadFSAQ1.SelectedItem.Selected = False
            RadFSAQ1.SelectedItem.Value = "NoSelectedValue"
         End If
End If

If this is not the solution can you explain me what you need to do ?
GL
GERENTE,

I have accepted another answer but you have helped me a lot on this question so I am going to open up another question to continue the rest of what I am trying to do. Please check this VB.NET section tomorrow.

Thanks very much for all your help, I really do appreciate it very much.

I'll continue this thread tomorrow.

Thanks Guys/Gals

Paul G
Radiobutton value is stored in the Checked property.
The Checked value is boolean
I don't know if that is what is causing the problem though

If RadFSAQ1.SelectedItem.Text = "" or _
RadFSAQ1.SelectedItem.Checked = False Then

'Your next line is commented
'RadFSAQ1.SelectedItem.Text = NoSelectedText
RadFSAQ1.SelectedItem.Checked = NoSelectedValue

End If

regards
Corey
you said the purpose of your code was to select all "incorrect" values which have not been selected?  what about this?

For nCount = 0 To RadioButtonList.Items.Count - 1
      If RadioButtonList.Items(nCount).Selected = False Then
             RadioButtonList.Items(nCount).Value = "NoSelectedValue"
      End if
Next