繁体   English   中英

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

[英]How to bind data to aspxlistbox in aspx.cs

以下是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>

您可以创建一个数据源

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();

尝试使用此代码。.您将不必对DataSource进行任何更改。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM