how to display a default value in dropdown list while it is bound with formview?

Last post 06-30-2007 9:39 AM by Talha.Majeed. 1 replies.

Sort Posts:

  • how to display a default value in dropdown list while it is bound with formview?

    06-30-2007, 7:57 AM

    Hi,

    I am using asp.net.2.0 and sqlserver2005. I'm using a dropdownlist in a EditItemTemplate of a formview.

    Here is my code.

     

    <td align="left" nowrap="nowrap" valign="middle">
    <
    asp:DropDownList ID="ddInvType" runat="server" DataSourceID="sdsInvoiceType" DataTextField="InvoiceTypeID" DataValueField="InvoiceTypeID" SelectedValue='<%# Bind("InvType") %>' Width="100px">
    </asp:DropDownList>
    </
    td>

     

    <asp:SqlDataSource ID="sdsInvoiceType" runat="server" ConnectionString="<%$ConnectionStrings:ss %>"
    SelectCommand="SELECT Invoice.InvoiceTypeID FROM Invoice ORDER BY Invoice.InvoiceType"> 
    </asp:SqlDataSource>

    I need to display the dropdownlist with the default value like 'None' while the field have no data(InvType is null or zero). As the field is Integer type i cant set 'None' in the field value of that table.

    Please help me how to display default value in dropdownlist, while at the same time dropdownlist should load all of its data?
     


     

    Remember to click “Mark as Answer” on the post, if it helps you.
  • Re: how to display a default value in dropdown list while it is bound with formview?

    06-30-2007, 9:39 AM
    Answer
    • Member
      304 point Member
    • Talha.Majeed
    • Member since 10-27-2006, 8:24 AM
    • Lahore, Pakistan
    • Posts 57

    Hello,

    After databinding, just insert a new item in the dropdownlist.

    public void LoadDropDownList()
    {
     DropDownList1.DataBind();
     DropDownList1.Items.Insert(0, new ListItem("Select A Value", "0"));
     DropDownList1.SelectedIndex = 0;
    }

    Hope this solves the problem.

    Talha Majeed
    Consultant
    Kalsoft ( Pvt. ) Ltd.

    Cell: +92 333 415 6026
    Email: talha.majeed@ekalsoft.com
    Web: http://www.ekalsoft.com
Page 1 of 1 (2 items)