我如何保留asp.net中页面的值

时间:2015-05-11 07:05:48

标签: asp.net

我需要帮助将“保留”值从一个页面转到另一个示例我回答了第一页并进入第二页如果我点击“上一页”我怎样才能在那里保留我的答案。

以下是我的代码:

   protected void Button1_Click(object sender, EventArgs e)
    {

        //string sesrbl1;

        string selectedvalue = RadioButtonList2.SelectedValue.ToString();
        string selectedvalue2 = RadioButtonList4.SelectedValue.ToString();
        string selectedvalue3 = RadioButtonList6.SelectedValue.ToString();
        string selectedvalue4 = RadioButtonList8.SelectedValue.ToString();
        string selectedvalue5 = RadioButtonList14.SelectedValue.ToString();
        string selectedvalue6 = RadioButtonList11.SelectedValue.ToString();

        bool risk;
     SqlCommand cmd = new SqlCommand();
     SqlConnection sqlcon = new SqlConnection("Data Source=DBASE;Initial Catalog=TumorRegistry;User ID=sa");

    try
            {
                //Session["rb1"] = RadioButtonList12.SelectedValue;
                //Session["rb2"] = RadioButtonList3.SelectedValue;
                //Session["rb3"] = RadioButtonList5.SelectedValue;
                //Session["rb4"] = RadioButtonList7.SelectedValue;
                //Session["rb5"] = RadioButtonList13.SelectedValue;





                if (RadioButtonList12.SelectedItem.Text== "FALSE")
                {
                    if (bool.TryParse("False", out risk))
                    {
                        risk = false;


                        Session["rb1"] = risk;

                    }

                }
                if (RadioButtonList12.SelectedItem.Text == "TRUE")
                {
                    if (bool.TryParse(RadioButtonList12.SelectedItem.Text, out risk))
                    {
                        risk = true;
                        Session["rb1"] = risk;
                    }
                }
                if (RadioButtonList3.SelectedItem.Text == "FALSE")
                {
                    if (bool.TryParse(RadioButtonList3.SelectedItem.Text, out risk))
                    {
                        risk = false;
                         Session["rb2"] = risk;
                    }
                }
                if (RadioButtonList3.SelectedItem.Text== "TRUE")
                {
                    if (bool.TryParse(RadioButtonList3.SelectedItem.Text, out risk))
                    {
                        risk = true;
                        Session["rb2"] = risk;
                    }
                }
                if (RadioButtonList5.SelectedItem.Text == "FALSE")
                {
                    if (bool.TryParse(RadioButtonList5.SelectedItem.Text, out risk))
                    {
                        risk = false;
                         Session["rb3"] = risk;
                    }
                }
                if (RadioButtonList5.SelectedItem.Text == "TRUE")
                {
                    if (bool.TryParse(RadioButtonList5.SelectedItem.Text, out risk))
                    {
                        risk = true;
                         Session["rb3"] = risk;
                    }
                }
                if (RadioButtonList7.SelectedItem.Text == "FALSE")
                {
                    if (bool.TryParse(RadioButtonList7.SelectedItem.Text, out risk))
                    {
                        risk = false;
                         Session["rb4"] = risk;
                    }
                }
                if (RadioButtonList7.SelectedItem.Text == "TRUE")
                {
                    if (bool.TryParse(RadioButtonList7.SelectedItem.Text, out risk))
                    {
                        risk = true;
                          Session["rb4"] = risk;
                    }
                }
                if (RadioButtonList13.SelectedItem.Text == "FALSE")
                {
                    if (bool.TryParse(RadioButtonList13.SelectedItem.Text, out risk))
                    {
                        risk = false;
                         Session["rb5"] = risk;
                    }
                }
                if (RadioButtonList13.SelectedItem.Text == "TRUE")
                {
                    if (bool.TryParse(RadioButtonList13.SelectedItem.Text, out risk))
                    {
                        risk = true;
                         Session["rb5"] = risk;
                    }
                }



                if (selectedvalue.ToString() == "0")
                {

                    selectedvalue = null;
                }
                else if (selectedvalue2.ToString() == "0")
                {
                    selectedvalue2 = null;
                }
                else if (selectedvalue3.ToString() == "0")
                {
                    selectedvalue3 = null;
                }
                else if (selectedvalue4.ToString() == "0")
                {
                    selectedvalue4 = null;
                }
                else if (selectedvalue5.ToString() == "0")
                {
                    selectedvalue6 = null;
                }



                //Session["rb1"] = RadioButtonList12.SelectedValue;
                //Session["rb2"] = RadioButtonList3.SelectedValue;
                //Session["rb3"] = RadioButtonList5.SelectedValue;
                //Session["rb4"] = RadioButtonList7.SelectedValue;
                //Session["rb5"] = RadioButtonList13.SelectedValue;



        //sending values to the submit button next page 
                Session["Holdtextbox1"] = RadioButtonList2.SelectedValue.ToString();
                Session["Holdtextbox2"] = RadioButtonList4.SelectedValue.ToString();
                Session["Holdtextbox3"] = RadioButtonList6.SelectedValue.ToString();
                Session["Holdtextbox4"] = RadioButtonList8.SelectedValue.ToString();
                Session["Holdtextbox5"] = RadioButtonList14.SelectedValue.ToString();
                Session["Holdtextbox6"] = RadioButtonList11.SelectedValue.ToString();



                selectedvalue = Session["Holdtextbox1"].ToString();
                selectedvalue2 = Session["Holdtextbox2"].ToString();
                selectedvalue3 = Session["Holdtextbox3"].ToString();
                selectedvalue4 = Session["Holdtextbox4"].ToString();
                selectedvalue5 = Session["Holdtextbox5"].ToString();
                selectedvalue6 = Session["Holdtextbox6"].ToString();






                    Response.Redirect("WebForm1.aspx");



               } 

              catch (Exception ex)
            {
                Response.Write(ex.Message);
            } 

这是第一页

这里是第2页我有2个按钮按钮1和2

        protected void Button1_Click(object sender, EventArgs e)
    {
       Response.Redirect("Default.aspx");
        ////Button1.Attributes.Add("onClick", "javascript:history.back(); return false;");
        //   Response.Redirect("Default.aspx");
    } 



       public void SaveRecord()
        {
            try
            {
                Holdtextbox1.Text = Session["Holdtextbox1"].ToString();
                Holdtextbox2.Text = Session["Holdtextbox2"].ToString();
                Holdtextbox3.Text = Session["Holdtextbox3"].ToString();
                Holdtextbox4.Text = Session["Holdtextbox4"].ToString();
                Holdtextbox5.Text = Session["Holdtextbox5"].ToString();
                Holdtextbox6.Text = Session["Holdtextbox6"].ToString();
                sqlcon.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection = sqlcon;
                SqlDataAdapter DA = new SqlDataAdapter();

                // string SqlInsert = "Insert into tbTRcBase (HPN,ISH, Asthma, DM, OtherCo,HPNTreatment, ISHTreatment,AsthmaTreatment, DMTreatment, OtherCoTreatment,SecondHandSmoke,Smoker, StopSmoking , Occupation , CancerFamilyHistory, FamilyWithCancer,ParentWithCancer) values ('" + bitsess.Text + "','" + bitsess2.Text + "','" + bitsess3.Text + "','" + bitsess4.Text + "','" + bitsess5.Text + "','" + Holdtextbox1.Text + "','" + Holdtextbox2.Text + "','" + Holdtextbox3.Text + "','" + Holdtextbox4.Text + "','" + Holdtextbox5.Text + "','" + Holdtextbox6.Text + "','" + rb1.SelectedValue.ToString() + "','" + rb2.SelectedValue.ToString() + "','" + cb1.Text + "','" + rb3.Text + "','" + cb2.Text + "','" + rb4.Text + "')";    
                string SqlInsert = "Insert into tbTRcBase (HPN,ISH, Asthma, DM, OtherCo,HPNTreatment, ISHTreatment,AsthmaTreatment, DMTreatment, OtherCoTreatment,SecondHandSmoke,Smoker, StopSmoking , Occupation , CancerFamilyHistory, FamilyWithCancer,ParentWithCancer) values ('" + Session["rb1"] + "','" + Session["rb2"] + "','" + Session["rb3"] + "','" + Session["rb4"] + "','" + Session["rb5"] + "','" + Holdtextbox1.Text + "','" + Holdtextbox2.Text + "','" + Holdtextbox3.Text + "','" + Holdtextbox4.Text + "','" + Holdtextbox5.Text + "','" + Holdtextbox6.Text + "','" + rb1.SelectedValue.ToString() + "','" + rb2.SelectedValue.ToString() + "','" + cb1.Text + "','" + rb3.Text + "','" + cb2.Text + "','" + rb4.Text + "')";
                //   string SqlInsert = "Insert into tbTRcBase (HPN, HPNTreatment) values ('" + Session["rb1"] + "','" + Holdtextbox1.Text + "')";   
                DA = new SqlDataAdapter(SqlInsert, sqlcon);
                DataTable dt = new DataTable();
                DA.Fill(dt);
                sqlcon.Close();
                Response.Write("<script language='javascript'>alert('" + " Data has been saved " + "')</script>");
            }
            catch (Exception e)
            {


                Response.Write("<script language='javascript'>alert('" + e.Message + "')</script>");
            }
    }

1 个答案:

答案 0 :(得分:0)

要保留上一页的值,您必须先使用存储在同一页面上的Session个变量,然后再将这些变量传递到下一页。

在上一页的Page_Load事件中,根据这些Session变量的值创建一个仅绑定相应控件的函数。

这样的事情:

public void BindData()
{
    Holdtextbox1.Text = Session["Holdtextbox1"].ToString();
    .
    .
    .
    //all your controls and the Session values.
}

希望这有帮助。