views:

840

answers:

4

I have one control named thumbviewer inside repeater. I want to set its imageurl in code. Currently it's done in aspx itself as

<asp:Repeater ID="Repeater1" runat="server" >
                    <ItemTemplate>
                        <span style="padding:2px 10px 2px 10px">

                            <bri:ThumbViewer Id="Th1"  runat="server" ImageUrl='<%# Eval("Name", "images/{0}") %>' Height="100px" Width="100px"/>
                        </span>
                    </ItemTemplate>
                </asp:Repeater>

How can i set ImageUrl in code?

A: 

You need to find the repeater and then look through the controls:

I do a similar thing here and load a control into a placeholder inside of a repeater..

        if (ResultRepeater != null && ResultRepeater.HasControls())
        {
            foreach (Control oControl in ResultRepeater.Controls)
            {
                if (oControl != null && oControl is RepeaterItem)
                {
                    PlaceHolder oSuggestMorePlaceholder = (PlaceHolder) oControl.FindControl("SuggestMorePlaceholder");

                    if (oSuggestMorePlaceholder != null)
                    {
                        SuggestMoreTabbedControl oTabbedControl = (SuggestMoreTabbedControl) Page.LoadControl("controls/SuggestMoreControl.ascx");
                        if (oTabbedControl != null)
                        {
                            oSuggestMorePlaceholder.Controls.Add(oTabbedControl);
                        }
                    }
                }
            }
        }
Gordon Carpenter-Thompson
+1 thanks for this solution
waqasahmed
Dude, you need to check out the FindControl() method
nialljsmith
Isn't FindControl() recursive? Kind of expensive if it is. Better off using it in one of the onitemdatabound event solutions suggested below.
Phil
http://www.codinghorror.com/blog/archives/000307.htmlNo, it's not recursive unfortunately...
Gordon Carpenter-Thompson
I think you are mistaken, @Gordon... FindControl() *is* recursive. Look at the comments to Jeff's post (CodingHorror) if you don't believe me.
Cerebrus
+1  A: 

I would recommend you to use repeater ItemDataBound event here. Here an example of how to use it.

Ramiz Uddin
+2  A: 

Your repeater has a onitemdatabound event.

<asp:Repeater ID="Repeater1" runat="server" onitemdatabound="Repeater1_ItemDataBound"></asp:Repeater>

In your code behind you can have an Event handler called

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    // ensure that we only look in the item template for our control
    if(  e.Item.ItemType == ListItemType.Item)
    {
         ThumbViewer tv = (ThumbViewer)e.Item.FindControl("Th1");
         tv.ImageUrl = "images/"+((<The object type you are binding>)e.Item.DataItem).Name;
    }
}

This is the most orthodox way to access data being bound to a repeater. IMHO

HTH

nialljsmith
According to code i updated what i have to write in place of Theobject type you are binding.Pls help
Just to be clear, the object that is being bound to the repeater has a Name property. When you are casting the e. Item.DataItem object to the desired type,it should look something like this:MyType myCoolType = (MyType)e.Item.DataItem;that's the basic way.but to shorten this code you can writetv.ImageUrl = ((MyType)e.Item.DataItem).Name;
nialljsmith
+5  A: 
protected void rpter_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        ThumbViewControl control = e.Item.FindControl("Th1") as ThumbViewControl;
        if (control != null)
        {
           control.ImageUrl = "";
        }
    }
}

and on the aspx

<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="rpter_ItemDataBound" >
                    <ItemTemplate>
                        <span style="padding:2px 10px 2px 10px">

                            <bri:ThumbViewer Id="Th1"  runat="server" Height="100px" Width="100px"/>
                        </span>
                    </ItemTemplate>
</asp:Repeater>

Is how I would personally do it.

If you wish to get the data for it, at that point, I believe e.Item.DataItem (or something similar) get its.

Cheers,

T

tim
snap! Fair play for adding the checks for null. Thats useful practice to get into, even if you're 100% the control will be there!
nialljsmith
it will effectively check for if the control is there, and if a control is there by that id, that is of type "ThumbViewControl" or whatever, so it's kind of two checks in one.I do love "as" :)
tim