开发者

how to bind the data from database to the dropdown?

Suppose in .aspx page their is dropdown in which i want to bind two coloumn in that dropdown.for example开发者_运维知识库 :-

in database their two column of First_name ,Last_name .I want these two columns value come in a single dropdown using C#.

How to do that? Please tell me.


If you are using the sql query , try this:

select First_name  + ' ' + Last_name   from table

Then you need to bind it in the Dropdown as normally.

dropdown1.DataSource = datatable;
dropdown1.DataBind();


An alternative to doing it in code would be to drop a SQL data source on the page and configure in the following way.

<asp:sqldatasource id="SqlDataSource1" runat="server" 
connectionstring="<%$ ConnectionStrings:MyDatabase %>" 
selectcommand="SELECT [ID], [First_name] + ' ' + [Last_name] AS [FullName] FROM [tPerson]"></asp:sqldatasource>

And then your dropdown list control:

<asp:dropdownlist id="ddlPeople" runat="server" datasourceid="SqlDataSource1"
    datatextfield="FullName" datavaluefield="ID" />

This would all go in the asp.net page.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜