views:

113

answers:

1

Hi,

I have been working for a couple days trying to figure out how to do this. Working in C# I have a List of data being pulled from various database tables. I want to create different DropDownLists from this data that will persist across PostBacks. One way I was able to tinker through this was to parse the data into List> and nest two repeaters inside each other.

<asp:Repeater ID="rpt3" runat="server">
        <ItemTemplate>
            <select>
            <asp:Repeater DataSource="<%#Container.DataItem%>" runat="server">
                <ItemTemplate>
                    <option><%#Container.DataItem%></option>
                </ItemTemplate>
            </asp:Repeater>
            </select>
        </ItemTemplate>
    </asp:Repeater>

The problem with this is that the SelectedValues do not save in the ViewState.

Is there some easy way to do this I am missing?

Thanks a lot

+1  A: 

I would recommend using the DropDownList control to display your individual results.

In the front end, you need to simply declare your drop down list as such:

<asp:DropDownList id="ddl1" runat="server">
</asp:DropDownList>


Your backend code should contain a method that binds the datasource to the drop down list. ex:

this.ddl1.DataSource = ds; //Assuming that you are using a DataSet called ds.
this.ddl1.DataTextField = "TextColumnName"; //The column name or property name you want to use as the text data
this.ddl1.DataValueField = "ValueColumnName"; //The column name or property name you want to use as the value data
this.ddl1.DataBind();

Call this in the backend only when binding is needed, such as on the initial page load, or when the datasource needs to be refreshed.

If you want a repeater of drop down lists, simply do the following:

<asp:repeater id="rpt1" runat="server">
   <ItemTemplate>
         <asp:DropDownList id="ddl1" runat="server">
         </asp:DropDownList>
   </ItemTemplate>
</asp:repeater>


Bind your datasource that contains the collection of values you would want for each DDL to the repeater and within the repeater's OnItemDatabound event, bind the data item or source to the dropdown list for each repeater item.

If you want your choices to persist, wrap the all of the binding logic into a method that's only called on Page_Load inside of an if statement that checks if the page is in PostBack.

if(!IsPostBack) { //Add method here }

The Viewstate should take care of the rest.

jlech
Thanks for the help. If I was to use a repeater, do you have any idea how I might also keep the dropdowns limited to 3 per row?
rpf3
Just to make sure I understand correctly, you want a repeating control that, for each row, displays anywhere from 1 to 3 drop down lists?This is what I envision you are trying to achieve:<--Begin Repeater--> <> <--DDL--> <--DDL--> <--DDL--> <> <> <--DDL--> <> <> <--DDL--> <--DDL--> <> . . .<-- End Repeater-->It's a little crude, but is this the kind of output you're going for?
jlech
Thats the hope. Basically I have a table that will contain all the DDLs. These DDLs are filter options for the dataset that gets displayed below them. I don't want them all in one row because then they will get crunched together since I don't know going in how many there will be. I also don't want them to be one per row because I think this will be unintuitive.
rpf3