user219725
user219725

Reputation:

repeater control using multiple Array List

I m using nested repeater repeater1> label1> repeater2> label2> data coming from arraylist(ar1) <%container.Dataitem> Textbox> data coming from two different arraylist??(ar2 and ar3)

Earilier I hv connected one arraylsit with it . It was working fine. but the problem is now I have 3 different arraylist for 2 different controls.

Three arraylist ar1,ar2,ar3

in Textbox i want id to come from ar1 name to come from ar2

and in label i want text from ar3.

I was using <%#container.Dataitem%> if I m using only one array list but if I use it three times it is not working.

In repeater2 i m doing label text=<%#container.Dataitem%> but I don't know how to bind data in repeater 2 to ar1,ar2,ar3.

Thanks,

PS

Upvotes: 0

Views: 5432

Answers (2)

user219725
user219725

Reputation:

Jim its working fine with one problem : Edited

asp:Repater Id="Repeater1" runat="Server">
ItemTemplate><%#Container.DataItem%>

 <asp:Repeater ID="Repeater2" runat="server">

                <ItemTemplate>
                <tr>  <td> <input id='<%# Eval("Key") %>' type="text" name='<%# Eval("Value") %>' /></td></tr>       

                      <th class="brochuretitle"><asp:Label runat="server" ID="Label2" Text='<%#Eval("Key")%>'></asp:Label></th>   

                </ItemTemplate>


        </asp:Repeater>

and ascx.cs

Dictionary> combined = new Dictionary>();

                Dictionary<string, string> inner = new Dictionary<string, string>(); 

              for (int j = 0; j < ar3.Count; j++) 

              { 
                  inner.Add(ar3[j].ToString(),ar4[j].ToString());
                //  inner.Add(subCategories[j].ToString(), textControlNames[j].ToString()); 
              }
              for (int k = 0; k < ar1.Count; k++)
              {

                  combined.Add(ar1[k].ToString(), inner);
              }


                ((Repeater)(rptrItem.FindControl("Repeater2"))).DataSource = combined;
                ((Repeater)(rptrItem.FindControl("Repeater2"))).DataBind();

Now How to tell that key in input is related to inner and key in label is related to combined?

Upvotes: 0

Jim Schubert
Jim Schubert

Reputation: 20357

The way I would handle this is to process your ArrayLists into some other collection and bind to that.

for instance, if your collections all have the same number of items:

Dictionary<string, KeyValuePair<string, string>> combined = 
           new Dictionary<string, KeyValuePair<string, string>>();
for(int i = 0; i < list1.Count; i ++)
{
    combined.Add(list1[i], new KeyValuePair(list2[i], list3[i]));
}

Then, handle the binding of the nested repeater in the itemdatabound event. This may seem like unnecessary processing, but it would make the logic a lot easier.

(to bind your TextBox, you'll have to manually find the control in the row and set the ID and Text in the ItemDataBound event)

Edit: Here is a working example of what I assume you're trying to do:

ASPX CODE:

<asp:Repeater ID="categoryRepeater" runat="server">
    <HeaderTemplate><dl></HeaderTemplate>
    <ItemTemplate>
    <dt><em><%# Eval("Key") %></em></dt>
    <dd>
        <asp:Repeater ID="nestedRepeater" runat="server" DataSource='<%# Eval("Value") %>' >
            <HeaderTemplate><ol></HeaderTemplate>
            <ItemTemplate>
                <li>
                    <asp:Label ID="lblSubCategory" 
                        runat="server" 
                        Text='<%# Eval("Key") %>' />
                    <asp:TextBox ID="txtInformation"
                        runat="server"
                        Text='<%# Eval("Value") %>' />
                </li>
            </ItemTemplate>
            <FooterTemplate></ol></FooterTemplate>
        </asp:Repeater>
    </dd>
    </ItemTemplate>
    <FooterTemplate></dl></FooterTemplate>
    </asp:Repeater>

Codebehind (Init Arrays and DataBind) :

        ArrayList categories = new ArrayList 
            { "Category", "Category 2", 
                "Category 3", "Category 4" };
        ArrayList subCategories = new ArrayList 
            { "SubCategory 1", "SubCategory 2", 
                "SubCategory 3", "SubCategory 4" };
        ArrayList textControlNames = new ArrayList 
            { "Enter 1", "Enter 2", "Enter 3", "Enter 4" };

        Dictionary<string, Dictionary<string, string>> combined =
                new Dictionary<string, Dictionary<string, string>>();
        for (int i = 0; i < categories.Count; i++)
        {
            Dictionary<string, string> inner = new Dictionary<string, string>();
            for (int j = 0; j < subCategories.Count;j++)
            {
                inner.Add(subCategories[j].ToString(),
                    textControlNames[j].ToString());
            }
            combined.Add(categories[i].ToString(), inner);
        }

        categoryRepeater.DataSource = combined;
        categoryRepeater.DataBind();

Notice that since you can't generate the same IDs for labels or textboxes, I haven't icluded an ItemDataBound event as I originally mentioned.

Upvotes: 0

Related Questions