VB.NET - drop down list - Asked By Shashi R on 30-Mar-06 11:53 AM

how can we set a default value in a drop down box during page laod. I am getting the datatextfield from the back end. 

In the page load I have written two functions.
load_ddl()--loads the drop down with the entire list of items in the dataset
load_info()--sets the default value in the dropdown box.

I have used some thing like this below in the load_info()
 ddl.Items.Add(New ListItem("Name"))

but this stmt is not working. its not setting the default value in the drop down box.

DropDownList SelectedValue - Asked By F Cali on 30-Mar-06 12:03 PM

You can use the SelectedValue property of the DropDownList to select the default value.  Just make sure that the value you are assigning in this property exists in the DropDownList or else you'll get an error.

but - Asked By Shashi R on 30-Mar-06 12:58 PM

when i use the selectedvalue property, I was able to set the default value but my dropdown list doesnt show all the values. The first value is replaced with the default value. so I see 2 entries with the default value.
can this be fixed?

Sample Code - Asked By F Cali on 30-Mar-06 01:03 PM

Here's a sample of how to populate a drop down list and set the default value:

DropDownList1.DataSource = DataTable1;
DropDownList1.DataValueField = "ID";
DropDownList1.DataTextField = "Name";
DropDownList1.SelectedValue = "1";
DropDownList1.DataBind();

Once the DropDownList is shown, it will contain all the records from the DataTable and mapping the Name column of the DataTable to the Text and the ID column of the DataTable to the Value.

Try posting your code to help us determine where the problem is.
code - Asked By Shashi R on 30-Mar-06 01:20 PM
I am using datareader to load my drop down box
[CODE]
 Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here

       
        If Not IsPostBack() Then
            populateddl()
            LoadInfo()
        End If
    End Sub

    Sub LoadInfo()
        Dim Booknfo As New ClsBookDetails
        Dim BookID As Integer = Request.QueryString("id")
        BookInfo.GetBookInfo(BookID)

      
        'Set the value to the default value
        'BookInfo has the datatextfiled
         ddl.SelectedValue= BookInfo.MWID

    End Sub

    Sub populateddl()

        Dim BookInfo As New ClsBookDetails
        Dim myreader As SqlDataReader = Bookinfo.GetNames(Session("BranchID"))

        'Clear if any values are Left Behind
        ddl.Items.Clear()
        While myreader.Read
            ddl.Items.Add(New ListItem(myreader.GetValue(1), myreader.GetValue(0)))
        End While

    End Sub
[/CODE]
Response - Asked By F Cali on 30-Mar-06 02:20 PM
Your code looks ok.  Try not calling the LoadInfo first and see if all items in the DropDownList are complete.  Then if the DropDownList looks ok with all the items loaded, try calling the LoadInfo again.  It should select the value you've specified as the selected value.
After the control databinds - Asked By Venkat K on 30-Mar-06 02:30 PM
to dropdownlist, 
add the new list item:

DropDownList1.Items.Insert(0, New ListItem(myreader.GetValue(1),"0"))
I got it - Asked By Shashi R on 30-Mar-06 03:23 PM
This is how i figured

ddl.SelectedItem.Selected = False
ddl.Items.FindByText(BookInfo.MWID).Selected = True

Thank you Ronald for your help