GridView行未更新,gridview编辑模板中的文本框中的文本未来

时间:2013-04-06 07:01:01

标签: c# asp.net

我有网格视图,我想更新行,但它没有发生。数据源是DataTable。请帮忙。

以下是标记

<asp:GridView ID="GrdV" runat="server" AutoGenerateColumns="false"   
   OnRowEditing="GrdV_RowEditing"  OnRowUpdating="GrdV_RowUpdating">

 <Columns>
   <asp:TemplateField HeaderText="Clip Description">
        <ItemTemplate>
            <asp:Label ID="lblDescrptn" runat="server" Text='<%# Bind("Description") %>'>
            </asp:Label>
        </ItemTemplate>
        <EditItemTemplate>
            <asp:TextBox ID="descTbx" runat="server" Text='<%# Bind("Description") %>'>
            </asp:TextBox>
        </EditItemTemplate>
    </asp:TemplateField>

   <asp:CommandField ShowEditButton="True" />

 </Columns>

这是

背后的代码
    protected void GrdV_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

        // Retrieve the row being edited.
      int index = GrdV.EditIndex;
        GridViewRow row = GrdV.Rows[index];           
        TextBox t1 = row.FindControl("descTbx") as TextBox;

        DataTable dt = (DataTable)Session["tmdataTable"];

        dt.Rows[index]["Description"] = t1.Text; //Description is a column of my DataTable
        dt.AcceptChanges();
        GrdV.EditIndex = -1;
        GrdV.DataSource = dt;
        GrdV.DataBind();

    }

在调试时,我发现即使在我用新值填充文本框之后,文本框也会传递空字符串t1.Text =""。 我认为错误符合行

TextBox t1 = row.FindControl("descTbx") as TextBox;

PageLoad代码

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            GrdV.DataSource = Session["tmdataTable"];
            GrdV.DataBind();
        }


        DataTable Finaldt = getTable();

        GrdV.DataSource = Finaldt;
        GrdV.DataBind();
        Session["tmdataTable"] = Finaldt;

    }

3 个答案:

答案 0 :(得分:1)

EditIndex不可用,您需要GridViewUpdateEventArgs

中的e.RowIndex
// Retrieve the row being edited.
DataTable dt = (DataTable)Session["tmdataTable"];
GridViewRow row = GrdV.Rows[e.RowIndex];
TextBox t1 = row.FindControl("descTbx") as TextBox;

dt.Rows[row.DataItemIndex]["Description"] = t1.Text; //Description is a column of my DataTable
dt.AcceptChanges();
GrdV.EditIndex = -1;
GrdV.DataSource = dt;
GrdV.DataBind();

答案 1 :(得分:0)

 protected void GrdV_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
         if (e.Item is GridDataItem)
        {
        // Retrieve the row being edited.
      int index = GrdV.EditIndex;
        GridViewRow row = GrdV.Rows[index];           
        TextBox t1 = row.FindControl("descTbx") as TextBox;

        DataTable dt = (DataTable)Session["tmdataTable"];

        dt.Rows[index]["Description"] = t1.Text; //Description is a column of my DataTable
        dt.AcceptChanges();
        GrdV.EditIndex = -1;
        GrdV.DataSource = dt;
        GrdV.DataBind();
        }
    }

答案 2 :(得分:0)

稍微改变你的代码并检查。将EditIndex更改为e.RowIndex

protected void GrdV_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    GridViewRow row = GrdV.Rows[e.RowIndex]; // this line is changed      
    TextBox t1 = row.FindControl("descTbx") as TextBox;

    DataTable dt = (DataTable)Session["tmdataTable"];

    dt.Rows[row.DataItemIndex]["Description"] = t1.Text; //Description is a column of my DataTable
    dt.AcceptChanges();
    GrdV.EditIndex = -1;
    GrdV.DataSource = dt;
    GrdV.DataBind();
}

你做到了吗:

protected void Page_Load(object sender, EventArgs e)
{     
    if(!IsPostBack)
    {
        GrdV.DataSource = Session["tmdataTable"];
        GrdV.DataBind();
    }
}