Asp.net如何在使用datapager时维护单选按钮选择

时间:2018-02-28 13:15:02

标签: c# asp.net radio-button datapager

我正在ASP.net(c#)中创建一个测验应用程序,我目前有一个问题,一次显示使用数据采集器。然而,当我从问题1移到问题2然后回到问题1时,无线电按钮选择消失了。

编辑 - 我现在让单选按钮保持选择,每当我从下一个移动到前一个,但是,如果我选择radiobutton1然后继续问题2,它已经选择了radiobutton1,即使我还没有回答题。因此,出于某种原因,我在问题1上做出的任何选择都与其他问题重复。

Question1 example Question2 example

 <body>
        <form id="form1" runat="server">
            <div>
                <asp:ListView ID="lvCustomers" runat="server" GroupPlaceholderID="groupPlaceHolder1"
                    ItemPlaceholderID="itemPlaceHolder1" OnPagePropertiesChanging="OnPagePropertiesChanging" OnPreRender="ListPager_PreRender">
    
                    <LayoutTemplate>
                        <div id="itemPlaceHolder1" runat="server">
                        </div>
                        <asp:DataPager ID="DataPager1" runat="server" PagedControlID="lvCustomers" PageSize="1">
                            <Fields>
                                <asp:NextPreviousPagerField runat="server" ButtonType="Link"
                                    ShowFirstPageButton="false"
                                    ShowPreviousPageButton="true"
                                    ShowNextPageButton="false" />
                                <asp:NumericPagerField ButtonType="Link" />
                                <asp:NextPreviousPagerField ButtonType="Link"
                                    ShowNextPageButton="true"
                                    ShowLastPageButton="false"
                                    ShowPreviousPageButton="false" />
                            </Fields>
                        </asp:DataPager>
                    </LayoutTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%#Eval("QuestionID")%>'></asp:Label><br />
                        <asp:Label ID="Label1" runat="server" Text='<%#Eval("QuestionText")%>'></asp:Label><br />
                        <li>
                            <asp:RadioButton ID="Radio1" Text='<%#Eval("Answer1") %>' GroupName="radiobtns" EnableViewState="true" runat="server" />
                        </li>
                        <li>
                            <asp:RadioButton ID="Radio2" runat="server" GroupName="radiobtns" EnableViewState="true" Text='<%#Eval("Answer2") %>' />
                        </li>
                        <li>
                            <asp:RadioButton ID="Radio3" runat="server" GroupName="radiobtns" EnableViewState="true" Text='<%#Eval("Answer3") %>'  />
                        </li>
                        <li>
                            <asp:RadioButton ID="Radio4" runat="server" GroupName="radiobtns" EnableViewState="true" Text='<%#Eval("Answer4") %>'   />
                        </li>
                        <br />
                    </ItemTemplate>
                </asp:ListView>
            </div>
            <p>
                <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
            </p>
        </form>
    </body>
    </html>

 namespace WebApplication2.WebForms
    {
        public partial class _1QuestionQuiz : System.Web.UI.Page
        {
            string userAns;
            int QuestionID;

            protected void Page_Load(object sender, EventArgs e)
            {
                if (!Page.IsPostBack)
                {
                    BindListView();

                    if (Session["QuizID"] != null)
                    {
                        int QuizID = Convert.ToInt32(Session["QuizID"]);
                    }
                }
            }



            protected void ListPager_PreRender(object sender, EventArgs e)
            {
                if (Session["DTSource"] != null)
                {
                    lvCustomers.DataSource = Session["DTSource"];
                    lvCustomers.DataBind();
                }

                GetSelections();
            }


            private void BindListView()
            {
                SqlConnection conn = new SqlConnection();
                string connString = ConfigurationManager.ConnectionStrings["test1ConnectionString"].ConnectionString;
                SqlCommand Cmd = new SqlCommand();
                conn.ConnectionString = connString;

                conn.Open();

                if (lvCustomers != null)
                {
                    foreach (ListViewItem item in lvCustomers.Items)
                    {
                        Label lbl = (Label)item.FindControl("Label2");

                        if (lbl != null)
                        {
                            QuestionID = Convert.ToInt32(lbl.Text);
                            ViewState["qstion"] = QuestionID;
                        }

                        RadioButton rd1 = (RadioButton)item.FindControl("Radio1");
                        RadioButton rd2 = (RadioButton)item.FindControl("Radio2");
                        RadioButton rd3 = (RadioButton)item.FindControl("Radio3");
                        RadioButton rd4 = (RadioButton)item.FindControl("Radio4");

                        if (rd1.Checked)
                        {
                            userAns = rd1.Text;
                            ViewState["Radio1"] = rd1.Text;
                        }

                        else if (rd2.Checked)
                        {
                            userAns = rd2.Text;
                            ViewState["Radio2"] = rd2.Text;
                        }

                        else if (rd3.Checked)
                        {
                            userAns = rd3.Text;
                            ViewState["Radio3"] = rd3.Text;
                        }

                        else if (rd4.Checked)
                        {
                            userAns = rd4.Text;
                            ViewState["Radio4"] = rd4.Text;

                        }

                        SqlCommand comm = new SqlCommand("InsertSelections", conn);
                        comm.CommandType = CommandType.StoredProcedure;
                        SqlDataAdapter adapter = new SqlDataAdapter(comm);
                        SqlParameter p1 = new SqlParameter("Answer", userAns);
                        SqlParameter p2 = new SqlParameter("QuestionID", QuestionID);
                        SqlParameter p3 = new SqlParameter("QuizID", (Session["QuizID"]));
                        comm.Parameters.Add(p1);
                        comm.Parameters.Add(p2);
                        comm.Parameters.Add(p3);

                        comm.ExecuteNonQuery();


                    }
                }
            }



     private void GetSelections()
        {
            foreach (ListViewItem item in lvCustomers.Items)
            {
                Label lbl = (Label)item.FindControl("Label2");

                if (lbl != null)
                {
                    QuestionID = Convert.ToInt32(lbl.Text);
                }

                RadioButton rd1 = (RadioButton)item.FindControl("Radio1");
                RadioButton rd2 = (RadioButton)item.FindControl("Radio2");
                RadioButton rd3 = (RadioButton)item.FindControl("Radio3");
                RadioButton rd4 = (RadioButton)item.FindControl("Radio4");

                        //Try radiobutton 1 as a tester
            if (rd1 != null)
            {
                if (lbl != null && ViewState["Radio1"] != null)
                {
                    rd1.Checked = true;
                }
            }
             if (rd2 != null)
            {
                if (lbl != null && ViewState["Radio2"] != null)
                {
                    rd2.Checked = true;

                }

            }
             if (rd3 != null)
            {
                if (lbl != null && ViewState["Radio3"] != null)
                {
                    rd3.Checked = true;

                }

            }
             if (rd4 != null)
            {
                if (lbl != null && ViewState["Radio4"] != null)
                {
                    rd4.Checked = true;
                    break;
                }

               }     
            }
        }

            protected void Button1_Click(object sender, EventArgs e)
            {
                Response.Redirect("Score.aspx", false);
            }


            protected void OnPagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
            {
                (lvCustomers.FindControl("DataPager1") as DataPager).SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
                BindListView();

            }
    }
    }

1 个答案:

答案 0 :(得分:0)

尝试重写LoadViewState和SaveViewState以维护单选按钮的状态。例如,尝试类似下面的操作,并在需要的地方将值拉回ViewState:

protected override void LoadViewState(object savedState)
{
    base.LoadViewState(savedState);

    if (ViewState["Radio1"] != null)
        Radio1.Text = (int)ViewState["Radio1"];
    ...
}

protected override object SaveViewState()
{
    ViewState["Radio1"] = Radio1.Text;
    ...

    return base.SaveViewState();
}