How to set text in RadioButtonList from code behind?

S

staeri

I have a RadioButtonList like this:

<asp:RadioButtonList ID="lstReportType" runat="server">
<asp:ListItem Selected="True"
Value="CompanyCheck">Text 1</asp:ListItem>
<asp:ListItem Value="CompanyStandard">Text 2</
asp:ListItem>
<asp:ListItem Value="CompanyExtended">Text 3</
asp:ListItem>
</asp:RadioButtonList>

I need to change the text of the ListItems from the code behind file.
How can that be done?

Very grateful for reponse!

// S
 
J

Jeff Winn

Depends on where you need them changed at, in the example I'm just changing
the text on the page's load event.

The Items property on the control contains the collection of items that are
being displayed to the user. If you must want to modify the text of an item,
grab it from the collection by the index you're wanting to change (keeping
in mind that it's a zero-based index) and set the text property.

protected void Page_Load(object sender, EventArgs e) {
this.lstReportType.Items[0].Text = "Blah";
}
 
S

staeri

Depends on where you need them changed at, in the example I'm just changing
the text on the page's load event.

The Items property on the control contains the collection of items that are
being displayed to the user. If you must want to modify the text of an item,
grab it from the collection by the index you're wanting to change (keeping
in mind that it's a zero-based index) and set the text property.

protected void Page_Load(object sender, EventArgs e) {
    this.lstReportType.Items[0].Text = "Blah";

}



I have a RadioButtonList like this:
<asp:RadioButtonList ID="lstReportType" runat="server">
               <asp:ListItem Selected="True"
Value="CompanyCheck">Text 1</asp:ListItem>
               <asp:ListItem Value="CompanyStandard">Text 2</
asp:ListItem>
               <asp:ListItem Value="CompanyExtended">Text 3</
asp:ListItem>
</asp:RadioButtonList>
I need to change the text of the ListItems from the code behind file.
How can that be done?
Very grateful for reponse!
// S- Dölj citerad text -

- Visa citerad text -

Thanks a lot! I'll try this.

// S
 
S

staeri

Depends on where you need them changed at, in the example I'm just changing
the text on the page's load event.
The Items property on the control contains the collection of items thatare
being displayed to the user. If you must want to modify the text of an item,
grab it from the collection by the index you're wanting to change (keeping
in mind that it's a zero-based index) and set the text property.
protected void Page_Load(object sender, EventArgs e) {
    this.lstReportType.Items[0].Text = "Blah";
- Visa citerad text -

Thanks a lot! I'll try this.

// S- Dölj citerad text -

- Visa citerad text -

I've tried your solution but nothing happens with the text. I'm using
vb and I've tried in Page_Load:

Me.lstReportType.Items(0).Text = "Blah"
lstReportType.Items(0).Text = "Blah"

What can be wrong?

// S
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top