Question : Datalist reset all its items when OnItemCommand is fired

I had worked on datalist which has a another datalist inside it the nested list consists of colour image buttons now when someone click on any of the buttons the OnItemCommand is firing and updating the data of both parent and child items but i want the other items in the list to mantain their state, but the other items are also reseting to initial state

Thanks
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
protected void Swatches_ItemCommand(Object sender, DataListCommandEventArgs e)
    {
        if (e.CommandName == "Select")
        {
            string part = e.CommandArgument.ToString();

            foreach (DataListItem parentItem in thumbRotatoralt.Items)
            {
                Label lblPart = (Label)parentItem.FindControl("lblPart");
                Label lblproductcode = (Label)parentItem.FindControl("lblproductcode");

                if (lblPart.Text == part)
                {
                    Image img1 = (Image)parentItem.FindControl("img1");
                    Label lblPrice = (Label)parentItem.FindControl("lblPrice");

                    DataTable DTColourFetch = new DataTable();
                    DTColourFetch = LoadColourInfo(part, ColourCode, "GetAllColorSwatchesWithMin");

                    if (DTColourFetch.Rows.Count > 0)
                    {
                        string Image1 = DTColourFetch.Rows[0]["Imagename"].ToString();



                        lblPrice.Text = FormatePrice(price, presaleprice);

                        img1.ImageUrl = FormatImageURL(Image1);


                        string pnames = lblname.Text;
                        string ProductName = pnames + " " + getfirstcolourname;
                        lblname.Text = ProductName.Trim();
                    }
                }
            }
        }
    }


 <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:DataList ID="thumbRotatoralt" runat="server" OnItemDataBound="ParentList" OnItemCommand="AddToBasket_ItemCommand">
                <ItemTemplate>
                    <div class="ItemListInnerstyle">
                        <table>
                            <tr>
                                <td colspan="2">
                                   <a class="ItemListlinkUnderline" cssclass="relateditemdescr2pbuttons" href="javascript:void(window.openRadWindow('<%#DataBinder.Eval(Container.DataItem,"Part")%>', '<%=ALTCatID %>'))">
                                    <asp:Label ID="lblname" CssClass="ItemListDescr" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.ModelName") %>' />
                                    </a>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <a class="ItemListlinkUnderline" cssclass="relateditemdescr2pbuttons" href="javascript:void(window.openRadWindow('<%#DataBinder.Eval(Container.DataItem,"Part")%>', '<%=ALTCatID %>'))">
                                        <asp:Image ID="imgwebsite" runat="server" CssClass="ItemListMainImg" BorderWidth="0"
                                            ImageUrl='<%# FormatImageURL(DataBinder.Eval(Container.DataItem, "ImageName").ToString()) %>' />
                                    </a>
                                    <br />
                                    <asp:DataList DataKeyField="Sequence" ID="DLColour" runat="server" RepeatDirection="Horizontal"
                                        OnItemCommand="Swatches_ItemCommand" SelectedItemStyle-BorderColor="Black" SelectedItemStyle-BorderStyle="Solid"
                                        SelectedItemStyle-BorderWidth="2px" CellPadding="2" CellSpacing="2">
                                        <SelectedItemStyle BorderColor="Black" BorderWidth="2px" BorderStyle="Solid"></SelectedItemStyle>
                                        <ItemTemplate>
                                            <asp:ImageButton ID="imgcolourbtn" runat="server" CssClass="ItemListSwatchImg" ImageUrl='<%# RtnValue(DataBinder.Eval(Container.DataItem, "colourcode11").ToString(), DataBinder.Eval(Container.DataItem, "Swatchpart").ToString()) %>'
                                                ToolTip="Click on colour to view items" CommandName="Select" CommandArgument='<%# Eval("part") %>'
                                                BorderWidth="1px" BorderColor="#999999" BorderStyle="Solid" />
                                        </ItemTemplate>
                                    </asp:DataList>
                                </td>
                                <td>
                                    <asp:Label ID="lblproductcode" CssClass="ItemListDescr" runat="server" Visible="true"
                                        Text='<%# DataBinder.Eval(Container, "DataItem.Part") %>' />
                                    <br />
                                    <asp:Label ID="lblPrice" CssClass="ItemListPrice" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Price") %>' />
                                    <br />
                                    <asp:TextBox ID="qty" Text="1" runat="server" Width="20px"></asp:TextBox>
                                    <asp:Button ID="btnCart" runat="server" CssClass="pbuttonssmal" Text="Add to Basket"
                                        CommandName="AddItemToBasket" />
                                    <br />
                                    <a class="ItemListlinkUnderline" cssclass="relateditemdescr2pbuttons" href="javascript:void(window.openRadWindow('<%#DataBinder.Eval(Container.DataItem,"Part")%>', '<%=ALTCatID %>'))">
                                        More Details </a>
                                </td>
                            </tr>
                        </table>
                    </div>
                </ItemTemplate>
            </asp:DataList>
        </ContentTemplate>
    </asp:UpdatePanel>

Answer : Datalist reset all its items when OnItemCommand is fired

You didn't post the part where you bind the data to the datalist, but my first guess would be that the binding always takes place. Try putting it inside a if-statement:
if(!Page.IsPostback)
{
thumbRotatoralt.DataSource = datasource;
thumbRotatoralt.DataBind();
}
Random Solutions  
 
programming4us programming4us