Reputation: 185
I have a DropDownList that pulls values from a table in sql server. I want the dropdownlist to populate the list of values based on a selection by the user but also display the contents of the rest of my table in case they need to change something. For ex:
ddl1 has values of: 1, 2, 3, 4, 5 user selects 5, so the value displayed in ddl2 is five, but if you exand the ddl2 you will also see the values of one, two, three, four...
This is the code in my load event...
If Not IsPostBack Then
result = dal.dbConnect(DAL.dbType.SqlServer, ConfigurationManager.AppSetting("SQLServerConnection"))
If result = "Successful" Then
Try
dt = dal.ExecuteSelectStoredProc(DAL.dbType.SqlServer, "StoredProc1", "@obj1", DropDownList1.Text)
DropDownList2.DataSource = dt
DropDownList2.DataTextField = dt.Columns.Item(1).ToString
DropDownList2.DataBind()
Catch ex As Exception
End Try
End If
End If
Stored Procedure...
USE [DB1]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[StoredProc1]
@obj1 nvarchar(4)
AS
BEGIN
SET NOCOUNT ON;
SELECT [Obj1], [obj2]
FROM [DB1].[dbo].[Code]
WHERE [obj1] = @obj1
END
Upvotes: 0
Views: 2567
Reputation: 15253
You need to set the AppendDataBoundItems property to true to preserve the existing values in the DDL:
If you're looking to implement a cascading effect with the DDLs, take a look at this:
http://www.codersbarn.com/post/2007/07/21/Code-Snippet-Cascading-DropDownLists.aspx
Upvotes: 1
Reputation: 27474
I'm not clear why you want to have two drop-down lists, or what is in each.
If you simply want a dropdown list with all the values from a table, but with the value showing being a value previously selected, just say "ddl.SetSelectedValue(somevalue)".
Upvotes: 0
Reputation: 3821
In your SelectedIndex
Change event method, write the following:
ddl2.SelectedValue = ddl1.SelectedValue;
Upvotes: 0