Get the value of Radiobutton in repeater in asp. Net vb. Net

Diksha dudi 126 Reputation points
2022-04-11T20:51:52.763+00:00

My radiobutton checked status is always coming false. It always goes in else condition. 192038-screenshot-27.png191949-screenshot-28.png191996-screenshot-29.png

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,365 questions
{count} votes

Accepted answer
  1. Albert Kallal 5,146 Reputation points
    2022-04-11T23:45:42.167+00:00

    >Actually java script is for single selection, without it, there are multiple selection.

    Ok, this is confusing?

    A single RadioButton does not have, nor allow multiple selections. It has ONE value. Either you select it, or you don't?

    You can for each repeater row shove in any "text" value for the RadioButton, but that does NOT result in a multiple selection radio button. You might want to post a screen shot of what that radio button looks like.

    but, if you want multiple choices for the one RadioButtonList?

    Then you need to fill out the list of choices (very much like a combo box, or listbox). Then you have a list of selections, and selecting one will result in a value for the selection.

    Say you have this simple markup:

    192048-image.png

    And the button code to fill this out, could be this:

       Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click  
    
            rbRating.DataSource = MyRst("SELECT ID,Rating FROM tblRating ORDER BY ID")  
            rbRating.DataBind()  
    
        End Sub  
    
        Function MyRst(strSQL As String) As DataTable  
    
            Dim rstData As New DataTable  
            Using conn As New SqlConnection(My.Settings.TEST4)  
                Using cmdSQL As New SqlCommand(strSQL, conn)  
                    conn.Open()  
                    rstData.Load(cmdSQL.ExecuteReader)  
                End Using  
            End Using  
    
            Return rstData  
    
        End Function  
    

    And now we have this:

    192049-image.png

    So, in above case, if I select a value, then the button will take on the "ID" from the data table.

    But, lets move the above INSIDE of a repeater.

    So, we have say a few hotels, and the above will allow us to rate the Hotel.

    Note that we have to do TWO things:

    Fill the RB list with the data of choices.
    THEN set the current selection to the current data row in the repeater.

    You can certainly use a RadioButton list, and drive it from the database.

    So, say this markup:

    192091-image.png

    And code to fill?

    We have both the data source for the database RB list choices, and then on repeater bind, we ALSO set the current choice.

    So, our code looks like this:

        Dim rstRating As New DataTable  
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load  
    
            If Not IsPostBack Then  
                LoadData()  
            End If  
    
        End Sub  
    
    
        Sub LoadData()  
    
            rstRating = MyRst("SELECT ID, Rating FROM tblRating ORDER BY ID")  
            Repeater1.DataSource = MyRst("SELECT * from tblHotelsA ORDER BY HotelName")  
            Repeater1.DataBind()  
    
        End Sub  
    
        Protected Sub Repeater1_ItemDataBound(sender As Object, e As RepeaterItemEventArgs) Handles Repeater1.ItemDataBound  
    
            If e.Item.ItemType = ListItemType.Item Or  
                e.Item.ItemType = ListItemType.AlternatingItem Then  
    
                Dim OneRow As DataRow = e.Item.DataItem.Row    ' get current bind data row   
                Dim rbList As RadioButtonList = e.Item.FindControl("rbRating")  
                rbList.DataSource = rstRating  
                rbList.DataBind()  
                rbList.SelectedValue = OneRow("Rating")    ' SET EACH repeater row select value  
            End If  
    
        End Sub  
    
        Function MyRst(strSQL As String) As DataTable  
    
            Dim rstData As New DataTable  
            Using conn As New SqlConnection(My.Settings.TEST4)  
                Using cmdSQL As New SqlCommand(strSQL, conn)  
                    conn.Open()  
                    rstData.Load(cmdSQL.ExecuteReader)  
                End Using  
            End Using  
    
            Return rstData  
    
        End Function  
    

    and now we have this output:

    192028-image.png

    Now, I have two example buttons, one in the repeater, and one outside.

    I can show the selected RB value in the repeat with a simple button click event

    (the button in the repeater).

    so we do this:

    192083-image.png

    And now code behind for that one button in the repeater looks like this:

    That code will look like this:

        Protected Sub cmdRowSel_Click(sender As Object, e As EventArgs)  
    
            Dim btn As Button = sender  
            Dim MyRow As RepeaterItem = btn.NamingContainer  
    
            Dim rbChoice As RadioButtonList = MyRow.FindControl("rbRating")  
    
            Debug.Print("rb choice (value) = " & rbChoice.SelectedItem.Value)  
            Debug.Print("rb choice (Text) = " & rbChoice.SelectedItem.Text)  
    
    
        End Sub  
    
    So, output looks like:  
    
    rb choice (value) = 3  
    rb choice (Text) = Good  
    

    Or, I could have a button outside of the repeater (like you have) and show/process all values of the repeater.

    That will look like this (the button outside of repeater).

        Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click  
    
            For Each MyRow As RepeaterItem In Repeater1.Items  
    
                Dim rbChoice As RadioButtonList = MyRow.FindControl("rbRating")  
    
                Debug.Write("rb choice (value) = " & rbChoice.SelectedItem.Value)  
                Debug.Print("---> rb choice (Text) = " & rbChoice.SelectedItem.Text)  
    
            Next  
    
        End Sub  
    

    Output:

    191998-image.png

    So, it is not at all clear what that JavaScript event code you have is doing, but as noted, if you have multiple selections in the RB item, then quite sure you need to use a radio button list. They are nice, since they automatic only allow one choice - and you don't need any code for the single selecton from the "many" choices of the RB.

    Regards,
    Albert D. Kallal (Access MVP 2003-2017)
    Edmonton, Alberta Canada

    0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Diksha dudi 126 Reputation points
    2022-04-12T06:08:56.597+00:00

    Yes i m getting 3 msgbox all false. How can we get the value of selected radiobutton


  2. Diksha dudi 126 Reputation points
    2022-04-12T05:30:34.107+00:00

    This is vb. Net code
    Imports System.Data
    Imports System.Data.SqlClient

    Partial Class select_address
        Inherits System.Web.UI.Page
    
        Dim user_id As String
    
    
        Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    
            Me.user_name()
            Me.user_address()
    
        End Sub
    
    
        Private Sub user_name()
            If Not Session("Uname") Is Nothing Then
    
                Dim email As String
                email = Session("Uname")
    
                Dim strcon As String = ConfigurationManager.ConnectionStrings("scon").ConnectionString
                Dim con As New SqlConnection(strcon)
    
                con.Open()
                Dim query As String
                query = "select id from users where email='" & email & "' "
    
    
                Dim Command As New SqlCommand(query, con)
                Dim sqlReader As SqlDataReader = Command.ExecuteReader()
                While sqlReader.Read
                    user_id = sqlReader("id").ToString
                End While
                con.Close()
    
                con.Open()
    
                Using cmd As New SqlCommand("SELECT * from users where id='" & user_id & "'", con)
                    Using sda1 As New SqlDataAdapter(cmd)
                        Dim dt1 As New DataTable()
    
    
                        sda1.Fill(dt1)
                        user.DataSource = dt1
                        user.DataBind()
                    End Using
                End Using
    
                con.Close()
            Else
                Response.Redirect("login_signup/login.aspx")
            End If
    
        End Sub
    
        Private Sub user_address()
    
            Dim constr As String = ConfigurationManager.ConnectionStrings("scon").ConnectionString
            Using con As New SqlConnection(constr)
    
    
                Using cmd As New SqlCommand("SELECT * FROM address where user_id='" + user_id + "'", con)
                    Using sda1 As New SqlDataAdapter(cmd)
                        Dim dt1 As New DataTable()
                        sda1.Fill(dt1)
                        address.DataSource = dt1
                        address.DataBind()
                    End Using
                End Using
            End Using
    
        End Sub
    
        Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
    
            Dim count As Integer
            count = 0
    
            For Each item As RepeaterItem In address.Items
                count += 1
    
                If item.ItemType = ListItemType.AlternatingItem OrElse item.ItemType = ListItemType.Item Then
    
                    Dim rdb As RadioButton = CType(item.FindControl("RadioButton1"), RadioButton)
    
                    If (rdb.Checked) Then
                        MsgBox(rdb.Text)
                    Else
                        MsgBox("not selected")
                    End If
    
                End If
    
            Next
            MsgBox(count)
        End Sub
    End Class
    
    0 comments No comments

  3. Diksha dudi 126 Reputation points
    2022-04-12T05:34:00.89+00:00

    192107-screenshot-30.png192089-screenshot-31.png192182-img20220412104227.jpg


  4. Diksha dudi 126 Reputation points
    2022-04-12T06:04:01.043+00:00

    This is my aspx code

    192156-screenshot-32.png192138-screenshot-33.png192183-screenshot-34.png192157-screenshot-35.png192191-screenshot-36.png

    0 comments No comments