设置GridView的EditIndex会使EditItemTemplate中的元素不更新

时间:2019-02-13 15:12:07

标签: asp.net gridview

我有一个喜欢的网格视图

<asp:GridView ID="grdArticles" runat="server" 
    OnRowEditing="grdArticles_OnRowEditing">
  <Columns>
    <asp:TemplateField>
      <ItemTemplate>                                                                    
        <%#Eval("num") %> st                                                                
      </ItemTemplate>                                                                
      <EditItemTemplate>
        <asp:TextBox ID="txtNum" Width="30px" Text='<%#Eval("num") %>' 
          runat="server"/>                                                                
      </EditItemTemplate>
   </asp:TemplateField>
   <asp:CommandField ShowEditButton="True" ShowDeleteButton="True" />
  </Columns>
</asp:GridView>

其中填充了

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
      grdArticles.DataSource = Repo.GetArticles();
      grdArticles.DataBind();
    }        
}

和grdArticles_OnRowEditing就像

protected void grdArticles_OnRowEditing(object sender, GridViewEditEventArgs e)
{
  grdArticles.EditIndex = e.NewEditIndex;
}

当我按下编辑按钮时,文本框将显示出来,而不是空白文本,但是将没有与num相对应的文本。而是变成空的。

为什么?

1 个答案:

答案 0 :(得分:0)

在更新EditIndex后添加grdArticles.DataBind();,如下所示:

protected void grdArticles_OnRowEditing(object sender, GridViewEditEventArgs e)
{
  grdArticles.EditIndex = e.NewEditIndex;
  grdArticles.DataBind();

}