Elton da Costa
Elton da Costa

Reputation: 1277

RadioButtonList null values

have a radiobutton list which I am filling with Strings and would like to know how to get in a given time the value of the selected element and throw it into a String for example. but with the command SelectedValue and SelectedItem only have null values​​.

This radio button list is filled several times during the execution of the same page.

//Where do you fill the RadioButtonList
public void MostraImagensCarrefadas()
{
    List<String> files = new manFile().getListFilesForDirectory(this, MAE.DIRETORIO_TEMP_IMG);

    rbImagemPrincipal.Items.Clear();

    if (files != null)
    {
        foreach (String item in files)
        {
            rbImagemPrincipal.Items.Add(new ListItem(item));
        }
    }
}


//As it is in aspx
<div>
<asp:RadioButtonList ID="rbImagemPrincipal" runat="server" RepeatDirection="Vertical" AutoPostBack="false" OnSelectedIndexChanged="rbImagemPrincipal_SelectedIndexChanged"></asp:RadioButtonList>

 //where only encounter null values ​​when trying to get the selected item (clicked)
 //Nothing I do is the value obtained direferente null.
if (rbImagemPrincipal.SelectedItem != null)
                {
                    if (rbImagemPrincipal.SelectedItem.ToString() == str)
                    {
                        imagem.imagemPrincipal = "SIM";

                    }
                }

Upvotes: 0

Views: 1719

Answers (2)

pooja
pooja

Reputation: 21

It seems that you're populating the RadioButtonList on the page load - If so - make sure you surround your population of the RadioButtonList with an If/Then/Postback block: if not Page.IsPostBack then ' populate your RBL end if

eg:

        if (!IsPostBack)
        {
            loadradiobuttonlist();
        }

Upvotes: 1

Na Na
Na Na

Reputation: 838

First of all, this is the page let you know the value, not the application is getting it.

So, you need a ScriptManager and Timer, both are Ajax extensions. Add them to the page.

protected void Page_Load(object sender, EventArgs e)
{
    Timer1.Interval = 2000; // set your interval
}
protected void Timer1_Tick(object sender, EventArgs e)
{
     int result =  RadioButtonList1.SelectedIndex;
}

result is your radiobuttonlist selection index. Use it to select item from list.

Upvotes: 0

Related Questions