简体   繁体   English

GridView中的事件,回发和数据绑定丢失

[英]Event In GridView, Postback And Databinding Loss

I have a simple gridview in a user control. 我在用户控件中有一个简单的gridview。

This gridview is binding to a List of object type Foo that just has a simple string property: 此gridview绑定到对象类型Foo的列表,该对象类型仅具有简单的字符串属性:

public class Foo
{
      public string MyProperty {get; set;
}

To do the databinding, I have the following code. 要进行数据绑定,我有以下代码。 The MyFoos property is the collection I am binding to. MyFoos属性是我绑定到的集合。 I set this property in the page_load of the page hosting the user control. 我在托管用户控件的页面的page_load中设置了此属性。 gridMyFoos is the id of the gridview in question. gridMyFoos是所讨论的gridview的ID。

public List<Foo> MyFoos {get; set;}

    protected void Page_Load(object sender, EventArgs e)
    {
         if (!Page.IsPostBack)
          {
            if (MyFoos != null)
            {
                PopulateGrid();
            }
         }
    }

private void PopulateGrid()
{
        gridMyFoos.DataSource = MyFoos;
        gridMyFoos.DataBind();

} }

I have Edit and Delete buttons in the gridMyFoos using the regular old way with EditItemTemplate . 我使用EditItemTemplate的常规旧方法在gridMyFoos具有“编辑”和“删除”按钮。

Here is the problem. 这是问题所在。 If I take out the !Page.IsPostBack I see records, however the RowCommand events will not fire. 如果我将!Page.IsPostBack取出,则会看到记录,但是不会触发RowCommand事件。 If I keep it in there, I don't see any records. 如果我将其保留在此处,则看不到任何记录。 What gives? 是什么赋予了?

You can use RowUpdating event: 您可以使用RowUpdating事件:

GridView gv  = ((GridView)sender);
GridViewRow row = gv.Rows[e.RowIndex];

and the RowEditing event put this: 和RowEditing事件将其放置在此位置:

gvName.EditIndex = e.NewEditIndex;

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

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