且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

设置单选按钮列表从codebehind选择

更新时间:2022-01-11 22:19:22

您可以这样做:

radio1.SelectedIndex = 1;

但是,这是最简单的形式和您的UI增长将最有可能成为问题。说,举例来说,如果一个团队成员的插入的在项目单选按钮列表上方选项2 但是不知道我们使用的幻数的在code-后面选择 - 现在的应用程序选择了错误的指数

But this is the most simple form and would most likely become problematic as your UI grows. Say, for instance, if a team member inserts an item in the RadioButtonList above option2 but doesn't know we use magic numbers in code-behind to select - now the app selects the wrong index!

也许你想看看使用FindControl为了确定列表项实际需要,通过名称,以及适当地选择。例如:

Maybe you want to look into using FindControl in order to determine the ListItem actually required, by name, and selecting appropriately. For instance:

//omitting possible null reference checks...
var wantedOption = radio1.FindControl("option2").Selected = true;