【问题标题】:Page doesn't refresh on gridview button click页面不刷新gridview按钮点击
【发布时间】:2012-12-08 05:01:00
【问题描述】:

我已经向question 询问过这个问题,但随着我在 Web 应用程序中的进展,我再次偶然发现了这个问题,因为那里的答案给了我一个新问题。使用该解决方案,我遇到的问题是更新按钮没有读取我的 gridview 文本框中的新值,而是读取了旧值。

代码隐藏

这样它就不会使用新值:

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

    private void DataBinder()
    {
        Grid30.DataBind();
        Grid31.DataBind();
        Grid32.DataBind();
        Grid33.DataBind();
        Grid34.DataBind();
        Grid35.DataBind();
        Grid36.DataBind();
        Grid37.DataBind();
        Grid38.DataBind();
        Grid40.DataBind();
        Grid41.DataBind();
        Grid42.DataBind();
        Grid43.DataBind();
        Grid44.DataBind();
        Grid45.DataBind();
        Grid51.DataBind();
        Grid52.DataBind();
        Grid53.DataBind();
        Grid54.DataBind();
        Grid55.DataBind();
        Grid56.DataBind();
        Grid57.DataBind();
        Grid58.DataBind();
        Grid61.DataBind();
        Grid62.DataBind();
        Grid63.DataBind();
        Grid64.DataBind();
    }

有了这个,它得到了新的值,但它不再刷新:

    protected void Page_Load(object sender, EventArgs e)
    {
        LoadData();
        DataBinder();

    }

    private void DataBinder()
    {
        if (!Page.IsPostBack)
        {
            Grid30.DataBind();
            Grid31.DataBind();
            Grid32.DataBind();
            Grid33.DataBind();
            Grid34.DataBind();
            Grid35.DataBind();
            Grid36.DataBind();
            Grid37.DataBind();
            Grid38.DataBind();
            Grid40.DataBind();
            Grid41.DataBind();
            Grid42.DataBind();
            Grid43.DataBind();
            Grid44.DataBind();
            Grid45.DataBind();
            Grid51.DataBind();
            Grid52.DataBind();
            Grid53.DataBind();
            Grid54.DataBind();
            Grid55.DataBind();
            Grid56.DataBind();
            Grid57.DataBind();
            Grid58.DataBind();
            Grid61.DataBind();
            Grid62.DataBind();
            Grid63.DataBind();
            Grid64.DataBind();
        }
    }

更新/加载数据代码

    protected void Grid36_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

        GridViewRow row = Grid36.Rows[e.RowIndex];

        var txtQuantity = (TextBox)row.FindControl("Spoor36TB");
        int quantity = int.Parse(txtQuantity.Text);

        Grid36.EditIndex = -1;
        DataBinder();
    }

    private void LoadData()
    {
        foreach(Rail rail in getAllPositions())
        {
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn(rail.RailNr.ToString(), typeof(string)));
            for (int i = 0; i < rail.AmountOfPositions; i++)
            {
                DataRow dr = dt.NewRow();
                dr[rail.RailNr.ToString()] = "1";
                dt.Rows.Add(dr);
            }
            DataSources(rail.RailNr, dt);
        }
    }

编辑 好吧,我删除了 DataBinder();在 Grid36_RowEditing() 下。这不是真正的解决方案,但目前是一个糟糕的解决方法。现在我必须按两次编辑按钮,但至少我能够更新网格。虽然这对我的问题来说不是/一个可怕的解决方案,但我希望有人仍然能够给我一个真正的解决方案

    protected void Grid36_RowEditing(object sender, GridViewEditEventArgs e)
    {
        Grid36.EditIndex = e.NewEditIndex;
        //DataBinder();
    }

【问题讨论】:

  • 数据绑定中的代码仅在初始加载时执行,如您声明的不是回发。
  • 为什么在回发时打电话给LoadData?你也应该把它放在!PostBack-check 中。一般来说:当 ViewState 启用(默认)时,不要从 Page_Load 重新加载回发数据。
  • 你的 LoadData 正在做什么.. 如果它不是 PostBack 当然它会工作.. 所以你需要添加将加载的逻辑,无论它是否是回发或编写逻辑检查 If IsPostBack,如果需要,将值存储在 Session 变量中。你需要显示什么 LoadData();方法看起来像
  • LoadData 已添加到帖子中,它将单元格添加到网格视图中。我在每个单元格中都放了一个 1 只是为了测试。很快,这些单元格的数据将从数据库中调用
  • @TimSchmelter 如果我把它放在 !PostBack-check 中,当我点击任何网格视图中的按钮时,它会删除我所有的网格

标签: c# asp.net button gridview refresh


【解决方案1】:

在行更新方法中,更新数据库中的值,然后执行数据源以及数据绑定方法。

示例:-

GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
int id = Int32.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
TextBox tname = (TextBox)row.FindControl("nam");
TextBox tques = (TextBox)row.FindControl("que");
MySqlCommand cmd = new MySqlCommand("update exam set name1=@name,ques=@ques where id = @id", con);
cmd.Parameters.Add("@id", MySqlDbType.Int16).Value = id;
cmd.Parameters.Add("@name", MySqlDbType.VarChar, 30).Value = tname.Text.Trim();
cmd.Parameters.Add("@ques", MySqlDbType.VarChar,40).Value = tques.Text.Trim();
con.Open();
cmd.ExecuteNonQuery();
GridView1.EditIndex = -1;
// Bind grid now
 GridView1.DataSource= dt; // get data from database
 GridView1.Databind();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-13
    • 2012-02-17
    • 2014-04-10
    • 1970-01-01
    • 2018-06-18
    • 1970-01-01
    相关资源
    最近更新 更多