我有一个网格视图
<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
对应的文本。它反而空了。
为什么?
更新EditIndex后添加grdArticles.DataBind();
,如下所示:
protected void grdArticles_OnRowEditing(object sender, GridViewEditEventArgs e)
{
grdArticles.EditIndex = e.NewEditIndex;
grdArticles.DataBind();
}