Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add "Please Select" to Drop Down in datalist 2

Status
Not open for further replies.

mwa

Programmer
Jul 12, 2002
507
0
0
US
I have a datalist that contains a databound dropdown list. When I click the edit command of the datalist, the drop down is populated by a sql stored procedure. The data is then displayed in the datalist with the correct value selected in the drop down. This all works fine, until no value has been selected. The datalist sp returns null, and the drop down does not contain a null option, so I get the following: "'cboRelation' has a SelectedValue which is invalid because it does not exist in the list of items". Is there a way to inject a "Please Select" option into the drop down, with a value of "" to resolve this?

mwa
<><
 
After databinding to the dropdownlist add the following:

DropDownList1.Items.Insert(0, "Please Select")
DropDownList1.SelectedIndex = 0
 
Why dont just put a label to display the 'Please select' message. The selected index sould be -1 at start, and so there will always exist a selectedValue
 
Maybe I should give some code, because I'm not clear on how either of those will work:
Drop-Down:
Code:
<asp:DropDownList id="cboRelation" DataSource='<%#GetRelations()%>' Selectedvalue='<%# Container.DataItem("dep_relation")%>' DataTextField="relation_name" DataValueField="relationship_id" runat="server" ></asp:DropDownList>
Code-Behind To populate drop down:
Code:
Function GetRelations() As SqlDataReader
        Dim dr As SqlDataReader = SQLData.GetRelation()
        Return dr
    End Function
Code Behind for edit command of datalist:
Code:
Private Sub DataList1_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles DataList1.EditCommand
        lblMessage.Visible = False
        lbInsert.Visible = False
        DataList1.EditItemIndex = e.Item.ItemIndex
        BindList()
    End Sub

Sub BindList()
        Dim _DB2data As New DB2Data
        'get the ssn
        ssn = _DB2data.GetEmpSsn()
        Dim dr As SqlDataReader = SQLData.GetDep(ssn)
        If dr.HasRows() Then
            DataList1.DataSource = dr
            DataList1.DataBind()
        Else
            DataList1.Visible = False
            lblMessage.Text = "No dependents found."
            lblMessage.Visible = True
        End If
        dr.Close()
    End Sub

Does this help?



mwa
<><
 
As a quick workaround, I added the value "Please Select" to the table that the sp uses to populate the drop down. I still think this should be able to be acomplished through code though.

mwa
<><
 
I understand, but where would that be placed in my code? I've tried putting it in the Page Load, EditCommand, and BindList function. But, none of these work.

mwa
<><
 
[attn]After databinding[/attn] to the dropdownlist add the following:
You can't get any clearer instructions that were given above...


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Then why didn't it work?


mwa
<><
 
It work's fine:
Code:
        Dim dt As New datatable
        Dim dr As DataRow

        dt.Columns.Add("column1")
        For i As Integer = 0 To 10
            dr = dt.NewRow
            dr(0) = i
            dt.Rows.Add(dr)
        Next
        DropDownList1.DataTextField = "column1"
        DropDownList1.DataValueField = "column1"
        DropDownList1.DataSource = dt
        DropDownList1.DataBind()

        DropDownList1.Items.Insert(0, "Please Select")
        DropDownList1.SelectedIndex = 0


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
I understand how it would work in that context. You are always setting the selected index to 0. I don't want this. I want it to select the value that the user has saved in a table. If the user has not selected a value, then I want the "Please Select" option selected.

mwa
<><
 
I understand how it would work in that context. You are always setting the selected index to 0. I don't want this.
OK, so don't include the line that changes the SelectedIndex...


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Back to my original error: "'cboRelation' has a SelectedValue which is invalid because it does not exist in the list of items".

It seems like it is binding the drop down, and trying to set the SelectedValue to a value that doesn't exist at the same time (or slightly after). Then, your code is trying to add in the Please Select option. By that time, it's too late. The error has already occurred.

mwa
<><
 
After binding the data to your dropdown, check to see if you have a valid ID. If you do not, then SelecteIndex = 0.

Example:

"ValueToBeSelected" is the ID coming from your database that you want selected in the dropdown----

After binding the data:

If IsDBNull(ValueToBeSelected) or ValueToBeSelected = 0

cboRelation.Items.Insert(0, "Please Select")
cboRelation.SelectedIndex = 0

Else
cboRelation.SelectedItem.Selected = False cboRelation.Items.FindByValue(ValueToBeSelected).Selected = True

End If
 
Maybe this is what I'm missing:
After binding the data to your dropdown

I'm not doing a cboRelation.databind()

I am simply setting the datasource of the dropdown equal to a function that returns a datareader:

Code:
<asp:DropDownList id="cboRelation" DataSource='<%#GetRelations()%>' Selectedvalue='<%# Container.DataItem("dep_relation")%>' DataTextField="relation_name" DataValueField="relationship_id" runat="server" ></asp:DropDownList>

asp.net seems to handle the databinding for me. So I cannot inject that extra "Please Select" option into the dropdown as you are describing.



mwa
<><
 
asp.net seems to handle the databinding for me. So I cannot inject that extra "Please Select" option into the dropdown as you are describing.

WHAT???


At some point the data is being bound. Are you calling a function you created?

Any way you do it, the examples given to you will work. Find the code that retrieves your data, then binds it.. then add the code given to you above
Code:
DropDownList1.Items.Insert(0, "Please Select")
 
I've given the code for the page in my second post. Do you see a databind?


mwa
<><
 
You are binding in your BindList() function. That in turn binds your dropdown list because it is in the datalist. What you need to do, is to use the ItemDataBound event of the datalist. Get a reference to your ddl, and then use the code provided to add the additional item to it.

Jim
 
OK. That's the part I was missing. I didn't realize that the dropdownlist is bound at the same time the datalist is bound. So, I tried to add the following code:
Code:
    Private Sub DataList1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles DataList1.ItemDataBound
        'add a friendly message to the drop down
        If DataList1.EditItemIndex <> -1 Then
            Dim newItem As New ListItem
            newItem.Value = ""
            newItem.Text = "Please Select"
            Dim cboRelation As DropDownList
            cboRelation = CType(DataList1.FindControl("cboRelation"), DropDownList)
            cboRelation.Items.Insert(0, newItem)
        End If
    End Sub
But I get the error "Object not set to an instance of an object".

Any ideas on how to proceed?

mwa
<><
 
Are you trying to get a refernece to the dropdownlist when the row is being edited or not edited?

If you want the edited row then:
Code:
If e.Item.ItemType = ListItemType.EditItem Then
   .. your code here.. except use this:
   cboRelation = CType([b]e.item.[/b]FindControl("cboRelation"),  DropDownList)
End If
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top