locked
DropDownList in Repeater RRS feed

  • Question

  • User-645034270 posted

    I have a repeater that contains an DropDownList in the ItemTemplate. I am trying to populate the dropdownlist based on the id of the current repreater record. Is there a way to set the value so that I can pass it to the datasource?

     Thanks for any help.

        <asp:Repeater runat="server" ID="repItems">
          <ItemTemplate>
            <div style="width: 20%; float: left;">
              <asp:Image ID="imgItem" runat="server" ImageUrl='<%# "~/Handlers/Crop.ashx?item=" + Eval("ID") %>' />
              <h2><%# Eval("Name") %></h2>
              <asp:DropDownList runat="server" ID="ddlOptions"  />
            </div>
          </ItemTemplate>    
        </asp:Repeater>
     
     
     
    Tuesday, July 8, 2008 1:29 AM

Answers

  • User-1411293322 posted

    Hi,

    The simplest approach to solve the above requirement is to set data binding for the drop down at design time.

    1. As the drop down is placed inside the repeater it has access to the current repeater item. So all we need to do is create a function that will filter data for drop down based on the current id.

    2. Set data source for Repeater and then set data source for drop down to a filtering function. See the sample code below for simple demo.

    3. call databind method on the Repeater in the page load and it will bind drop down list automatically. 

    The aspx page:

      

    <form id="form1" runat="server">
        <div>        
            <asp:Repeater ID="Repeater1" runat="server">
            <ItemTemplate>
                <asp:Literal ID="myLiteral" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Column1") %>'></asp:Literal><br />
                <asp:DropDownList ID="DropDownList1" runat="server" DataSource='<%# FilterDropDownData((string)DataBinder.Eval(Container.DataItem, "Column1")) %>' DataTextField="Column2">
                </asp:DropDownList><hr />            
            </ItemTemplate>
            </asp:Repeater>
        </div>
        </form>
     

    The code behind:

     

    public partial class _Default : System.Web.UI.Page
    {
        DataTable myRepeaterData;
        DataTable myDropDownData;
    
        protected void Page_Load(object sender, EventArgs e)
        {
            myDropDownData = PopulateDropDownData();
            myRepeaterData = PopulateRepeaterData();
            
            Repeater1.DataSource = myRepeaterData;
            Repeater1.DataBind();        
        }
        public DataTable PopulateRepeaterData()
        {
            DataTable myTable = new DataTable();
            DataRow myRow;
    
            myTable.Columns.Add("Column1", typeof(String));
    
            myRow = myTable.NewRow();
            myRow["Column1"] = "Value11";
            myTable.Rows.Add(myRow);
    
            myRow = myTable.NewRow();
            myRow["Column1"] = "Value12";
            myTable.Rows.Add(myRow);
    
            return myTable;
        }
        public DataView FilterDropDownData(string id)
        {
            DataView myView = new DataView(myDropDownData);
            myView.RowFilter = "Column1 = '" + id + "'";
            return myView;
        }
        public DataTable PopulateDropDownData()
        {
            DataTable myTable = new DataTable();
            DataRow myRow;
    
            myTable.Columns.Add("Column1", typeof(String));
            myTable.Columns.Add("Column2", typeof(String));
    
            myRow = myTable.NewRow();
            myRow["Column1"] = "Value11";
            myRow["Column2"] = "Value1111";
            myTable.Rows.Add(myRow);
    
            myRow = myTable.NewRow();
            myRow["Column1"] = "Value11";
            myRow["Column2"] = "Value1112";
            myTable.Rows.Add(myRow);
    
            myRow = myTable.NewRow();
            myRow["Column1"] = "Value12";
            myRow["Column2"] = "Value1211";
            myTable.Rows.Add(myRow);
    
            myRow = myTable.NewRow();
            myRow["Column1"] = "Value12";
            myRow["Column2"] = "Value1212";
            myTable.Rows.Add(myRow);
    
            return myTable;
        }
    }
      
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 8, 2008 7:03 AM

All replies

  • User-645034270 posted

     Thanks for the links, but I am not sure I am making myself clear. The examples were more for getting a value in the current repeater record. I am trying to pass that value to the datasource of the ddl, as it is populating based on current record. The datasource is a method is a class library that is returning a datatable. The method needs an id parament which would be the id of the current repeater record.

     Thanks again.

    Tuesday, July 8, 2008 3:23 AM
  • User-1034726716 posted

    You can populate your DDL on ItemDataBound event of Repeater.. see below 

    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                DropDownList dll = (DropDownList )e.Item.FindControl("ddOptions");
                if (dll != null)
                {
                    string ID = //Grab the id here
                    DataTable dt = // call the method from your Class library that returns a DataTable and pass the value of ID as the paramter

                    if (dt.Rows.Count > 0)
                    {
                       dll.DataSource = dt;
                       dll.DataTextField = "ColumnName";
                       dll.DataValueField = "ColumnName";
                       dll.DataBind();
                    }
                }

            }

    }

    Tuesday, July 8, 2008 4:07 AM
  • User-105798415 posted

    Hello durilai,

     

    I presume you are bound the dropdownlist dynamically, so I will provide you with a bit of a drastic solution. You can add a hidden label to the itemtemplate of the repeater control and set its text value to the id retrieved from the datasource, like this...

     

    <asp:Repeater runat="server" ID="repItems">
          <ItemTemplate>
            <div style="width: 20%; float: left;">
              <asp:Image ID="imgItem" runat="server" ImageUrl='<%# "~/Handlers/Crop.ashx?item=" + Eval("ID") %>' />
              <h2><%# Eval("Name") %></h2>
              <asp:DropDownList runat="server" ID="ddlOptions"  />
              <asp:Label ID="lblHidden" runat="server" Visible="false" Text='<%# Eval("ID") %>'></asp:Label>
            </div>
          </ItemTemplate>   
        </asp:Repeater>

     then when binding the dropdownlist you can retrieve the label's text which will contain the id you need to pass it in to your databinding method from the control collection of the repeater, like this...

     

    string paramID = ((Label)repItems.Controls[0]).Text;

     so at this point you have retrieved the id you need to pass in to your datasource.

     

    Hope it helped,

     

    Leo. 

     

    Tuesday, July 8, 2008 4:29 AM
  • User-1411293322 posted

    Hi,

    The simplest approach to solve the above requirement is to set data binding for the drop down at design time.

    1. As the drop down is placed inside the repeater it has access to the current repeater item. So all we need to do is create a function that will filter data for drop down based on the current id.

    2. Set data source for Repeater and then set data source for drop down to a filtering function. See the sample code below for simple demo.

    3. call databind method on the Repeater in the page load and it will bind drop down list automatically. 

    The aspx page:

      

    <form id="form1" runat="server">
        <div>        
            <asp:Repeater ID="Repeater1" runat="server">
            <ItemTemplate>
                <asp:Literal ID="myLiteral" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Column1") %>'></asp:Literal><br />
                <asp:DropDownList ID="DropDownList1" runat="server" DataSource='<%# FilterDropDownData((string)DataBinder.Eval(Container.DataItem, "Column1")) %>' DataTextField="Column2">
                </asp:DropDownList><hr />            
            </ItemTemplate>
            </asp:Repeater>
        </div>
        </form>
     

    The code behind:

     

    public partial class _Default : System.Web.UI.Page
    {
        DataTable myRepeaterData;
        DataTable myDropDownData;
    
        protected void Page_Load(object sender, EventArgs e)
        {
            myDropDownData = PopulateDropDownData();
            myRepeaterData = PopulateRepeaterData();
            
            Repeater1.DataSource = myRepeaterData;
            Repeater1.DataBind();        
        }
        public DataTable PopulateRepeaterData()
        {
            DataTable myTable = new DataTable();
            DataRow myRow;
    
            myTable.Columns.Add("Column1", typeof(String));
    
            myRow = myTable.NewRow();
            myRow["Column1"] = "Value11";
            myTable.Rows.Add(myRow);
    
            myRow = myTable.NewRow();
            myRow["Column1"] = "Value12";
            myTable.Rows.Add(myRow);
    
            return myTable;
        }
        public DataView FilterDropDownData(string id)
        {
            DataView myView = new DataView(myDropDownData);
            myView.RowFilter = "Column1 = '" + id + "'";
            return myView;
        }
        public DataTable PopulateDropDownData()
        {
            DataTable myTable = new DataTable();
            DataRow myRow;
    
            myTable.Columns.Add("Column1", typeof(String));
            myTable.Columns.Add("Column2", typeof(String));
    
            myRow = myTable.NewRow();
            myRow["Column1"] = "Value11";
            myRow["Column2"] = "Value1111";
            myTable.Rows.Add(myRow);
    
            myRow = myTable.NewRow();
            myRow["Column1"] = "Value11";
            myRow["Column2"] = "Value1112";
            myTable.Rows.Add(myRow);
    
            myRow = myTable.NewRow();
            myRow["Column1"] = "Value12";
            myRow["Column2"] = "Value1211";
            myTable.Rows.Add(myRow);
    
            myRow = myTable.NewRow();
            myRow["Column1"] = "Value12";
            myRow["Column2"] = "Value1212";
            myTable.Rows.Add(myRow);
    
            return myTable;
        }
    }
      
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 8, 2008 7:03 AM
  • User-645034270 posted

     Thanks nitinbadole,

     I modified my version a bit, but you got me on the right track. I had a method returning a datatable already, so I used that instead of the dataview. Same result.

    Thanks for all the help! Problem Solved.

    ASPX

    <asp:Repeater runat="server" ID="repItems">
      <ItemTemplate>
        <div style="width: 20%; float: left;">
          <asp:HyperLink ID="imgItem" runat="server" NavigateUrl='<%# "~/Item.aspx?item=" + Eval("ID") %>' ImageUrl='<%# "~/Handlers/Crop.ashx?item=" + Eval("ID") %>' />
          <asp:HyperLink ID="lnkItem" runat="server" NavigateUrl='<%# "~/Item.aspx?item=" + Eval("ID") %>' Text='<%# Eval("Name") %>' CssClass="ItemName" />
          <asp:DropDownList runat="server" ID="ddlOptions" DataSource='<%# GetOptions((int)Eval("ID")) %>' DataTextField="Name" DataValueField="ID">
          </asp:DropDownList>
        </div>
      </ItemTemplate>
    </asp:Repeater>   

    CS

    protected void Page_Load(object sender, EventArgs e)
    {
      repItems.DataSource = "repeater datasource";
      repItems.DataBind();
    }
    
    public DataTable GetOptions(int iItem)
    {
      DataTable dtOptions = CLASSLIBRARY.METHOD_RETURNS_DATATABLE(iItem);
      return dtOptions;
    }
      
    Tuesday, July 8, 2008 4:00 PM