简体   繁体   中英

Radiobuttonlist selected value

Could someone help me with this solution, stuck on it.

I have a list and filling radiobuttonlist with data, but I can't get selected value when i press submit button.

List<CustMobilePhonesEntity> cusMobile = GetCusMobile(Email);


 RadioButtonList1.Items.Add(customerMobile[0].PhoneNumber);

                RadioButtonList1.DataSource = customerMobile;
                RadioButtonList1.DataTextField = "PhoneNumber";
                RadioButtonList1.DataValueField = "PhoneNumber";
                RadioButtonList1.DataBind();


Label1.Text = RadioButtonList1.SelectedValue;

Any ideas what I'm doing wrong, thank you.

First you need to check you are doing it in this fashion (If not the list is again binded with the DataSource when you click submit and the selection is lost)

if(! IsPostBack)
{
   RadioButtonList1.DataSource = customerMobile;
   RadioButtonList1.DataTextField = "PhoneNumber";
   RadioButtonList1.DataValueField = "PhoneNumber";
   RadioButtonList1.DataBind();
}

Also since you are binding RadioButtonList1.Items.Add(customerMobile[0].PhoneNumber); this won't be required (not clear if anything else).

Also see that ViewState is enabled

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