I have the following HiddenField within a Repeater labeled "AdminGetAllRPT":
<asp:Repeater ID="AdminGetAllRPT" runat="server" OnLoad="AdminGetAllRPT_Load">
<HeaderTemplate>
<table id="AdminGetAllTBL">
<tr>
<td></td>
<td>Username</td>
<td>Email Address</td>
<td>First Name</td>
<td>Last Name</td>
<td>Active?</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:LinkButton ID="AdminEditLnk" runat="server" OnClick="AdminEdit_OnClick">Edit
<asp:HiddenField ID="editAdminEmail" runat="server" Value='<%# Eval("emailAddress") %>' />
</asp:LinkButton>
</td>
<td><%# Eval("userName") %></td>
<td><%# Eval("emailAddress") %></td>
<td><%# Eval("firstName") %></td>
<td><%# Eval("lastName") %></td>
<td><%# Eval("isActive") %></td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr class="alternateTemplate">
<td>
<asp:LinkButton ID="AdminEditLnk" runat="server" OnClick="AdminEdit_OnClick">Edit
<asp:HiddenField ID="editAdminEmail" runat="server" Value='<%# Eval("emailAddress") %>' />
</asp:LinkButton>
</td>
<td><%# Eval("userName") %></td>
<td><%# Eval("emailAddress") %></td>
<td><%# Eval("firstName") %></td>
<td><%# Eval("lastName") %></td>
<td><%# Eval("isActive") %></td>
</tr>
</AlternatingItemTemplate>
<FooterTemplate>
<tr>
<td colspan="2"><a href="#">Add Administrator</a></td>
</tr>
</table>
</FooterTemplate>
</asp:Repeater>
I have a linkbutton that fires this code:
protected void AdminEdit_OnClick(object sender, EventArgs e)
{
HiddenField hf = (HiddenField)AdminGetAllRPT.FindControl("editAdminEmail");
but the hf is coming up null. What am I doing wrong?
As jball has pointed out, you need to search at repeater item level. Where is your link button located? I believe that it should be within repeater it self (otherwise you can have multiple hidden fields (one for each repeater row) and which one is supposed to be picked up by linkbutton?). So if both link button and hidden field are in repeater then you can use code such as
Change AdminGetAllRPT.FindControl...
to
e.Item.FindControl...
Edit: Thanks for the votedown on a correct answer.
But yes e.Item.FindControl is what you want because what you're trying to do is not an event on the button, it's an event on the ItemCommand of the repeater.
Then in the code-behind you can setup the event
Done...
To iterate over all RepeaterItems, you can have a button outside of the repeater (or maybe in the footer or something, this is not a repeater ItemCommand)
You need to find the hidden field within a RepeaterItem.
One other thing, it may not be encapsulated under your edit button control.
For instance it should look like so for the find control method to work:
Since AdminEdit.Controls is a container of controls, you should now be able to find that control as a first level child. If it is not a child of the button, you will either need to make it a child, or reference it from a different method. This is especially true for dynamically generated controls. Ie... what control or root element is it a child of?