单击rowediting事件时gridview正在消失

时间:2017-04-22 06:59:26

标签: c# asp.net gridview

我正在创建一个网络应用程序,在我的gridview中我有一行RowEditing事件,其中我将标签的值分配到EditItemTemplate的文本框中

但是我遇到了这个问题,

我的文本框出现在第二次点击中,例如

  

我需要点击两次,然后我的文本框出现了(假设我   单击行号(1)和文本框所在的行号(5)   出现在行号(1)上)

这就是我的代码看起来

protected void gridone_RowEditing(object sender, GridViewEditEventArgs e)
{
    Label lblone = (Label)gridone.Rows[e.NewEditIndex].FindControl("lbltaskmid");
    string one = lblone.Text;
    string script = "alert('" + one + "');";
    ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", script, true);
}

所以我改变了这样的代码

protected void gridone_RowEditing(object sender, GridViewEditEventArgs e)
{
    Label lblone = (Label)gridone.Rows[e.NewEditIndex].FindControl("lbltaskmid");
    string one = lblone.Text;
    string script = "alert('" + one + "');";
    ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", script, true);
    gridone.DataBind();
}

现在,当我点击编辑按钮

时,我的gridview正在消失

这是我的gridview的HTML代码

<asp:GridView OnRowEditing="gridone_RowEditing" ID="gridone" AutoGenerateColumns="False" runat="server">
    <Columns>
        <asp:TemplateField HeaderText="ID">
            <ItemTemplate>
                <asp:Label ID="lbltaskmid" runat="server" Text='<%#Eval("taskmid") %>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txttaskmid" runat="server"></asp:TextBox>
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Name">
            <ItemTemplate>
                <asp:Label ID="lbltaskname" runat="server" Text='<%#Eval("taskname") %>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txttaskname" runat="server"></asp:TextBox>
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Description">
            <ItemTemplate>
                <asp:Label ID="lbldesc" runat="server" Text='<%#Eval("description") %>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txtdesc" runat="server"></asp:TextBox>
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:ButtonField ButtonType="Button" CommandName="Edit" HeaderText="Add" ShowHeader="True" Text="EDIT" />
    </Columns>    
</asp:GridView>

我需要在这做什么?

1 个答案:

答案 0 :(得分:0)

您必须设置GridView的EditIndex

protected void gridone_RowEditing(object sender, GridViewEditEventArgs e)
{
    gridone.EditIndex = e.NewEditIndex;

    gridone.DataSource = mySource;
    gridone.DataBind();
}

FindControl("lbltaskmid")无效,因为lbltaskmid不在EditItemTemplate中。最好在RowDataBound事件中执行FindControl和读/写控件。