绑定到数据库后选择列表框的值 [英] select values of listbox after binding to database

查看:77
本文介绍了绑定到数据库后选择列表框的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的列表框包含许多值。我想在列表框控件上选择那些已保存在我的数据库列中的特定值。我的数据库列有3个值,当我尝试在列表框上显示时,它只选择最后一个值。

例如:

My listbox contains many values. I am tring to selects those particular values on listbox control which are already save in my database column. My database column has 3 values and when i trying it to show on listbox it selects only last value.
eg:

<asp:ListBox ID="ListBox1" runat="server" Height="149px" SelectionMode="Multiple" Width="113px">
        <asp:ListItem Value="1">Vinz</asp:ListItem>
        <asp:ListItem Value="2">Jhen</asp:ListItem>
        <asp:ListItem Value="3">Chris</asp:ListItem>
        <asp:ListItem Value="4">Shynne</asp:ListItem>
        <asp:ListItem Value="5">Chu</asp:ListItem>
        <asp:ListItem Value="6">Mark</asp:ListItem>
        </asp:ListBox> 




SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
      {
         ListBox1.SelectedValue = reader["subcategory_id"].ToString();
      }




select r.*, sc.subcategory_id from dbo.tbl_adregister r INNER JOIN tbl_adsubcategory sc on r.ad_id=sc.ad_id	



表(tbl_adsubcategory)中有1,2,3 column(subcategory_id)但在列表框中只显示了3,我想在列表框中选择所有3。

希望你能理解我的问题。 Plz帮助。


Table (tbl_adsubcategory) has 1,2,3 in its column (subcategory_id) but only 3 is show selected in the listbox, i want that all 3 is select in the listbox.
Hope you able to understand my problem. Plz help.

推荐答案

这里有一个线索...



here's a clue...

SqlDataReader reader = cmd.ExecuteReader();
List<string> selected = new List<string>();

while (reader.Read())
{
     selected.Add(reader["subcategory_id"].ToString());
}

listView1.SelectedItems.Clear();
for(int i=0;i<selected.count();i++)>
        listView1.Items[(int)i].Selected = true;

</string></string>


预先选择带有sql列值的列表框 [ ^ ]


这篇关于绑定到数据库后选择列表框的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆