简体   繁体   中英

asp.net: How to manually (c#) select radiobutton in radiobuttonlist inside gridview from Session

Im having problems after postback in asp.net / c#. All radiobuttlists in gridview are cleared on postback (one radiobuttonlist on each row). So I save them to a Session variable.

But I cant set them back from the Session variable. Here is the code in page_load:

//.. testing for null,etc
for (int i = 0; i < lstRadioButtons.Count; i++)
{
    RadioButtonList rbl = (RadioButtonList)gwTract.Rows[i].FindControl("RadioButtonList1");
    rbl.SelectedItem.Value = lstRadioButtons[i]; //list with strings "0", etc
    Debug.WriteLine("yep...");
}

Thanks in advance!

Assuming lstRadioButtons contains the value for a given row:

for (int itemIndex = 0; itemIndex < rbl.Items.Count; itemIndex++)
    if (rbl.Items[i].Value == lstRadioButtons[i])
        rbl.SelectedIndex = itemIndex;

Assuming lstRadioButtons contains the index for a given row:

rbl.SelectedIndex = int.Parse(i); //You may want to use TryParse to handle failure

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM