This is a data-bound dropdown list control (VB.NET) that is inherited from
System.Web.UI.WebControls.DropDownList server control.
Some times you have the requirement to populate the
DropDownList from any database table’s column with some filters. One way to accomplish this would be making a
DataSet and fill it with result of the query or stored procedure and then source the
DropDownList from that
DataSet. The other approach would be to have a
UserControl which has properties for connection string, table name, columns to display, column for value. The control discussed here in this article builds the query according to the values of these properties and pulls the data from database.
This control has the following properties:
TableName: from which you want to populate the data.
Connection_String: This is a place where you can put the connection string. It’s not really a good place to put the connection string, so if you want, you can also specify that in web.config and get the application variable from there. The control will pick this field first, and if this is
Nothing, then it will look for any application variable called “
connectionString”. You are welcome to modify the code according to your need.
DisplayColumn: Exact name of the column from the database table which you want to display in the
ValueColumn: Column name from the database which you want as value field in
False will decide whether to put any first value.
FirstFieldText: This value will go into the first field of
DropDownList. This is used if you just want to display something <-Please choose bla bla bla -->.
DisplayColumnSeparator: If you want to display two column fields in the
DropDownList with some separator, then this one comes in the picture. Example: let’s say database columns are
LastName, but you want to display FirstName#LastName in the
DropDownList, then you will put “#” in the
ValueColumnSeparator: This also works the exactly the same way as
DisplayColumnSeparator other than this value goes into the “Value” field of
WhereClause: This text will goes into the SQL query’s
where clause. This property is optional and very useful in filtering the data from the table. For example: “ Where UserID =1”.
HTML to add inline
<%@ Register TagPrefix="ddldb"
namespace= WebApplication1 assembly=WebApplication1 %>
<ddldb:dbdropdownlist id="ddldb1" runat="server" width="216px" >
Issues with this control
- Uses query to access data. Not written to use sproc, but can be modified according to the need.
- Many more yet to appear :).