2011-06-20 29 views
0

你好所选RadioButton在构造函数,确定在C#

我有这样的构造:

public EmployeeCategorizationControl() 
     { 


     } 

和许多单选按钮:

<asp:RadioButtonList ID="selectedYesNoQuestionBlock1" runat="server" RepeatDirection="Horizontal" 
       OnSelectedIndexChanged="Question1GotAnswered" AutoPostBack="true"> 
       <asp:ListItem Text="Yes" Value="1"></asp:ListItem> 
       <asp:ListItem Text="No" Value="0"></asp:ListItem> 
      </asp:RadioButtonList> 

<asp:RadioButtonList ID="selectedYesNoQuestionBlock2" runat="server" RepeatDirection="Horizontal" 
       AutoPostBack="true" OnSelectedIndexChanged="Question2GotAnswered"> 
       <asp:ListItem Text="Yes" Value="1"></asp:ListItem> 
       <asp:ListItem Text="No" Value="0"></asp:ListItem> 
      </asp:RadioButtonList> 

在我的构造函数,我怎么能确定哪个无线电按钮被选中?

在此先感谢!

+0

退房[这个问题](http://stackoverflow.com/questions/378620/getting-selected-value-from-radiobuttonlist),它可能是一个重复的。你会想在你的构造函数中做类似的事情。希望这可以帮助。 – lhan

回答

2

随着asp.net,与构造函数中的控件交互不是一个好主意,因为page life cycle的工作方式。您可能希望浏览页面生命周期msdn页面,并考虑使用LoadInit事件。

0

您不能:Request不可用,直到构建页面实例。你必须在the page lifecycle的稍后时间完成。

Load之前(初始化例如在)你只能通过请求访问的选择:

protected sub Page_Init(object sender, EventArgs args) { 
    var selection = Request.Form["selectedYesNoQuestionBlock1"]; 
} 

Load请求值映射到你的控制对象 - 从这一点上就可以通过直接访问值对照:

protected sub Page_Load(object sender, EventArgs args) { 
    var selection = selectedYesNoQuestionBlock1.SelectedValue; 
}