views:

803

answers:

2

Hi,

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

+1  A: 

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.

Jim Schubert
i m kind of confused according to what u said so let say if my ascx file has thi Repeater1 repeater2 <td> <%Container.DataItem%></td> <th<input id=<%Container.DataItem%> name=<%Container.DataItem%>></th> /repeater2> Repeater1 and in ascx.cs if i assign repeater2 to combined. how I would be able to know which arraylist values to go there.My all arraylist are of same count But could you tell me in detail what you r trying to do over here
TSSS22
Can you provide a little more code in your original post? And perhaps examples of the data you're trying to bind? Also, why are you trying to manually set the ID of the textboxes? A problem you'll encounter there is that an ID must be unique for every control on the page. So, if you have 5 entries in ArrayList #2, and you're binding that to a control's ID, you'll only be able to use those values once, then you'll have to generate new IDs for the next Category.
Jim Schubert
Thanks Jim for explaining in detail about this . Your answer looks promising to me but I have only one doubt.In ascx file how you are assigning the values I meant . You have written <%# Eval("Key") %> and <%# Eval("Value") %> in label and text but I thought we can do this only if we are assessing from datasource ( key and Value are coming from datasource). In your ascx.cs file also u didn't mention abt them so I got this combined contains all the three arraylist but how to represent it in ascx .
TSSS22
As I mentioned above I was using <Container.DataItem> till now.But If i use this in ascx it would not tell which array list is for textbox which is for label.Yah and ID's are unique I have a code which is generating some unique values of Id's every time and putting in that arraylist.So that would not be an issue.
TSSS22
Key refers to the Dictionary.Key and Value refers to the Dictionary.Value. For the object being bound, Key is the string and Value is the inner dictionary. So, in the nested repeater, Key refers to inner.Key and value refers to inner.Value. Also, if your IDS are truly unique, the ArrayList from which you pull the IDs has to be larger than the other collections (or else Category1 ids will be the same as Category2, and therefore be invalid html)
Jim Schubert
was not able to explain here to so added answer to my question
TSSS22
A: 

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?

TSSS22
`DataBinder.Eval(((RepeaterItem)Container.Parent.Parent).DataItem,"Key")` will access the key of the parent repeater. The way this works is Container.Parent is the current Repeater. That parent is the row in the parent repeater. So, you cast that row and get the DataItem, then eval "Key". I hope that helps.
Jim Schubert
Also, you may want to look at using some other data structures. In your case, since you're access a parent item from the child, you may want to create a class that has a generic list of categories, and for each Category, have a subcategory and text item with a reference to the parent category. This way, persisting your data if needed will be a lot easier. It may even be easier to processing using XML and transforming your XML into your desired control structure.
Jim Schubert
I guess you got it wrong in parent repeater I don't want the value from Dictionary key value pair. Value in Parent repeater is coming from some other arraylist no relation to this one. Main is the subcategory one using different arraylist and as I hv shown in ascx.cs file it is connected to repeater2 only so I can't use the first method . I will try to do it through second method may be that works
TSSS22
Jim thanks for your help didn't think abt it before. I used Datatable and add three columns and put their value in it.its working absolutely fine.
TSSS22