2017-06-21 15 views
1

ラジオボタンの状態を保存し、ページをリロードするときにラジオボタンを再度選択します。私は次のコードを書いていますが、動作しません。ラジオボタンの状態を保存する

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

public partial class Question_1 : System.Web.UI.Page 
{ 
    public int index; 
    public bool flag = false; 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     if(flag) 
     { 
      index = (int)Session["index"]; 
      if (index == 5) 
      { 
       totallyagree.Checked = true; 
      } 
      else if (index == 4) 
      { 
       agree.Checked = true; 
      } 
     } 
    } 
    protected void next_Click(object sender, EventArgs e) 
    { 
     flag=true; 
     if (totallyagree.Checked) 
     { 
      Session["index"] = 5; 
     } 
     else if (agree.Checked) 
     { 
      Session["index"] = 4; 
     } 
     Response.Redirect("Question 2.aspx"); 
    } 
    protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     //Session["index"] = RadioButtonList1.SelectedIndex; 
    } 
} 

私はこの問題を手伝ってください。

答えて

0

あなたはIsPostBackを試しましたか?

protected void Page_Load(object sender, EventArgs e) 
{ 
    if(!IsPostBack) 
    { 
     index = (int)Session["index"]; 
     if (index == 5) 
     { 
      totallyagree.Checked = true; 
     } 
     else if (index == 4) 
     { 
      agree.Checked = true; 
     } 
    } 
} 
+0

IsPostBackを使用すると、Null参照例外が発生します。なぜフラグ変数を使用したのかという問題ですが、ラジオボタンの状態は保存されません。 –

関連する問題