保存单选按钮状态

时间:2017-06-21 19:41:53

标签: c# asp.net

我想保存单选按钮状态,并在重新加载页面时再次选择单选按钮。我编写了以下代码,但它无法正常工作:

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;
    }
}

请帮我解决这个问题。

1 个答案:

答案 0 :(得分: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;
        }
    }
}