Domanda

Mi chiedevo come si potrebbe trovare i controlli nella HeaderTemplate o FooterTemplate di un controllo Asp.Net Repeater.

io possa accedere sull'evento ItemDataBound, ma mi chiedevo come ottenere loro dopo (ad esempio per recuperare un valore di un ingresso nell'intestazione / piè di pagina).

Nota:. Ho postato questa domanda qui dopo aver trovato la risposta solo in modo che me lo ricordo (e forse altre persone potrebbero trovare questo utile)

È stato utile?

Soluzione

Come notato nei commenti, questo funziona solo dopo aver databound vostro ripetitore.

Per trovare un controllo nel intestazione :

lblControl = repeater1.Controls[0].Controls[0].FindControl("lblControl");

Per trovare un controllo nel piè di pagina :

lblControl = repeater1.Controls[repeater1.Controls.Count - 1].Controls[0].FindControl("lblControl");

Con i metodi di estensione

public static class RepeaterExtensionMethods
{
    public static Control FindControlInHeader(this Repeater repeater, string controlName)
    {
        return repeater.Controls[0].Controls[0].FindControl(controlName);
    }

    public static Control FindControlInFooter(this Repeater repeater, string controlName)
    {
        return repeater.Controls[repeater.Controls.Count - 1].Controls[0].FindControl(controlName);
    }
}

Altri suggerimenti

Una migliore soluzione

È possibile controllare tipo di elemento in caso ItemCreated:

protected void rptSummary_ItemCreated(Object sender, RepeaterItemEventArgs e) {
    if (e.Item.ItemType == ListItemType.Footer) {
        e.Item.FindControl(ctrl);
    }
    if (e.Item.ItemType == ListItemType.Header) {
        e.Item.FindControl(ctrl);
    }
}

Si può prendere un riferimento sul controllo sull'evento ItemCreated, e quindi utilizzare in un secondo momento.

Trova controllo in Ripetitore (intestazione, punto, Piè di pagina)

public static class FindControlInRepeater
{
    public static Control FindControl(this Repeater repeater, string controlName)
    {
        for (int i = 0; i < repeater.Controls.Count; i++)
            if (repeater.Controls[i].Controls[0].FindControl(controlName) != null)
                return repeater.Controls[i].Controls[0].FindControl(controlName);
        return null;
    }
}

Questo è in VB.NET, appena si traducono in C #, se ne avete bisogno:

<Extension()>
Public Function FindControlInRepeaterHeader(Of T As Control)(obj As Repeater, ControlName As String) As T
    Dim ctrl As T = TryCast((From item As RepeaterItem In obj.Controls
                   Where item.ItemType = ListItemType.Header).SingleOrDefault.FindControl(ControlName),T)
    Return ctrl
End Function

E usarlo facile:

Dim txt as string = rptrComentarios.FindControlInRepeaterHeader(Of Label)("lblVerTodosComentarios").Text

Prova a farlo funzionare con piè di pagina, e gli elementi controlla troppo =)

Il modo migliore e pulito per fare questo è all'interno del Item_Created evento:

 protected void rptSummary_ItemCreated(Object sender, RepeaterItemEventArgs e)
        {
            switch (e.Item.ItemType)
            {
                case ListItemType.AlternatingItem:
                    break;
                case ListItemType.EditItem:
                    break;
                case ListItemType.Footer:
                    e.Item.FindControl(ctrl);
                    break;
                case ListItemType.Header:
                    break;
                case ListItemType.Item:
                    break;
                case ListItemType.Pager:
                    break;
                case ListItemType.SelectedItem:
                    break;
                case ListItemType.Separator:
                    break;
                default:
                    break;
            }
    }
private T GetHeaderControl<T>(Repeater rp, string id) where T : Control
{
    T returnValue = null;
    if (rp != null && !String.IsNullOrWhiteSpace(id))
    {
        returnValue = rp.Controls.Cast<RepeaterItem>().Where(i => i.ItemType == ListItemType.Header).Select(h => h.FindControl(id) as T).Where(c => c != null).FirstOrDefault();
    }
    return returnValue;
}

Trova e getta il controllo. (In base alla risposta del VB Piyey)

Per ItemDataBound

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Header)//header
    {
            Control ctrl = e.Item.FindControl("ctrlID");
    }
    else if (e.Item.ItemType == ListItemType.Footer)//footer
    {
            Control ctrl = e.Item.FindControl("ctrlID");
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top