gridview.binddata()打破了gridview中的编辑行

时间:2015-06-09 13:46:22

标签: c# asp.net

这是我之前提出的同样问题,但我想出了一个更好的方式(更简洁,更重要)。我有一个gridview,我有一个动态创建的按钮,并添加到row_databound方法的页脚。我必须在Gridview1.databind()方法中使用page_load才能使其工作并将记录插入数据库。但是当我在page_load中有数据绑定时,编辑按钮实际上并没有编辑数据库中的行。它会在命中编辑时显示可编辑的框,但在更新时不会更新它。删除按钮和编辑按钮都使用gridview和数据源内置参数,就像我说的,删除按钮仍然正常,无论我在gridview.databind方法中是否有page_load。为什么?下面是代码。

protected void Page_Load(object sender, EventArgs e)
{


    if (this.IsPostBack == false)
    {
       loadlist();
    }

    GridView1.DataBind();
}


protected void GridView1_RowDataBount(object sender, GridViewRowEventArgs e)
    {




        if (e.Row.RowType == DataControlRowType.Footer)
        {
            textboxes.Clear();
            for (int i = 0; i < e.Row.Cells.Count; i++)
            {
                if (i == 0)
                {
                    buttons.Clear();
                    String buttontitle = "Add";
                    //var tempbutton = new Button();
                    string tempString = i.ToString();
                    //tempbutton.ID = tempString;
                    addbutton.ID = tempString;
                    //tempbutton.Text = buttontitle;
                    addbutton.Text = buttontitle;
                    //tempbutton.Click += tempbutton_Click;
                    addbutton.Click += tempbutton_Click;
                    addbutton.UseSubmitBehavior = false;
                    buttons.Add(i, addbutton);
                    e.Row.Cells[i].Controls.Add(addbutton);

                }


                if (i > 0)
                {

                    var tempbox = new TextBox();
                    string tempString = i.ToString();
                    tempbox.ID = tempString;
                    textboxes.Add(i, tempbox);
                    e.Row.Cells[i].Controls.Add(tempbox);


                }

            }

<asp:GridView ID="GridView1" runat="server" AutoGenerateEditButton="True"
     AutoGenerateDeleteButton="True" ShowFooter="true"
     OnRowDataBound="GridView1_RowDataBount" 
     style="position:absolute; left:5%; width:90%; margin:0; top: 140px; height: 353px;" 
     AllowSorting="True" AutoGenerateColumns="true" AllowPaging="True"  >

    <AlternatingRowStyle BackColor="Aqua" />       
</asp:GridView>

2 个答案:

答案 0 :(得分:0)

将GridView1.DataBind()放在if (this.IsPostBack == false)部分中。每次单击某个操作(编辑,删除,插入)时,都会导致回发并再次调用数据绑定。只应在数据源更新时调用它。

答案 1 :(得分:0)

你需要在&#34;中绑定gridview。 ispostback == false&#34;条件