Skip to posts
  • About

Win Web Hosting

Web Hosting Information
  • Home
  • ASP.NET
  • Cloud Hosting
  • Dedicated Servers
  • Linux Web Hosting
  • Microsoft AJAX
  • Microsoft Siverlight
  • Windows Web Hosting

How to bind a list of items in a dropdownlist based on the selection from another dropdownlist in asp.net?

May 24, 2010 James 2 comments
ASP.NET Another, ASP.NET, based, bind, dropdownlist, from, items, list, Selection

Comments

Leave a comment Trackback
  • Written by GT about 2 years ago.

    I assume you are binding that list to a SqlDataSource or something like that?

    If this is the case what you can do is select “Custom Query” when creating your datasource, and within the control, you can then do something such as

    “SELECT field1 from table2 where field2=@MYVAR”

    What this does is allow you to then specify @MYVAR as a parameter, you are then prompted to choose what MYVAR is, select “Control” and the select DropDownList1′s selected value.

    You can then bind that to DropDownList2, once you have done that you can trigger a DataSource2.Databind() to refresh the data

    EDIT : In case you didnt use the control, i thought I would update with the manual code:

    Dim myConnection As OleDbConnection
    Dim myCommand As OleDbCommand
    Dim myReader As OleDbDatareader
    Dim strSQL As String

    myConnection = New OleDbConnection(“yourconnectionstring”)

    strSQL=”SELECT field1,field2 from table2 where field2=’” & datalist1.selectedvalue.tostring & “‘”

    myCommand = New OleDbCommand(strSQL, myConnection)

    myConnection.Open()
    myReader = myCommand.ExecuteReader()
    While myReader.Read()
    datalist2.Items.Add(new ListItem(myReader.GetString(1).ToString, myReader.GetString(0).ToString))
    End While
    myreader.close()
    myConnection.Close()

    Obviously those apostrophes “‘” and .GetStrings may vary depending on your record types :)

  • Written by Smutty about 2 years ago.

    In DropDownList1:
    - Set the DataTextField and DataValueField properties of
    - Set the AutoPostBack property of DropDownList1 to true.

    - Handle the OnSelectedIndexChanged in your code behind to read the SelectedValue of the Item selected of DropDownList1 and use this SelectedValue to retrieve the DataSource of DropDownList2.

    Hope this helps.

Comment Pages:

Leave a Comment

Cancel Reply

You must be logged in to post a comment.

RSS

Win Web Hosting Home Page

Cheap Windows Hosting

 

February 2012
M T W T F S S
« Jan    
 12345
6789101112
13141516171819
20212223242526
272829  

Archives

  • February 2012 (24)
  • January 2012 (92)
  • December 2011 (163)
  • November 2011 (205)
  • October 2011 (339)
  • September 2011 (367)
  • August 2011 (367)
  • July 2011 (322)
  • June 2011 (338)
  • May 2011 (358)
  • April 2011 (94)
  • March 2011 (40)
  • February 2011 (36)
  • January 2011 (41)
  • December 2010 (30)
  • November 2010 (40)
  • October 2010 (41)
  • September 2010 (52)
  • August 2010 (150)
  • July 2010 (165)
  • June 2010 (159)
  • May 2010 (238)
  • April 2010 (248)
  • March 2010 (202)
  • February 2010 (576)

Recent Posts

  • Video scrubbing control with Silverlight – Tips and Tricks
  • ASP.NET Styles More…
  • MIXMASH (Featuring WPF & its little brother Silverlight)
  • ASP.NET TreeList – How to Delete Selected Nodes
  • [howto] animations in silverlight 2.0 with Expression Blend

Tags

.NET 2010 about AJAX Application ASP.NET best Business cheap cloud Company Computing Control Create data Dedicated Development Free from help Host hosting India Internet Introduction Linux Microsoft more need Page Part private Server servers service Services Silverlight Site Software Source Tutorial Using Video website Windows

Categories

  • ASP.NET
  • Cloud Hosting
  • Dedicated Servers
  • Linux Web Hosting
  • Microsoft AJAX
  • Microsoft Siverlight
  • Windows Web Hosting

Meta

  • Register
  • Log in
  • Valid XHTML
Powered by WordPress Web Design by SRS Solutions © 2012 Win Web Hosting