删除GridView中的行需要在删除前保存TextBox值

时间:2015-12-09 10:29:46

标签: c# asp.net

我有GridView删除记录,我有这个列

<asp:TextBox ID="txtTitle" runat="server" Text='<%# Bind("TITLE") %>' MaxLength="50" />

然后第2栏

<asp:LinkButton ID="lbtnDelete" runat="server" CommandName="Delete"></asp:LinkButton>

删除有效,但我希望在删除前保存TextBox值。我试着这样:

protected void gvActivites_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    TextBox textbox = (TextBox)gvActivites.Rows[e.RowIndex].FindControl("txtTitle");

    lblDeleteSuccess.Text = textbox.Text + " has been deleted successfully..";
}

但它没有保存TextBox的值,如何在删除之前保存TextBox的值?

2 个答案:

答案 0 :(得分:2)

只需携带会话或viewState: 它的不断增加:

GridViewRow row = (GridViewRow)gvActivites.Rows[e.RowIndex];
int rowIndex = row.RowIndex;
TextBox textbox = (TextBox)row.FindControl("txtTitle");

其onrowdeleting:您需要根据usename列设置单元格索引 细胞[columnindex]

GridViewRow row = (GridViewRow)gvActivites.Rows[e.RowIndex];
int rowIndex = row.RowIndex;
string lvUserName= gvActivites.Rows[rowIndex].Cells[2].Text.ToString();
Session["Name"] = lvUserName;
lblDeleteSuccess.Text = lvUserName + " has been deleted successfully..";

答案 1 :(得分:1)

在删除之前将其分配给字符串变量,然后将其添加到Viewstate或Session。

TextBox textbox = (TextBox)gvActivites.Rows[e.RowIndex].FindControl("txtTitle");
string TextBeforeDeletion = Textbox.Text;
ViewState.Add("TextBeforeDeletion, TextBeforeDeletion );

或者在一行代码中更好:

string TextBeforeDeletion = ((TextBox)gvActivites.Rows[e.RowIndex].FindControl("txtTitle")).Text;
ViewState.Add("TextBeforeDeletion, TextBeforeDeletion );