问题
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?
回答1:
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.
<asp:Repeater OnItemCommand="MyButtonCommandEvent" ID="AdminGetAllRPT" runat="server" OnLoad="AdminGetAllRPT_Load">
Then in the code-behind you can setup the event
void MyButtonCommandEvent(Object src, RepeaterCommandEventArgs e) {
//My event that is raised for each button pressed in the RepeaterItem
var poo = e.Item.FindControl("myhiddenfieldid") as HiddenField;
}
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)
foreach(var item in AdminGetAllRPT.Items)
{
if (item.ItemType == RepeaterItemType.Item || item.ItemType == RepeaterItemType.AlternatingItem)
{
var hiddenField = item.FindControl("hiddenFieldid") as HiddenField;
//Do Stuff
}
}
回答2:
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:
<asp:button id="AdminEdit" runat="server" onclick.... > <!-- Now put in your hidden field control inside (encapulated) in your button. --> <asp:HiddenField ID="editAdminEmail" runat="server" Value='<%# Eval("emailAddress") %>' /> </asp:button>
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?
回答3:
You need to find the hidden field within a RepeaterItem.
foreach(var item in AdminGetAllRPT.Items)
{
var hf = (HiddenField)item.FindControl("editAdminEmail");
}
回答4:
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
protected void AdminEdit_OnClick(object sender, EventArgs e)
{
var repeaterItem = ((Control)sender).NamingContainer;
HiddenField hf = (HiddenField)repeaterItem.FindControl("editAdminEmail");
来源:https://stackoverflow.com/questions/4435766/repeater-with-hiddenfield