lunes, 20 de diciembre de 2010

The dropdownlist web control

The dropdownlist web control is very useful and easy to implement, it enables users to select from a single selection drop down list.

Let's see some properties:

<asp:dropdownlist id="ddl1" runat="server" datasource="<%# here it goes an expression %>" datatextfield="datasourcefield" datavaluefield="datasourcefield" />

Now what we will do is to bind the dropdownlist web control with a datasource, so we first need to create a connection to a database. In this case we'll use the northwind database which you can download it for free.

Now let's create the connection string with all the items we'll need with C# code:

String strConn, strCmd;
strConn = "database=northwind; server=localhost; Integrated Security=SSPI;";
strCmd = "Select employeeid, firstname, lastname from employees";
sqldataadapter da = new sqldataadapter(strCmd, strConn);
dataset ds = new dataset();
da.fill(ds, "elist");
datatable dt = new datatable();
dt = ds.tables["elist"];
dt.columns.add("eName", typeof(String),"firstname + ' ' + lastname");

I hope you find this useful.

Regards


No hay comentarios:

Publicar un comentario