87605

asp:DropDownList selectedvalues in multiple selection list

Question:

I have asp:DropDownList wich is populated from SQLdatasource and I need to set selected items on select list (asp:DropDownList or asp:DropDownList).

Here is my code:

<asp:SqlDataSource runat="server" ID="sqlAihealueet" ConnectionString="Server=SQL2008;Database=Data;UID=123;PWD=123" SelectCommand="SELECT TOP (100) PERCENT valikkoaktiivinen, alue, id, otsikko, osa1b, osa2b, osa3b, osa4b FROM dbo.FiValikko"></asp:SqlDataSource> <asp:DropDownList name="aihevalinta" id="aihevalinta" multiple="true" DataSourceid="sqlAihealueet" DataValueField="id" DataTextField="otsikko" class="populate placeholder" style="width:450px; font-size:11px" runat="server"> </asp:DropDownList>

On this page I have all other fields set up on codebehind like this:

vapaaselite.text = drRow.Item("vapaaselitetxt").ToString

How should I make that multiple selected ones to that select list on?

I have tryed this (aihealueet data = "9682,9683"):

aihevalinta.SelectedValues = drRow.Item("aihealueet")

not working, gives me error: SelectedValues' is not a member of 'System.Web.UI.WebControls.DropDownList

Is there way I could make selecteced items straight from sql datasource, from SQLquery? I could mark them easily on SQLquery.

...or how Do I do this with asp:Listbox? I tryed same kind of aproach. I only can get one item as selected. If I try to make other one selected too the first selected ones select state disappears.

aihevalinta.SelectedValue = "9565" aihevalinta.SelectedValue = "9566"

<img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/O1kkE.jpg" data-original="https://i.stack.imgur.com/O1kkE.jpg" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />

The best solution for me would be to mark selected one straigt from SQldatasource (same where the list comes form). data would be like this:

<img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/gHoqx.jpg" data-original="https://i.stack.imgur.com/gHoqx.jpg" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />

Answer1:

If I understand correctly you would like to fill your DropDownList with multiple values, something like this: <img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/gvE69.jpg" data-original="https://i.stack.imgur.com/gvE69.jpg" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />

It is possible, I do it in this way:

Markup:

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"> </asp:DropDownList>

Code behind:

if (!IsPostBack) { SqlConnection con = new SqlConnection(strConnString); con.Open(); str = "select * from Pagamenti ORDER BY [Data] DESC"; com = new SqlCommand(str, con); sqlda = new SqlDataAdapter(com); ds = new DataSet(); sqlda.Fill(ds, "Pagamenti"); string spacer = " --|-- "; for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { DropDownList1.Items.Add(string.Format("{0:Id ####}", ds.Tables[0].Rows[i]["Id"]) + spacer + Convert.ToDateTime(ds.Tables[0].Rows[i]["Data"]).ToString("dd/MM/yyyy") + spacer + string.Format("{0:R #.###,##}", (ds.Tables[0].Rows[i]["Somma"]))); } con.Close(); }

Let us know if this is what you where looking for.

Answer2:

I had to make work around to my problem. I'am sure there have to some better way to do this.

My work around were done using some jquery script and non visible asp.net textbox field (valinnatlista).

<script> var data= $("#valinnatlista").val() ; var dataarray=data.split(","); $("#aihevalinta").val(dataarray); $("#aihevalinta").multiselect("refresh"); </script>

Recommend

  • Yii2: Using Kartik Depdrop Widget?
  • Creating UDF with VSTO in Excel
  • How to set an entity field that does not exist on the table but does exists in the raw SQL as an ali
  • how to query for min or max inet/cidr with postgres
  • How to Add Polymorphic Comments to Feed?
  • Xaml, wpf image position and crop issue
  • sweetalert2 inputoptions from file in select example
  • Getting different value with placeholder over CPU/GPU
  • Floated image with variable width and heading with background image
  • Extract zip entries to another Zip file
  • Using a canvas object in a thread to do simple animations - Java
  • GridView breaks while scrolling
  • How to change the font size of a single index for UISegmentedControl?
  • Time complexity of a program which involves multiple variables
  • Custom validator control occupying space even though display set to dynamic
  • How do I alternate colors in Flat List (React Native)
  • Listbox within Listbox and scrolling trouble in Windows Phone 7 Silverlight
  • Atlas images wrong size on iPad iOS 9
  • Highlight and Bold text in JTextPane
  • Change multiple background-images with jQuery
  • Xamarin Forms - UWP Fonts
  • How can I send an e-mail from a vbs script
  • Sails.js/waterline: Executing waterline queries in toJSON function of a model?
  • Android screen density dpi vs ppi
  • Can Jackson SerializationFeature be overridden per field or class?
  • Why doesn't :active or :focus work on text links in webkit? (safari & chrome)
  • DirectX11 ClearRenderTargetViewback with transparent buffer?
  • How to apply VCL Styles to DLL-based forms in Inno Setup?
  • htaccess rewriting URLs with multiple forward slashes
  • Change an a tag attribute in JavaScript based on screen width
  • How to show dropdown in excel using jrxml (jasper api)?
  • Display Images one by one with next and previous functionality
  • jquery mobile loadPage not working
  • Web-crawler for facebook in python
  • Unanticipated behavior
  • A cron job substitute?
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • how does django model after text[] in postgresql [duplicate]
  • python draw pie shapes with colour filled