将ListItem添加到ListBox的顶部

时间:2014-02-06 16:10:27

标签: asp.net listbox

我在下面有 ListBox ,想知道如何将 ListItem 添加到 ListBox 中形成 SQLDataSource 。我想在此 ListBox 的顶部添加一个Zero,然后添加来自 SQLDataSource 的数据。

<asp:ListBox 
    ID="ListBox4" 
    runat="server" 
    DataSourceID="getAvaibleChapters" 
    DataTextField="chapterNo" 
    DataValueField="chapterNo" 
    Rows="1"  
    SelectedValue='<%# Bind("no") %>' />



 <asp:SqlDataSource ID="getAvaibleChapters" runat="server" 
        ConnectionString="<%$ ConnectionStrings:RXIConnectionString %>" 

        SelectCommand="SELECT chapterNo FROM Chapters WHERE (subjectID = @subjectid) order by chapterNo asc">
         <SelectParameters>
             <asp:QueryStringParameter Name="subjectid" QueryStringField="subjectid" />
         </SelectParameters>
    </asp:SqlDataSource>

3 个答案:

答案 0 :(得分:1)

尝试这样..它可能对你有帮助。

 SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        DataRow dr = dt.NewRow();
        dr.ItemArray = new object[] { 0, "---Select an item---" };
        dt.Rows.InsertAt(dr,0);
        cmbProName.DisplayMember = "ProductName";
        cmbProName.ValueMember = "PID";
        cmbProName.DataSource = dt;

答案 1 :(得分:0)

我会像下面的内容那样更改DataSource SQL语句

select 0, 'select all'
UNION 
select chapterNo, no from  Chapter

http://sqlfiddle.com/#!3/baad6/2

答案 2 :(得分:0)

这是执行此操作的最佳方法,

DECLARE @temp TABLE
(
 chapterNo int
)
insert @temp
values (0);
INSERT INTO @temp
SELECT chapterNo FROM Chapters WHERE subjectID = @subjectid;
select * from @temp order by chapterNo asc