If you want a static set of items in the dropdown list you can define them in the property panel under the property Items you will find a collection, click the "..." and add items with value and text to appear in the dropdown list.
Best regards,
Per Salmi
If you're trying to get the values from the database, you can do something like this:
Sub Page_Load(ByVal senderAs Object,ByVal eAs EventArgs)If Not Page.IsPostBackThen GetData()End If
End SubSub GetData()
Dim connStringAs String
Dim conAs SqlConnectionTry connString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings("ConnectionString1").ConnectionString
con =New SqlConnection(connString)
Dim cmdAs SqlCommand =New SqlCommand()
Dim readerAs SqlDataReadercmd.Connection = con
cmd.CommandText ="EXECUTE dbo.GetDepartmentCode" reader = cmd.ExecuteReaderIf reader.HasRowsThen
While (reader.Read)
Dim newItemAs New ListItem
newItem.Text = (reader.Item("DeptCode")).ToString
newItem.Value = (reader.Item("DeptCode")).ToString
deptCodeDDL.Items.Add(newItem)
End While
End If reader.Close()Catch exAs Exception
Response.Write(ex)
Finally con.Close() con.Dispose()End TryCatch exAs ApplicationException
Response.Write("Could not load the database")
End Try
End Sub
The ASP.Net side would like this:
<asp:DropDownList ID="deptCodeDDL" runat="server"></asp:DropDownList>
A few things to note though.
In the code I've posted, I use a stored procedure (sproc) to retrieve the data from the database. If you don't what sprocs are, you can read about themhere. Using sprocs is generally considered a better way of accessing the db.
Also, you will of course, have to replace DeptCode with the column name from where you will be getting the values.
Finally, I use a ConnectionString, which is being retrieved from the Web.config file. You should have that in you application folder.
If you don't, given below is a sample:
<!--
Note: As an alternative to hand editing this file you can use the
web admin tool to configure settings for your application. Use
the Website->Asp.Net Configuration option in Visual Studio.
A full list of settings and comments can be found in
machine.config.comments usually located in
\Windows\Microsoft.Net\Framework\v2.x\Config
--><configuration>
<appSettings/>
<connectionStrings>
<add name="ConnectionString1" connectionString="Data Source=SERVERNAME\SQLSERVER;Initial Catalog=DBNAME;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
<!--
Set compilation debug="true" to insert debugging
symbols into the compiled page. Because this
affects performance, set this value to true only
during development.
--><compilation debug="false"
</compilation>
<!--
The <authentication> section enables configuration
of the security authentication mode used by
ASP.NET to identify an incoming user.
--><authentication mode="Windows"/>
<!--
The <customErrors> section enables configuration
of what to do if/when an unhandled error occurs
during the execution of a request. Specifically,
it enables developers to configure html error pages
to be displayed in place of a error stack trace.<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
--></system.web>
</configuration>
You will have to replace SERVERNAME with you server name and DBNAME with your db name.
No comments:
Post a Comment