0

How can I get the value of the selected item inside the repeater object that I filled from the database, again in the dropdownlist object that I filled from the database?

How can I transfer the values ​​of the items I selected from these dropdownlist objects to a listbox?

For example, there are 3 dropdownlists in the repeater. I want to transfer the values ​​selected from these dropdownlists to the listbox when I press the button.

Thank you...

Ilan.aspx

<asp:Repeater ID="rptNitelikler" runat="server" OnItemDataBound="rptNitelikler_ItemDataBound">
     <ItemTemplate>
          <div class="control-group form-group">
               <label class="form-label text-dark"><%#Eval("nitelik") %></label>
               <asp:Label ID="id" runat="server" Visible="false" Text='<%#Eval("nid") %>'></asp:Label>
               <asp:Label ID="lblItem" Visible="false" runat="server" Text="Label"></asp:Label>
               <asp:DropDownList ID="ddl" CssClass="form-control select2-show-search" data-placehodler="Seç" Width="100%" runat="server">
               </asp:DropDownList>
          </div>
     </ItemTemplate>
</asp:Repeater>

Ilan.aspx.cs

    private void NitelikGetir()
    {
        int ana, alt, kat;
        ana = int.Parse(Session["AnaId"].ToString());
        alt = int.Parse(Session["AltId"].ToString());
        kat = int.Parse(Session["KatId"].ToString());
        using (BayUniEntities ent = new BayUniEntities())
        {
            var birlestir = (from nk in ent.NITELIKKATEGORI
                             join an in ent.ANANITELIKLER
                             on nk.AnaNitelikId equals an.AnaNitelikId
                             where nk.AnaKategoriId==ana && nk.AltKategoriId==alt && nk.IlanKategoriId==kat
                             select new { nitelik=an.AnaNitelik, nid=an.AnaNitelikId }).ToList();


            rptNitelikler.DataSource = birlestir;
            rptNitelikler.DataBind();
        }
    }

    protected void rptNitelikler_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        int id = Convert.ToInt32(DataBinder.Eval(e.Item.DataItem, "nid"));
        DropDownList selectList = e.Item.FindControl("ddl") as DropDownList;
        if (e.Item.ItemType==ListItemType.Item || e.Item.ItemType==ListItemType.AlternatingItem)
        {
            using (BayUniEntities ent = new BayUniEntities())
            {
                var birlestir = (from an in ent.ALTNITELIKLER
                                 where an.AnaNitelikId == id
                                 select an).ToList();

                selectList.DataSource = birlestir;
                selectList.DataTextField = "AltNitelik";
                selectList.DataValueField = "AltNitelikId";
                selectList.DataBind();
            }
        }
    }
  • You need to set the `SelectedIndex` or the `SelectedValue` property of the drop down after binding it. – user1429080 Jun 18 '20 at 08:50
  • You do want to set the selected value of the dropdown, right? In your question you say "_How can I get the value of the selected item_" but it looks like you need to set it instead. – Cleptus Jun 18 '20 at 08:56
  • Sorry. I update tittle – Mehmet Akif Jun 18 '20 at 09:00
  • what do you wanna do? When the user selects an item from the dropdownlist, then make something happen? – Homungus Jun 18 '20 at 09:08
  • @Homungus For example, there are 3 dropdownlists in the repeater. I want to transfer the values ​​selected from these dropdownlists to the listbox when I press the button. – Mehmet Akif Jun 18 '20 at 09:21
  • @MehmetAkif please edit your question and add your last comment to it :) – Homungus Jun 18 '20 at 09:31

1 Answers1

0

To get the selected values from your dropdownlists you need to iterate over the items in the repeater. In your button-handler-code do something like this:


foreach (RepeaterItem repeaterItem in rptNitelikler.Items)
{
    DropDownList ddl = (DropDownList)repeaterItem.FindControl("ddl");
    if (ddl != null)
    {
        string selectedValue = ddl.SelectedValue;
        // insert code to add value to listbox here.

        string selectedText = ddl.SelectedItem.Text;
        // Insert code to add Text to listbox here.
    }
}

Homungus
  • 1,068
  • 1
  • 10
  • 17
  • thanks for answer. But When I add Listbox, always add "1" and "3". If you want, I'll send you screenshot. – Mehmet Akif Jun 18 '20 at 10:01
  • what do you want to add? Text of the selected item in dropdown or the value? Value is AltNitelikId, text is AltNitelik – Homungus Jun 18 '20 at 10:06
  • I want add value. The same values ​​are returned in your code. – Mehmet Akif Jun 18 '20 at 10:11
  • please edit your question and specify what you want exactly - so one can see the difference between expected output and actual output. – Homungus Jun 18 '20 at 11:09
  • Look. There is one repeater object. There is one dropdownlist object inside.I fill both of these objects from the database. I fill the attributes of a product in the dropdownlist object.The more quality there is in the database, the more dropdownlists it brings to the web page.When I press the button, I want to save the values ​​I selected from these dropdownlist objects to one listbox object.For example, for Notebook product, qualities such as Ram, processor, color will come. I will choose the ram processor color, but I will save their value in the listbox. I think it was pretty clear. – Mehmet Akif Jun 18 '20 at 12:19