locked
filling a dataset object and assign it to the DataSource of the DropDownList RRS feed

  • Question

  • User-1497429768 posted

    Hi,

    I fill a dataset object and assign it to the DataSource of the DropDownList.The following
    is the sql code:field A(varchar(3))、B(varchar(MAX)) and C(float).

    select A,(A+' '+convert(char(8),B)+' '+convert(char(10),C)) as D from Table

    THe DataTextField of the DropDownList is D.

    The result in sql server Management Studio is as the follows.

    AAA BBBBBBBB 123
    AAA BBBB         9502
    AAA BBBBBB     72010
    AAA BBBBBBB   693022

    But the result of the DropDownList is as the follows.

    AAA BBBBBBBB 123
    AAA BBBB 9502
    AAA BBBBBB 72010
    AAA BBBBBBB 693022

    I want the result in sql server Management Studio. Hava any idea? Thanks a lot.

    Friday, May 5, 2017 8:59 AM

Answers

  • User-821857111 posted

    You apply a monospace font via CSS. For example, you can apply a CssClass value of "monospace" to the DropDownList, and then add a suitable style rule in your CSS file or in the aspx:

    <asp:DropDownList ID="DropDownList1" runat="server" CssClass=monospace"... OnDataBound="DropDownList1_DataBound"
    <style>
    .monospace { font-family: monospace; }
    </style>

    Also, you need to HtmlDecode the list items once you have bound the data to the list because they are encoded by default:

    protected void DropDownList1_DataBound(object sender, EventArgs e) 
    {
        foreach(ListItem in DropDownList1.Items)
        {
            item.Text = Server.HtmlDecode(item.Text);
        }
    }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 5, 2017 11:40 AM

All replies

  • User-821857111 posted

    If you want to preserve whitespace in dropdown options, you can replace the spaces with &nbsp; and use a monospace font.

    Friday, May 5, 2017 9:17 AM
  • User-1497429768 posted

    Hi mikesdotnetting,

         Thank you for your reply so fast.I tune my code as the follows.

    select A,(A+' '+convert(char(8),B)+' '+replicate('&nbsp;',8-len(convert(char(8),B)))+' '+convert(char(10),C)) as D from Table.

         But I can't find the monospace font,and the result is like AAA BBBB&nbsp;&nbsp;&nbsp;&nbsp; 9502.

         I think I must debug my code. Thanks.

       best regards,

      jeff

         

        

    Friday, May 5, 2017 9:51 AM
  • User-821857111 posted

    You apply a monospace font via CSS. For example, you can apply a CssClass value of "monospace" to the DropDownList, and then add a suitable style rule in your CSS file or in the aspx:

    <asp:DropDownList ID="DropDownList1" runat="server" CssClass=monospace"... OnDataBound="DropDownList1_DataBound"
    <style>
    .monospace { font-family: monospace; }
    </style>

    Also, you need to HtmlDecode the list items once you have bound the data to the list because they are encoded by default:

    protected void DropDownList1_DataBound(object sender, EventArgs e) 
    {
        foreach(ListItem in DropDownList1.Items)
        {
            item.Text = Server.HtmlDecode(item.Text);
        }
    }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 5, 2017 11:40 AM
  • User-1497429768 posted

    Hi Mike,

        That's great.It can work.

        Thank you so much.

        best regards,

        jeff

      

     

    Friday, May 5, 2017 2:48 PM