Gridview rowupdating不起作用

时间:2012-12-27 18:56:36

标签: asp.net visual-studio-2010 gridview

我有一个简单的任务,即点击更新链接按钮更新行,

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowEditing="GridView1_RowEditing"
            OnRowUpdating="GridView1_RowUpdating" DataKeyNames="ID">
            <Columns>
                <asp:TemplateField HeaderText="ID">
                    <ItemTemplate>
                        <asp:Label ID="Label3" runat="server" Text='<% #Eval("ID") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Mobile">
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<% #Eval("Mobile") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="LName">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<% #Eval("LName") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<% #Eval("LName") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="action">
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Edit">LinkButton</asp:LinkButton>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:LinkButton ID="LinkButton2" runat="server">Update</asp:LinkButton>
                    </EditItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

在CS页面

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            TextBox t1 = GridView1.Rows[e.RowIndex].FindControl("TextBox1") as TextBox;


            string s = t1.Text.ToString();
            Label3.Text = s;
}

我尝试了几次,但我没有得到任何答案。我错过了什么可能的代码或代码中有任何错误。感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

您的LinkBut​​ton需要在EditTemplate中应用CommandName:

<asp:LinkButton ID="LinkButton2" runat="server" CommandName="Update">Update</asp:LinkButton>