【问题标题】:Keeping ViewState of page after PostBack RefreshPostBack 刷新后保持页面的 ViewState
【发布时间】:2019-01-15 14:04:25
【问题描述】:

我有一个下拉列表,其中包含在后面的代码中创建的列表项。 ddl填充()。非常简单并且可以完成工作。它填充了当前月份和未来几个月。我使用此下拉菜单作为填充 Gridview 的选择。当此下拉索引更改时,它会更改隐藏字段值和相应的查询以填充网格视图。所有这些都正常工作。在gridview 中,我有另一个下拉列表和一个按钮。两者都将选定的行提交到数据库。这也很好用。问题是,每次这些行之一提交到数据库时,都会导致回发并重置整个页面。它将下拉列表重置为第一个列表项。 IE。例如,我将下拉列表更改为索引 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);
    }

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

【问题讨论】:

标签: c# asp.net


【解决方案1】:

我发现这个问题很难问,因为它有很多活动部件。 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。这真的是我需要的。我还有其他令人耳目一新的物品。这就是为什么它下面的那些电话在那里。如果有人看到这个,希望它会有所帮助。

【讨论】:

    猜你喜欢
    • 2011-08-20
    • 2019-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-03
    • 2017-12-10
    • 2019-09-19
    • 1970-01-01
    相关资源
    最近更新 更多