Search code examples
c#asp.netgridviewwebformshiddenfield

Hidden Field Value not finding value


I have an hiddenfield field in my gridview but the code behind cant get its value maybe someone could find the problem.
HTML:

<asp:TemplateField HeaderText="TweetID" Visible="false">
<ItemTemplate>
<asp:HiddenField ID="TweetID" runat="server" Value='<%#Eval("TweetID") %>' />
</ItemTemplate>
</asp:TemplateField>

.cs:

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    int index = Convert.ToInt32(e.RowIndex);
    HiddenField tid = GridView1.Rows[index].FindControl("TweetID") as HiddenField;
    //Response.Write(tid.Value);
    TweetHelper.RemoveTweet( Convert.ToInt32(tid.Value), 1);
}

by the way the response writes nothing.


Solution

  • Based on your code above what you are doing is overkill.

    Either make TweetID a Gridview.DataKey.

    Or if that's not an option, convert your Delete button to a template field and add TweetID as a CommandArgument to the Delete button.