|
-
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.
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
|