输入不保存在Gridview行更新事件
问题描述:
我知道这个问题已被问过,但我已经尝试过这些帖子中的解决方案,没有任何工作。输入不保存在Gridview行更新事件
我有一个从一个XML文件填充,并且是为了更新一个XML文件。由于此gridview所在页面的用途,数据绑定发生在page_load事件上。另外,我使用TemplateFields来控制gridview的布局。
这是我的代码。首先,在GridView:
<asp:GridView ID="fileGrid" runat="server" EnableViewState="true" AutoGenerateColumns="False" Width="315px" BorderStyle="None"
OnRowDeleting="fileGrid_RowDeleting" OnRowEditing="fileGrid_RowEditing"
OnRowCancelingEdit="fileGrid_RowCancelingEdit" OnRowUpdating="fileGrid_RowUpdating"
OnRowDataBound="fileGrid_RowDataBound">
<Columns>
<asp:BoundField DataField="name" HeaderText="Uploaded Files" SortExpression="name" ReadOnly="true">
<ItemStyle CssClass="fileName" />
</asp:BoundField>
<asp:TemplateField HeaderText="Description">
<ItemStyle CssClass="desc-cell" />
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("desc") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="descTb" runat="server" Text='<%# Bind("desc") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemStyle CssClass="cmdCell" />
<ItemTemplate>
<asp:LinkButton ID="lnkEdit" runat="server" CommandName="Edit">Edit</asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="lnkUpdate" runat="server" CommandName="Update">Update</asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemStyle CssClass="cmdCell" />
<ItemTemplate>
<asp:LinkButton ID="lnkDelete" runat="server" CommandName="Delete">Delete</asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="lnkCancel" runat="server" CommandName="Cancel">Cancel</asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
而且我OnRowUpdating功能。这是问题发生的地方:
protected void fileGrid_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int eRow = e.RowIndex;
GridViewRow row = fileGrid.Rows[eRow];
string name = row.Cells[0].Text;
string desc = ((TextBox)(row.Cells[1].FindControl("descTb"))).Text;
// Update XML via Linq-to-XML here
}
但是,我无法捕获任何输入到descTb文本框中的值; “desc”总是返回null。我知道page_load在RowUpdating之前触发,但我无法在PostBack检查中包装填充GridView的代码,因为那时我的gridview空白(并且我仍然没有得到那个血腥的文本框值)。会话内存中初始的,预编辑的网格视图数据并在RowUpdating事件中更新该数据不起作用。将文本框控件上的EnableViewState设置为“true”,并删除文本属性中的<%#Bind()%>不起作用。
有没有人有任何想法?
答
至于有没有必要让在RowUpdating事件单独GridViewRow,你可以使用GridView.Rows[e.RowIndex].FindControl
像:
string desc = ((TextBox)(fillGrid.Rows[e.RowIndex].FindControl("descTb"))).Text;
更新:下面是一些逻辑代码,可以帮助你:
protected void Page_Load(object sender, EventArgs e)
{
fillGrid.DataSource = table;
if(!IsPostBack)
fillGrid.DataBind();
}
protected void fillGrid_RowEditing(object sender, GridViewEditEventArgs e)
{
fillGrid.EditIndex = e.NewEditIndex;
fillGrid.DataBind();
}
protected void fillGrid_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string desc = ((TextBox)(fillGrid.Rows[e.RowIndex].FindControl("descTb"))).Text;
}
我知道,我只是想保持我的代码尽可能简短。无论如何,更改这并不能解决我的问题。 –
请参阅我已更新我的答案。 – AsifAli72090