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

问题描述 投票:0回答:1

我有一个网格视图

<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对应的文本。它反而空了。

为什么?

asp.net gridview
1个回答
0
投票

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

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

}
© www.soinside.com 2019 - 2024. All rights reserved.