Hidden Field Value not finding value

895 views Asked by At

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.

2

There are 2 answers

0
fnostro On BEST ANSWER

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.

0
Navoneel Talukdar On

Your code should work fine.However another way to find the control is

GridViewRow row = GridView1.Rows[e.RowIndex];
HiddenField hdn = (HiddenField)row.FindControl("TweetID");
string value = hdn.Value;

or simply

var tweetid = ((HiddenField)GridView1.Rows[e.RowIndex].FindControl("TweetID")).Value;