views:

44

answers:

1

Hi I'm having a bit of an issue with a asp.net repeater

I'm building a categories carousel with the dynamic categories being output by a repeater. Each item is a LinkButton control that passes an argument of the category id to the onItemClick handler.

a page variable is set by this handler to track what the selected category id is....

public String SelectedID
    {
        get
        {

            object o = this.ViewState["_SelectedID"];
            if (o == null)
                return "-1"; 
            else
                return (String)o;
        }

        set
        {
            this.ViewState["_SelectedID"] = value;
        }
    }

problem is that i cant seem to read this value while iterating through the repeater as follows...

<asp:Repeater ID="categoriesCarouselRepeater" runat="server" 
                onitemcommand="categoriesCarouselRepeater_ItemCommand">
            <ItemTemplate>
            <%#Convert.ToInt32(Eval("CategoryID")) == Convert.ToInt32(SelectedID) ? "<div class=\"selectedcategory\">":"<div>"%>
 <asp:LinkButton ID="LinkButton1" CommandName="select_category" CommandArgument='<%#Eval("CategoryID")%>' runat="server"><img src="<%#Eval("imageSource")%>" alt="category" /><br />

</div>

</ItemTemplate>

</asp:Repeater>

calling <%=SelectedID%> in the item template works but when i try the following expression the value of SelectedID returns empty..

<%#Convert.ToInt32(Eval("CategoryID")) == Convert.ToInt32(SelectedID) ? "match" : "not a match"%>

the value is being set as follows...

protected void categoriesCarouselRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
    {

       SelectedID = e.CommandArgument.ToString();
}

Any ideas whats wrong here?

A: 

Within the categoriesCarouselRepeater_ItemCommand code you've shown, you're assigning the CommandArgument to a property called 'SelectedCategory'.

Should this not be assigning the property to the 'SelectedID' property instead?

** EDIT..

The problem I see is one of two scenarios:

1) You are not rebinding the repeater with each postback, and therefore the expression within your ItemTemplate is not being evaluated - The output from the repeater will remain unchanged with each postback.

OR

2) You are rebinding the repeater control with each postback, however, upon clicking on your LinkButton for the first time, the repeater control is re-binded PRIOR to the ItemCommand event handler firing, and therefore, the 'SelectedID' property has not been set until after the repeater has finished being output.

If you were to click on one of your LinkButtons a 2nd time, the previously selected ID would be in viewstate at the time of the repeater control contents being rendered, and therefore be one step behind in rendering which category has been clicked, and so on...

marcusstarnes
Sorry about that I was actualy playing around trying to use another value and coppied the wrong peice of code.It is in fact the SelectedID property thats causing the issue.I've edited the code snipit above*
carrot_programmer_3