在gridview中的页面索引更改事件中保存单选按钮状态 [英] save Radio button state within page index change event in gridview

查看:106
本文介绍了在gridview中的页面索引更改事件中保存单选按钮状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

专家们,我正在大学项目中,在gridview中,我有10个问题,每个问题都有四个选项,以单选按钮的形式答复.
我的要求是在页面索引更改事件期间,我需要保持所选单选按钮的状态.请为此指导我.

Hi experts , I am working on university project, in the gridview i have 10 questions and each having four options as answers in the form of radio button.
my requirement is during the page index change event i need to maintain state of selected radio buttons. please guide me for this.

推荐答案

int i=0;
private void GetSelectedRecord(object sender, EventArgs e)
    {
        i=0;
        if (gv1.PageIndex == 0)
        {
            if (Session["storesession2"] == null)
            {
                GridBind();
            }
            else
            {
                ArrayList page1 = (ArrayList)Session["storesession2"];
                gv1.PageIndex = 0;
                
                GridBind();
                foreach (GridViewRow grow in gv1.Rows)
                {
                    string text = page1[i].ToString();
                    RadioButton rd1 = (RadioButton)grow.FindControl("rbtn1");
                    RadioButton rd2 = (RadioButton)grow.FindControl("rbtn2");
                    RadioButton rd3 = (RadioButton)grow.FindControl("rbtn3");
                    RadioButton rd4 = (RadioButton)grow.FindControl("rbtn4");
                    if (rd1.Text == text)
                    {
                        rd1.Checked = true;
                    }
                    if (rd2.Text == text)
                    {
                        rd2.Checked = true;
                    }
                    if (rd3.Text == text)
                    {
                        rd3.Checked = true;
                    }
                    if (rd4.Text == text)
                    {
                        rd4.Checked = true;
                    }

                    i++;
                }
            }
            
        }
        else if (gv1.PageIndex == 1)
        {
            if (Session["storesession1"] == null)
            {
                GridBind();
            }
            else
            {
                ArrayList page2 = (ArrayList)Session["storesession1"];
                gv1.PageIndex = 0;
                foreach (GridViewRow grow in gv1.Rows)
                {
                    string text = page2[i].ToString();
                    RadioButton rd1 = (RadioButton)grow.FindControl("rbtn1");
                    RadioButton rd2 = (RadioButton)grow.FindControl("rbtn2");
                    RadioButton rd3 = (RadioButton)grow.FindControl("rbtn3");
                    RadioButton rd4 = (RadioButton)grow.FindControl("rbtn4");
                    if (rd1.Text == text)
                    {
                        rd1.Checked = true;
                    }
                    if (rd2.Text == text)
                    {
                        rd2.Checked = true;
                    }
                    if (rd3.Text == text)
                    {
                        rd3.Checked = true;
                    }
                    if (rd4.Text == text)
                    {
                        rd4.Checked = true;
                    }

                    i++;
                }

            }
        }
    }

    protected void SetSelectedRecord(object sender, EventArgs e)
    {

        //int Last_Page = gv1.PageCount;
        //int Current_Page = gv1.PageIndex;


        foreach (GridViewRow grow in gv1.Rows)
        {

            RadioButton rd1 = (RadioButton)grow.FindControl("rbtn1");
            RadioButton rd2 = (RadioButton)grow.FindControl("rbtn2");
            RadioButton rd3 = (RadioButton)grow.FindControl("rbtn3");
            RadioButton rd4 = (RadioButton)grow.FindControl("rbtn4");
            
            if (rd1.Checked == true)
            {
                if (gv1.PageIndex == 0)
                {
                    arrpage1.Add(rd1.Text);
                }
                else
                {
                    arrpage2.Add(rd1.Text);
                }

            }
            if (rd2.Checked == true)
            {


                if (gv1.PageIndex == 0)
                {
                    arrpage1.Add(rd2.Text);
                }
                else
                {
                    arrpage2.Add(rd2.Text);
                }

            }
            if (rd3.Checked == true)
            {


                if (gv1.PageIndex == 0)
                {
                    arrpage1.Add(rd3.Text);
                }
                else
                {
                    arrpage2.Add(rd3.Text);
                }

            }
            if (rd4.Checked == true)
            {

                if (gv1.PageIndex == 0)
                {
                    arrpage1.Add(rd4.Text);
                }
                else
                {
                    arrpage2.Add(rd4.Text);
                }

            }
            if (gv1.PageIndex == 0)
            {
                Session["storesession1"] = arrpage1;
            }
            if (gv1.PageIndex == 1)
            {
                Session["storesession2"] = arrpage2;
            }

            i++;

        }

    }


您可以在Cookie和会话的帮助下对其进行维护...

ASP.NET中的Cookie [ ASP.NET Cookie入门指南 [ ASP.NET会话管理内部工具 [ http://asp.net-tutorials.com/state/sessions/ [
you can maintain it with the help of cookies and session...

Cookies in ASP.NET[^]

Beginner''s Guide to ASP.NET Cookies[^]


ASP.NET Session Management Internals[^]

http://asp.net-tutorials.com/state/sessions/[^]


索引将所有单选按钮状态存储在数组中,然后从数组存储到会话,然后每当加载相同页面时,将所有控件状态从会话加载到数组,并从数组加载所有控件状态以设置单个控件状态.
when you change the page index store all the radio button state in an array,then from array to session, then whenever you load the same page back load all the control state back from session to array and from array to set the individual control state.


这篇关于在gridview中的页面索引更改事件中保存单选按钮状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆