RadioButtonList OnSelectedIndexChanged
Asked Answered
L

2

12

I'm looking for the best way to handle a change of index being selected on a ASP.net RadioButtonList (C# code behind). I have 3 list items. For the first one, I want it to show a hidden asp:textbox on the page, whereas the other 2 will hide the textbox.

//asp.net side
<asp:RadioButtonList ID="_indicatorAckType" runat="server" RepeatDirection="Horizontal"
                enabled="true" OnSelectedIndexChanged="onAckTypeChanged">
    <asp:ListItem Text="None" />
    <asp:ListItem Text="SHOW" />   
    <asp:ListItem Text="HIDE" />
</asp:RadioButtonList>

//code behind
protected void onAckTypeChanged(object sender, EventArgs e)
{
    if (_indicatorAckType.SelectedItem.Text == "SHOW")
        _myTextboxID.Visible = true;
    else
        _myTextboxID.Visible = false;
}

I initially tried using onclick event handlers, but I've been told that ListItem's cannot use onclick events with radio button items. What am I doing wrong here? This does not throw any errors or have any visibly obvious problems. I have tried making onSelectedIndexChanged do nothing except show the textbox and that doesn't work either.

Any help is appreciated! Thanks everyone.

Lithograph answered 8/8, 2011 at 14:41 Comment(0)
C
37

On the RadioButtonList, set the AutoPostBack attribute to true.

Counterstamp answered 8/8, 2011 at 16:30 Comment(3)
Dude. Solid! Great question and concise answer. THANKS.Pettifogging
Good for simple forms, but forms with various other controls, AutoPostBack will also refire their rendering routines if any and cause other issues. It's not always as simple as AutoPostBack!Zoometry
thnk u james .u saved my lot of timeArgali
P
1

Have a look at this it might help. And I suggest to turn off autopostback if enabled on radio button do it all on client side using jquery.

example:

Using jQuery, hide a textbox if a radio button is selected

Philbrook answered 8/8, 2011 at 14:53 Comment(1)
I've tried using jQuery in some javascript to show/hide the box, but the javascript doesn't seem to be handling the code properly. Could you edit in a sample please?Lithograph

© 2022 - 2024 — McMap. All rights reserved.