2012-09-10 2 views
0

제목에 쓴 것처럼 코드 숨김은 RadioButton의 선택을 변경하려고 할 때 잘못된 값을 가져옵니다.코드 숨김이 잘못된 RadioButton 값을 가져옴

Dilemma.aspx :

<asp:RadioButtonList ID="rbList" runat="server"> 
    <asp:ListItem Text="Yes, please." /> 
    <asp:ListItem Text="No, thanks." /> 
    <asp:ListItem Text="Ummm... maybe." /> 
</asp:RadioButtonList> 
<asp:Button ID=""btnChoose" runat="server" text="OK" OnClick="btnChoose_Click" /> 

Dilemma.aspx.cs 대신 새로 선택의 RadioButton을 얻기의

protected void Page_Load(object sender, EventArgs e) 
{ 
    int initialIndex = GetInitialIndex(); // Simplified for sake of question. 
    rbList.SelectedIndex = initialIndex; // This works. 
} 

protected void btnChoose_Click(object sender, EventArgs e) 
{ 
    int selection = rbList.SelectedIndex; // This gets it wrong! 
} 

는 코드 숨김 여전히 선택 믿고 index는 초기 색인입니다.

왜?

답변

0
protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!Page.IsPostBack) 
    { 
     int initialIndex = GetInitialIndex(); // Simplified for sake of question. 
     rbList.SelectedIndex = initialIndex; // This works. 
    } 
} 

누군가가이 답변을 게시했지만 어떤 이유로 삭제했습니다.
누가 내 앞에이 답변을 게시했는지 누구든지 - 작동합니다!

관련 문제