如何将数据绑定到aspx.cs中的aspxlistbox

时间:2014-02-11 04:22:05

标签: c# asp.net devexpress

以下是aspx文件中的dropdownedit,其中项目是硬编码。有什么方法可以通过将它与数据库值绑定在aspx.cs中添加项目吗?我无法使用ASPxListBox1.items.add。

<dx:ASPxDropDownEdit ClientInstanceName="checkComboBox" ID="ASPxDropDownEdit1" runat="server" AnimationType="None">
    <DropDownWindowStyle BackColor="#EDEDED" />
    <DropDownWindowTemplate>
    <dx:ASPxListBox Width="100%" ID="ASPxListBox1" ClientInstanceName="checkListBox" SelectionMode="CheckColumn"
        runat="server">
        <Border BorderStyle="None" />
        <BorderBottom BorderStyle="Solid" BorderWidth="1px" BorderColor="#DCDCDC" />
        <Items>
            <dx:ListEditItem Text="(Select all)" />
            <dx:ListEditItem Text="1" Value="1" />
            <dx:ListEditItem Text="2" Value="2" />
        </Items>
        <ClientSideEvents SelectedIndexChanged="OnListBoxSelectionChanged" />
     </dx:ASPxListBox>
            <table style="width: 100%">
                <tr>
                    <td style="padding: 4px">
                        <dx:ASPxButton ID="ASPxButton1" AutoPostBack="False" runat="server" Text="Close" style="float: right">
                            <ClientSideEvents Click="function(s, e){ checkComboBox.HideDropDown(); }" />
                        </dx:ASPxButton>
                    </td>
                </tr>
            </table>
     </DropDownWindowTemplate>
     <ClientSideEvents TextChanged="SynchronizeListBoxValues" DropDown="SynchronizeListBoxValues" />
</dx:ASPxDropDownEdit>

1 个答案:

答案 0 :(得分:1)

您可以创建数据源

Private void Fill_ListBox()

    conn.Open();
    SqlCommand cmd = New SqlCommand("YourSPROC", conn);
    cmd.CommandType = CommandType.StoredProcedure;


    using(SQlDataReader dr = cmd.ExecuteReader())
    {

    ListBox1.DataSource = dr
    ListBox1.DataTextField = dr["DBValue1"]
    ListBox1.DataValueField = dr["DBValue2"]
    }
   ListBox1.DataBind()
   conn.Close();

尝试此代码..您必须根据数据源进行一些更改。