带隐藏字段的中继器
我在标记为“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>
我有一个触发此代码的链接按钮:
protected void AdminEdit_OnClick(object sender, EventArgs e)
{
HiddenField hf = (HiddenField)AdminGetAllRPT.FindControl("editAdminEmail");
但 hf 为空。我做错了什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将 AdminGetAllRPT.FindControl... 更改
为
e.Item.FindControl...
编辑:感谢您对正确答案的投票。
但是,是的,e.Item.FindControl 正是您想要的,因为您要做的不是按钮上的事件,而是转发器的 ItemCommand 上的事件。
然后在代码隐藏中,您可以设置事件
Done...
要迭代所有 RepeaterItems,您可以在转发器外部有一个按钮(或者可能在页脚或其他位置,这不是转发器 ItemCommand)
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)
另一件事是,它可能没有封装在您的编辑按钮控件下。
例如,要使查找控件方法正常工作,它应该如下所示:
由于 AdminEdit.Controls 是控件的容器,因此您现在应该能够找到该控件作为第一级子控件。如果它不是按钮的子级,您需要将其设为子级,或者从不同的方法引用它。对于动态生成的控件尤其如此。即...它是哪个控件或根元素的子元素?
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?
您需要找到 RepeaterItem 中的隐藏字段。
You need to find the hidden field within a RepeaterItem.
正如 jball 指出的,您需要在转发器项目级别进行搜索。您的链接按钮位于哪里?我相信它应该在中继器本身内(否则你可以有多个隐藏字段(每个中继器行一个),并且应该由链接按钮选取哪一个?)。因此,如果链接按钮和隐藏字段都在转发器中,那么您可以使用以下代码
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