Link to home
Start Free TrialLog in
Avatar of Netlink2
Netlink2

asked on

how to set FormView RadioButtonList via code behind

Hi, how do I set a formview radiobutton to item 1 or item 2 via vb code-behind ?.

i.e. AdFormView.FindControl("radioButtons")
Avatar of RedKelvin
RedKelvin

Hi, if you place a radiobuttonlist on your form like this

<asp:RadioButtonList id="RadioButtonList1" runat="server" />

You can then manipulate it directly using the codebehind, this will add 2 radio buttons to the list

    Me.RadioButtonList1.Items.Add("MyItem1")
    Me.RadioButtonList1.Items.Add("MyItem2")


This will change the display text and set the value of the first item

    Me.RadioButtonList1.Items(0).Text = "MyItem3"
    Me.RadioButtonList1.Items(0).Value = "MyValue3"

The .items is an array, so the element must exist to modify it.

RedK
Avatar of Netlink2

ASKER

I tried your suggestion, I get the error, radiobutton is not a member of aspx.
Also, how do I select which item is active, i.e. how to set a radio button as the current defualt ?.
I guess that this is because it's part of a formview. It's inside of the following code:

<asp:FormView ID="AdFormView" runat="server" DataSourceID="AdDataSource" DefaultMode="Edit"
DataKeyNames="Id" OnItemUpdating="AdFormView_ItemUpdating">

The code is:

<asp:RadioButtonList  runat="server" ID="radioGarage">
      <asp:ListItem Value="1" >Yes</asp:ListItem>
      <asp:ListItem Value="2" Selected="True">No</asp:ListItem>
</asp:RadioButtonList>
you probably got that error if you pasted the code in the markup page, try dropping the radiobuttonlist control on the form from the toolbox, that way it will be added to the .designer page.

To set a list item as selected use the following, this will set the first item as selected

    Me.RadioButtonList1.Items(0).Selected = True

To enable/disable an item you can use

    Me.RadioButtonList1.Items(0).Enabled = True
I tried using Me.RadioButtonList1 etc, but I get the error, radiobutton is not a member of aspx. I think that this is because it's a part of an asp:FormView.
can you use
me.forviewName.RadioButtonList1

Take a peek at this too
http://www.thescripts.com/forum/thread506624.html
Sorry RedKelvin, that link took me nowhere.
ASKER CERTIFIED SOLUTION
Avatar of RedKelvin
RedKelvin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I gave up on trying, I just changed the radio button to a checkbox.