Repeater with HiddenField

2019-06-04 04:13发布

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?

4条回答
成全新的幸福
2楼-- · 2019-06-04 04:35

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");
查看更多
走好不送
3楼-- · 2019-06-04 04:40

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
    }
}
查看更多
何必那么认真
4楼-- · 2019-06-04 04:42

You need to find the hidden field within a RepeaterItem.

foreach(var item in AdminGetAllRPT.Items)
{        
   var hf = (HiddenField)item.FindControl("editAdminEmail");
}
查看更多
Root(大扎)
5楼-- · 2019-06-04 04:50

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?

查看更多
登录 后发表回答