简体   繁体   English

单击gridview按钮时页面未刷新

[英]Page doesn't refresh on gridview button click

I already asked a question about this problem, but as I progressed in my web application I stumbled across this problem again, because the answer there gave me a new problem. 我已经问了一个问题,关于这个问题,但正如我在我的web应用程序的进展我碰到这个问题再次绊倒,因为答案没有给我一个新的问题。 With that solution, I had the problem that the update button did not read the new value in the textbox of my gridview, but instead it read the old value. 使用该解决方案时,我遇到了一个问题,即更新按钮没有读取gridview文本框中的新值,而是读取了旧值。

CodeBehind 代码背后

With this it doesn't use the new value: 与此不使用新值:

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();
    }

And with this it gets the new value, but it no longer refreshes: 并以此获得新的价值,但不再刷新:

    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();
        }
    }

Update/LoadData Code 更新/加载数据代码

    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);
        }
    }

EDIT Well I removed the DataBinder(); 编辑好吧,我删除了DataBinder(); under Grid36_RowEditing(). 在Grid36_RowEditing()下。 This is no real solution, but a bad workaround for the moment. 这不是真正的解决方案,但目前是一个不好的解决方法。 Now I have to push the edit button twice, but atleast I'm able to Update the grid. 现在,我必须按两次编辑按钮,但是至少可以更新网格。 This is no/a horrible solution for my problem though, I hope someone will still be able to give me a real solution 不过,这不是解决我的问题的可怕方法,我希望有人仍然能够为我提供真正的解决方案

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

In Row updating method, Update the value in the database and then execute datasource as well as databind method. 在“行更新方法”中,更新数据库中的值,然后执行数据源以及databind方法。

Example :- 例子:-

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM