Last post May 23, 2007 06:19 PM by mikepope
May 22, 2007 04:48 AM|mln7374|LINK
I'm trying to insert a default value ("ADM") to a Radiobuttonlist in a FormView (Insert Mode) but since I'm new at this I don't know how. Can anybody help, please!
May 22, 2007 05:46 PM|mikepope|LINK
The basic idea, if I understand correctly, is that you define static items in the RadioButtonList control and also data-bind it. You then set the AppendDataBoundItems property to true, and ASP.NET adds the data to the existing static item.
Is that what you're asking?
May 23, 2007 01:17 AM|mln7374|LINK
This is what I'm trying to do:
I've created a table called "lanTable". The table holds 3 fields; lanNumber (int), lanName (varchar) and
This table will only have 4-5 records and one of them will have the lanDefaultValue set to True and this is the record I want to be the default value in a RadioButtonList when the user creates a new record in a form that holds several DataSources.
This way the RadioButtonList will be preset with the most common value.
May 23, 2007 06:19 PM|mikepope|LINK
Ok, gotcha. The problem, as you've deduced, is that there is databinding that lets you preselect (ie, set Selected=true) for the items in a RadioButtonList control. Given that you have a very small list of data records to bind to, an option is to handle
the RadioButtonControl's DataBound event, and in that event, manually look up each item and set its Selected value to the corresponding bit/Boolean field.
Here's an example that uses a class I created for this purpose, but the same principle applies when you're dealing with data. (In fact, if you're dealing with real data, you probably have the advantage of having a real ID to work with rather than having
to go through all the records for each list item.)
<%@ Page Language="VB" %>
Public Class IanTable
Private _ianName As String
Private _ianNumber As Integer
Private _ianDefaultValue As Boolean
Public Property IanName() As String
Set(ByVal value As String)
_ianName = value
Public Property IanNumber() As Integer
Set(ByVal value As Integer)
_ianNumber = value
Public Property IanDefaultValue() As Boolean
Set(ByVal value As Boolean)
_ianDefaultValue = value
Public Sub New()
Public Sub New(ByVal name As String, ByVal num As Integer, ByVal def As Boolean)
Me.IanName = name
Me.IanNumber = num
Me.IanDefaultValue = def
Protected a As ArrayList
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Dim i1 As New IanTable("mike", 1, False)
Dim i2 As New IanTable("sally", 2, False)
Dim i3 As New IanTable("fred", 3, True) ' This one is the default (whichever is true)
Dim i4 As New IanTable("mary", 4, False)
a = New ArrayList()
Me.RadioButtonList1.DataSource = a
Me.RadioButtonList1.DataTextField = "IanName"
Protected Sub RadioButtonList1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs)
For Each li As ListItem In Me.RadioButtonList1.Items
' Compare item against data source
Dim it As IanTable
For i As Integer = 0 To a.Count - 1
it = CType(a(i), IanTable)
If li.Text = it.IanName Then
li.Selected = it.IanDefaultValue
<form runat="server" id="Form1">
<asp:RadioButtonList ID="RadioButtonList1" runat="server"