2

我想每次都用一个链接按钮(编辑)填充一个下拉列表,如下所示: 在此处输入图像描述

我用这段代码来填充它:

sqlcmd1 = new SqlCommand(@"SELECT country FROM Authors  
                           WHERE AuthorID=@AuthorID 
                           AND AuthorUserName=@AuthorUserName", sqlconn);
sqlcmd1.Parameters.AddWithValue("@AuthorUserName", AuthorUserName);
sqlcmd1.Parameters.AddWithValue("@AuthorID", AuthorID);

dd_country.Text= ((string)sqlcmd.ExecuteScalar()).ToString();

但它会用一个数字填充下拉列表!

如果我将代码更改为以下内容,则会发生其他情况:

dd_country.SelectedItem.Text= ((string)sqlcmd.ExecuteScalar()).ToString();

它导致将下拉列表项更改为选定项;比如它把加拿大改成了布隆迪!事实上,这些项目只是暂时改变了,然后我在下拉列表中有两个加拿大项目!

在此处输入图像描述

如何在没有这些问题的情况下通过编辑按钮填充下拉列表?

4

1 回答 1

2

You must set the Value of the drop-down list to the number, and the Text to the country name.

Then you can do this to set it as selected -> dropdownlist.SelectedValue=1 and it will select the country appropriately.

Avoid this problem using Data-Source like so:

enter image description here

A dialog box pops up that looks like this:

enter image description here

And you can follow on :-) from there

于 2013-02-09T12:08:24.770 回答