回发刷新后保持页面的ViewState

时间:2019-01-15 14:04:26

标签: c# asp.net

我有一个下拉列表,后面有在代码中创建的列表项。 ddlFill()。非常简单,可以完成工作。它填充了当前月份以及未来几个月。我使用此下拉列表作为填充Gridview的选择。当此下拉索引更改时,它将更改隐藏字段值和相应的查询以填充gridview。所有这些都可以正常工作。在gridview中,我还有另一个下拉列表和一个按钮。两者都将选定的行提交到数据库。那也很好。问题是,这些行中的每一个提交到数据库时,都会导致回发并重置整个页面。它将下拉列表重置为第一个列表项。即例如,我将下拉列表更改为索引4。在这种情况下,应该是四月。如果我向数据库提交一行,则页面刷新并返回到索引0 ..在这种情况下为一月。如何防止它以这种方式重置并保持提交行时的位置?

我尝试了几种不同的选择。我已经尝试了会话状态。隐藏字段值更改。似乎没有任何作用。它要么不执行回发,因此从不提交给数据库,要么执行回发,正确提交,然后重置整个页面。包括将hiddenfield值重置为0

  /* This is up in Page load. */

       if (Session["pageStatus"] != null)
        {
            if (Session["pageStatus"].ToString() == "Loaded")
            {
                hf2.Value = "Loaded";
            }
        }
        else
        {
            hf2.Value = "New";
        }

        if (Session["selectedMonth"] != null)
        {
            hf1.Value = Session["selectedMonth"].ToString();
        }

        if (ViewState["button_was_clicked"] != null)
        {
            ddlFill();
            StyleDDL();
        }

        lblTestlabel.Text = hf2.Value;
        AddAttributes();


        ShowMonth();
        if (!Page.IsPostBack)
        {
            btnReviewCurrentMonth_OnClick(sender, e);
            ddlFill();
            StyleDDL();
        }

private void ddlFill()
    {



            string a, b, c, d, e, f;
            a = "0"; 
            b = "1";
            c = "2";
            d = "3";
            e = "4";
            f = "5";

            DropDownList1.Items.Insert(0, new ListItem(ReturnMonth(a))); // A blank object call and the ReturnMonth Method fill the list items.
            DropDownList1.Items.Insert(1, new ListItem(ReturnMonth(b)));
            DropDownList1.Items.Insert(2, new ListItem(ReturnMonth(c)));
            DropDownList1.Items.Insert(3, new ListItem(ReturnMonth(d)));
            DropDownList1.Items.Insert(4, new ListItem(ReturnMonth(e)));
            DropDownList1.Items.Insert(5, new ListItem(ReturnMonth(f)));

      /* These were for various testing options to get it to maintain the 
         state */
        hf2.Value = "Loaded";
        Session["pageStatus"] = "Loaded";
        DropDownList1.SelectedIndex = Int32.Parse(hf1.Value);
    }

我的目标是在提交到db后维护页面的状态。

1 个答案:

答案 0 :(得分:0)

我发现这个问题很难问,因为它有很多活动部件。 ddlFill()方法不是问题。它所做的只是填充列表项。那些相应的“选定索引”值在选定时将发生变化,并且基于这些值,我将为隐藏字段分配不同的值。然后将其用作SqlDataSource控制变量,并将基于该控制值取回GridView数据。

gridview中的下拉列表用于选择索引行并将其提交到SQL DB。在该代码的结尾,我正在刷新页面。它需要刷新才能正确提交,并返回一个新的Gridview,而先前提交的行现在消失了。那就是问题所在。刷新后,它将隐藏字段的值更改为0,然后将所有内容重置为零。所以,这就是我所做的。

 //This method controls the Drop Down List change event. Underwriter change. 
    protected void DropDownList1_OnSelectedIndexChanged(object sender, EventArgs e)
    {

        DropDownList ddl = (DropDownList)sender;
        GridViewRow row = (GridViewRow)ddl.Parent.Parent;
        int idx = row.RowIndex;

        GridView1.SelectedIndex = idx;

        string Client = GridView1.SelectedRow.Cells[0].Text;//Client Name
        string NewUw = ddl.Text.ToString();
        int UniqCNT = new Int32();
        UniqCNT = Int32.Parse(GridView1.SelectedRow.Cells[1].Text.ToString()); //UniqClient */
        string ExpPolicyNums = GridView1.SelectedRow.Cells[2].Text;
        int Ub = Int32.Parse(GridView1.SelectedRow.Cells[10].Text);//UniqBroker
        DateTime ExperationDate = DateTime.Parse(GridView1.SelectedRow.Cells[6].Text); //ExpDate


        string Company = GridView1.SelectedRow.Cells[7].Text; //Company issuer
        string Broker = GridView1.SelectedRow.Cells[8].Text;  //Broker_Name
        string Premium = GridView1.SelectedRow.Cells[3].Text; //Premiums
        string TotalPremium = GridView1.SelectedRow.Cells[4].Text; //Total premiums
        string Reviewed = "No"; //Updates the DB and shows that it hasn't been reviewed by the Message Creator
                                //DateCreated gets inserted when record is created 
        string InsertedBy = Request.LogonUserIdentity.Name.Substring(Request.LogonUserIdentity.Name.LastIndexOf(@"\") + 1);

        DateTime dateUpDated = DateTime.Now; //Inserts a dateUpdated record

        string query = "INSERT INTO [GTU_Apps].[dbo].[Reviewed_Renewal_Policy] (UniqClient, Client, [Expiring_Policies], Premiums, TotalPremium, UniqBroker, ExpDate, NewUw, Company, Broker_Name,  Reviewed, DateUpDated, InsertedBy) " +
            "VALUES (@UniqCNT, @Client, @ExpPolicyNums, @Premium, @TotalPremium, @Ub, @ExperationDate, @NewUw, @Company, @Broker,  @Reviewed, @dateUpDated, @InsertedBy)";

        using (SqlConnection conn = new SqlConnection("Data Source=GTU-BDE01;Initial Catalog=GTU_Apps;Integrated Security=True"))
        {
            using (SqlCommand comm = new SqlCommand(query, conn))
            {
                comm.Parameters.AddWithValue("@UniqCNT", UniqCNT);
                comm.Parameters.AddWithValue("@Client", Client);
                comm.Parameters.AddWithValue("@ExpPolicyNums", ExpPolicyNums);
                comm.Parameters.AddWithValue("@Premium", Premium);
                comm.Parameters.AddWithValue("@TotalPremium", TotalPremium);
                comm.Parameters.AddWithValue("@Ub", Ub);
                comm.Parameters.AddWithValue("@ExperationDate", ExperationDate);
                comm.Parameters.AddWithValue("@NewUw", NewUw);
                comm.Parameters.AddWithValue("@Company", Company);
                comm.Parameters.AddWithValue("@Broker", Broker);
                comm.Parameters.AddWithValue("@Reviewed", Reviewed);
                comm.Parameters.AddWithValue("@dateUpDated", dateUpDated);
                comm.Parameters.AddWithValue("@InsertedBy", InsertedBy);
                conn.Open();
                comm.ExecuteNonQuery();
                conn.Close();
            }
        }
        GridView1.DataBind();  
        GridView1.SelectedIndex = -1;
        int index = DropDownList1.SelectedIndex;
        ConfirmIndex(index);
        //End(sender, e);
    }

使用DataBind可以正常工作。它仅刷新了Gridview。这才是我真正需要的。我还有其他令人耳目一新的项目。这就是它下面的那些电话在那里的原因。如果有人看到此消息,希望对您有所帮助。