-
March 6th, 2010, 06:35 PM
#1
Generating Radio buttons from DB infomation.
I am trying to find the best way to dynamicly generate raido buttons from information in a DB. What I want to do is make a line that Has two buttons for each entry in the DB. Kinda like:
DB Line 1 <button1> <button2>
DB Line 2 <button1> <button2>
and so on.
I found a tutorial online at http://www.daniweb.com/forums/thread256420.html# which tells you how to use a placeholder and do it dynamicly but it kinda seemed a bit much. Does that turotial really give the best way or does anyone have suggestions on doing it better?
-
March 6th, 2010, 07:02 PM
#2
Re: Generating Radio buttons from DB infomation.
I would do it using a Repeater control. The advantage is that most of the work would be defined in the markup. You would put the radio button related markup in the item template. You also have flexibility over how it is rendererd. You can bind your the repeater to a suitable data source and press F5! Take a look at the documentation
All the best!
-
March 6th, 2010, 07:38 PM
#3
Re: Generating Radio buttons from DB infomation.
I was originaly trying to get it done in a repeater item but could not figure out how to make the ID for the each row of radio buttons different (so row one buttons was one group, row two group two, etc). Do you know how I would do that?
-
March 8th, 2010, 10:09 AM
#4
Re: Generating Radio buttons from DB infomation.
Well the grouping is independent of the ID. There is a property called GroupName which you could set differently for each row.
-
March 8th, 2010, 01:15 PM
#5
Re: Generating Radio buttons from DB infomation.
Originally Posted by nelo
Well the grouping is independent of the ID. There is a property called GroupName which you could set differently for each row.
Right but HOW?
-
March 9th, 2010, 05:05 AM
#6
Re: Generating Radio buttons from DB infomation.
a small example to give you some ideas...
Code:
Imports System.Data
Partial Class UploadAFile
Inherits System.Web.UI.Page
'on the page I have a
'<div runat="server" id="myRadios" ></div>
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'If Not Page.IsPostBack Then
'generate a dt for test purpouse
Dim dt As New DataTable("TestTable")
'with columns "Name", "Text", "AlternateText", "Checked"
Dim clName As New DataColumn("Name", GetType(String))
dt.Columns.Add(clName)
Dim clText As New DataColumn("Text", GetType(String))
dt.Columns.Add(clText)
Dim clAlternateText As New DataColumn("AlternateText", GetType(String))
dt.Columns.Add(clAlternateText)
Dim clChecked As New DataColumn("Checked", GetType(Boolean))
dt.Columns.Add(clChecked)
'Generate some rows of data
For counter As Integer = 1 To 5
Dim dr As DataRow = dt.NewRow()
dr("Name") = "ctl" & counter.ToString("00")
dr("Text") = "btn_A_" & counter.ToString("00")
dr("AlternateText") = "btn_B_" & counter.ToString("00")
dr("Checked") = False
dt.Rows.Add(dr)
Next
'generate radios
'on the page I have a
'<div runat="server" id="myRadios" ></div>
GenerateRadios(Me.myRadios, dt)
'generate the two buttons stuff
GenerateButtons(myRadios, dt)
'End If
End Sub
Private Sub GenerateRadios(ByVal container As Control, ByVal dt As DataTable)
'to add radiobuttons
'if you have a datatable
'that comes from a table
'with (at least) 3 fields
'1)for id of radio
'2)for text of radio
'3)for a checked/unchecked option
For Each dr As DataRow In dt.Rows
If Not String.IsNullOrEmpty(dr("Name").ToString) Then
Dim rd As New RadioButton()
rd.ID = dr("Name").ToString
rd.Text = dr("Text").ToString
rd.Checked = CBool(dr("Checked"))
rd.GroupName = "SameGruop"
container.Controls.Add(rd)
container.Controls.Add(New LiteralControl("<br />"))
End If
Next
End Sub
Private Sub GenerateButtons(ByVal container As Control, ByVal dt As DataTable)
'to add two buttons for each datatable row
For Each dr As DataRow In dt.Rows
If Not String.IsNullOrEmpty(dr("Name").ToString) Then
Dim btnOne As New Button()
Dim btnTwo As New Button()
btnOne.ID = dr("Name").ToString & "one"
btnTwo.ID = dr("Name").ToString & "two"
btnOne.Text = dr("Text").ToString
btnTwo.Text = dr("AlternateText").ToString
'do you need any serverside code to be executed
'on submit?
AddHandler btnOne.Click, AddressOf DynamicBtnOnClick
AddHandler btnTwo.Click, AddressOf DynamicBtnOnClick
container.Controls.Add(btnOne)
container.Controls.Add(btnTwo)
container.Controls.Add(New LiteralControl("<br />"))
End If
Next
End Sub
Protected Sub DynamicBtnOnClick(ByVal sender As Object, ByVal e As System.EventArgs)
'serverside onclick of buttons
'put a breakpoint here and look at btn.ID value...
Dim btn As Button = DirectCast(sender, Button)
If Not btn Is Nothing Then
Select Case btn.ID
'....
End Select
End If
End Sub
End Class
...at present time, using mainly Net 4.0, Vs 2010
Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
-
March 9th, 2010, 08:45 AM
#7
Re: Generating Radio buttons from DB infomation.
Thanks Cimperiali, your doing it all by code to and not even using a repeater. I had that example in the link I provided in the first post I guess that is the simplelest way to do it.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|