Reputation: 8626
I have a grid with 3 columns empName, bonus, Id
I wanted to implement edit functionality on gridview.
For that i went into editTemplates and added 3 textboxes:
<EmptyDataTemplate>
<asp:TextBox ID="txtEmpName" runat="server"></asp:TextBox>
<asp:TextBox ID="txtBonus" runat="server"></asp:TextBox>
<asp:TextBox ID="txtID" runat="server"></asp:TextBox>
</EmptyDataTemplate>
I am refering to THIS doccument for the code.
According to this i tried to write code like:
TextBox txtUname = (TextBox)gv.DataKeys[e.RowIndex].FindControl("txtEmpName");
But after (TextBox)gv.DataKeys[e.RowIndex].
it is not showing me FindControl
Is there any mistake in the steps i have followed for this procedure?
Please help me.
Upvotes: 0
Views: 3214
Reputation: 2602
The DataKeys and DataKeyNames are used to store values that you define in the gridview. What you simply need is
TextBox txtUname = (TextBox)gv.Rows[e.RowIndex].FindControl("txtEmpName");
In case you want to know more how DataKeys can be used try google or check out this example :
http://www.dotnetcurry.com/ShowArticle.aspx?ID=291
Upvotes: 1
Reputation: 987
TextBox txtUname = (TextBox)gv.DataKeys[e.RowIndex].FindControl("txtEmpName");
instead use Rows as
TextBox txtUname = (TextBox)gv.Rows[e.RowIndex].FindControl("txtEmpName");
And it should be "EditItemTemplate"
instead of "EmptyDataTemplate"
Upvotes: 2