Groups > Asp .Net > ASP.NET Tips and tricks > Re: Finding Controls in a Repeater Header




Finding Controls in a Repeater Header

Finding Controls in a Repeater Header
Wed, 12 Dec 2007 10:02:01 +000
The rationale behind many Microsoft ASP.NET controls is sound, but at times
their implementation is frustrating, and even using VS2008, old issues still
exist

For example, using a repeater, it is possible find controls in a header during
databinding by adding a listener to the ItemCreated or ItemDataBound events.

Once a repeater is bound, only the bound Items are easily accessible through the
Items property.  If you subsequently want to get to a control in a header (or
footer), there is no obvious method.

If you perform a recursive search of the repeater and its controls, you will see
that the control structure does contain the header.  So, what's an easy way to
find controls in the header?

What I do is add a static helper class to a web site that contains a recursive
control finder.  Now, you need to be aware that with repeated items, there will
be the same named item within each repeated item (that is ID : client ID and
Unique ID are managed to be unique)

Since a header and footer only exists once, their controls are easier to find by
name (assuming there is no control with that name elsewhere in the repeater
control descendants).

 

Here is some code to find controls by name and type

///<summary>

/// Finds the control by name, starting at a control and drilling down

///</summary>

///<param name="controlName"></param>

///<param name="searchStart"></param>

///<returns></returns>staticpublicControl
RecursiveFindControl(string controlName, Control searchStart)

{if (searchStart.ID == controlName)

return searchStart;

else

{foreach (Control child in searchStart.Controls)

{Control result = RecursiveFindControl(controlName, child);

if (result != null)return result;

}

}returnnull;

}

///<summary>

/// Finds the first control derived from a particulr type, recursively, starting
with the saerch start control and recursively drilling down

///</summary>

///<param name="controlType"></param>

///<param name="searchStart"></param>

///<returns></returns>staticpublicControl RecursiveFindControl(Type
controlType, Control searchStart)

{

// if the control is derived from the type passed, return that controlif
(searchStart.GetType().IsSubclassOf(controlType))

return searchStart;

else

{

// iterate the childrenforeach (Control child in searchStart.Controls)

{Control result = RecursiveFindControl(controlType, child);

if (result != null)return result;

}

}returnnull;

}
Post Reply
Re: Finding Controls in a Repeater Header
Wed, 12 Dec 2007 10:23:29 +000
I use Findcontrol .. 

aspx - 

 

  <asp:Repeater ID="r1" runat="server"
OnItemDataBound="r1_ItemDataBound">
                <ItemTemplate>
                    <asp:Label ID="rlable1"
runat="server">
                    </asp:Label>                 
                    <br />
                </ItemTemplate>
                <HeaderTemplate>
                    <asp:Label ID="headerLabel"
runat="server">
                    </asp:Label>
                    <hr />
                </HeaderTemplate>
            </asp:Repeater>

 

and in codebehind..

  protected void r1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item) // For items
        {
            Label rlable1 = e.Item.FindControl("rlable1") as Label;
            System.Data.DataRowView data = e.Item.DataItem as
System.Data.DataRowView;
            rlable1.Text = data["Extra"].ToString();
        }
        else if (e.Item.ItemType == ListItemType.Header) // For Header
        {
            Label headerLabel = e.Item.FindControl("headerLabel") as
Label;            
            headerLabel.Text = "Header That I want to Set from Code";
        }        
    }
Post Reply
Re: Finding Controls in a Repeater Header
Wed, 12 Dec 2007 10:36:21 +000
Your code is exactly what you can do during data binding, and I mentioned this
in the article.

But what happens if you want to get to the header controls AFTER the databind is
finished, or on a subsequent page postback?  You don't want to be rebinding the
repeater again just to get to a control!

This is where my method comes in.
Post Reply
Re: Finding Controls in a Repeater Header
Tue, 3 Jun 2008 13:52:31 +0000
Ok, but what about handling events raised by controls in a repeater's header?  
I've got a repeater with an ImageButton in the header, and when the image button
is clicked, neither the button's Click() event nor the repeater's ItemCommand()
event handlers are raised.  Is there any way to handle events from controls in a
repeater's header?
Post Reply
Re: Finding Controls in a Repeater Header
Tue, 3 Jun 2008 15:25:52 +0000
You can search for a control on each postback during page load or init and
explicitely rebind the event handler, which you must do for dynamic controls -
then it will fire after page load completes
Post Reply
<< Previous 1 2 Next >>
( Page 1 of 2 )
about | contact