question

Dikshadudi-8186 avatar image
0 Votes"
Dikshadudi-8186 asked Dikshadudi-8186 commented

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

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


dotnet-aspnet-webforms
screenshot-27.png (308.1 KiB)
screenshot-28.png (246.0 KiB)
screenshot-29.png (257.5 KiB)
· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Gee, that should work. On the other hand, a single radiobutton does not make a whole lot of sense, since when you click on it, and you only have one choice? You can't un-select it once you done that. Seems to be that should be a checkbox, and not a radio button.

However, you do have a JavaScript wired up, and it not clear what that code does. I would for a test, remove that on-click, and then see if if you click on the radiobutton, then your for each loop sees and gets the radiobutton.

So, far, a good guess is that the JavaScript routine is messing around and doing something here we can't see or know.

Remove the javascript OnClick markup, and then test this.

And if the RadioButton is to have more then one option, then you should be using a RadioButtonList.

0 Votes 0 ·

Actually java script is for single selection, without it, there are multiple selection.
I am using single Radiobutton cause i have to show the row wise data of the database

0 Votes 0 ·

I don't see how a single radiobutton needs some js code for a single seleciton - it is "always" single selection. But, see my code examples below - it might give you some ideas here. I think you need a RadioButtonList, and NOT a RB.

0 Votes 0 ·

Hi @Dikshadudi-8186 ,
According to your description, you always run the else condition, it means the radiobutton isn't checked. What's the relationship of the javascript onclick and the button click? Could you post us more codes?
Best regards,
Yijing Sun

0 Votes 0 ·
Show more comments
AlbertKallal-4360 avatar image
0 Votes"
AlbertKallal-4360 answered AlbertKallal-4360 edited

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






image.png (19.6 KiB)
image.png (3.3 KiB)
image.png (84.3 KiB)
image.png (49.3 KiB)
image.png (30.7 KiB)
image.png (5.6 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Dikshadudi-8186 avatar image
0 Votes"
Dikshadudi-8186 answered

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Dikshadudi-8186 avatar image
0 Votes"
Dikshadudi-8186 answered AlbertKallal-4360 commented

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



screenshot-31.png (227.9 KiB)
screenshot-30.png (211.1 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Ok, so when you run your above code, you going to see/get 3 msg box displayed. The first should be false, then true, then false.
So, in your case each repeater has a radio button.

So, to be clear, you suggesting that you do see/get 3 msg box when you run that for each loop, but all 3 show false? (and hence you issue/bug/question).

So, at this point, you don't need a RadioButton list, but a simple one Radiobutton per repeater item as you have does look fine and correct.

So, you get 3 msg, but all show false then?

0 Votes 0 ·
Dikshadudi-8186 avatar image
0 Votes"
Dikshadudi-8186 answered

This is my aspx code

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



screenshot-32.png (259.7 KiB)
screenshot-33.png (265.8 KiB)
screenshot-34.png (262.5 KiB)
screenshot-35.png (269.6 KiB)
screenshot-36.png (228.4 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Dikshadudi-8186 avatar image
0 Votes"
Dikshadudi-8186 answered Dikshadudi-8186 commented

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

· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Ok, so it all clear now. We get about 2-5 questions per week on this issue. The problem here is simple:

Remember, to build ANY, and I repeat ANY working web page?

You MUST always, but always always bind the controls ONE TIME on page load - but ONLY on the first page load!!!

Remember, when you click any button, cause any post-back?

Your page load event will fire EVERY TIME. That means we are re-loading, and re-binding our data each time - since page load fires EVERY time, and THEN your button event code runs!!! But, with a re-load of our data (and a re-bind), then we lose any and all sections we make!!!

So, change your page load code to this:

 Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    
          If Not IsPostBack Then
        
              Me.user_name()
              Me.user_address()
           
            End If
        
  End Sub

This above rule - (placing your data binding and loading of data in the If Not IsPostBack ???

You really can't build a working web page if you forget this rule - and of the last 200 web pages I built? EVERY ONE has the above if/then in it for loading my gridview, listview, repeaters etc.

So, what happens is all your code was just fine. But, you then click on the button, Page load triggers - re-loads your repeater and BLOWS OUT any changes, and then your button code runs.

A HUGE and really nice detail would be if you noted/told/pointed out that when you click on the radio button, it gets it gets un-selected on button click.
(msgbox should NOT be used here).



0 Votes 0 ·

Ok thank you so much.. It worked. ❤️

1 Vote 1 ·

yes, it was in front of our eyes all the time. The instant you posted the on-load event, I should have seen that!!!

This happens oh so often. You place say a dropdown list, a button. Then on page load we load up the dropdown list.

Then you select a dropdown value - hit a button, and the selection is gone!!! - and that's due to re-binding the dropdown list each time on page load.

So, even for me - I should have seen that!!

But, a great lesson and you did a great job by persisting, post more code. So, we had to play Sherlock homes here a bit, but we did get to the bottom.

All in all - team effort - well done!!!

Good luck!! - and I'm sure we will remember this "quirk" and even the caution notes. I mean, when you selected a radio button, and clicked submit - the fact that the radio button THEN de-selected was another tail tail sign. So, this was tricky - but it all makes sense now!

1 Vote 1 ·
Show more comments