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
<><
 
Also, I would use a differnt name than cboRelation, since that is the name of the ddl in the datalist. It may cause a problem, not sure, but better to use a different name.
 
OK... That worked. So now, the "Please Select" is the first option in the drop down and the value is "". But now I'm need to select the appropriate option. I'm going back to one of your other posts:
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
Where does this part go?

mwa
<><
 
I just tried this and it works, but I don't quite understand:
I added this to the drop down:
Code:
SelectedIndex='<%# GetRelationIndex(DataBinder.Eval(Container.DataItem, "dep_relation").ToString()) %>'
And then I added this function in the code behind:
Code:
Function GetRelationIndex(ByVal fldval As String) As Integer
        If fldval = DBNull.Value.ToString Then
            Return -1
        Else
            Dim cboRel As DropDownList
            cboRel = CType(DataList1.FindControl("cboRelation"), DropDownList)
            Return cboRel.Items.IndexOf(cboRel.Items.FindByValue(fldval))
        End If
    End Function

When I put Return 0 it selected the second option in the drop down, not the "Please Select" option. So I changed this to -1 and it selected the "Please Select" option correctly. It seems to me though, that since we injected the "Please Select" option into the 0 spot in the drop down, the index wuld be 0.

mwa
<><
 
Well that is not my post(BarkingFrog's). But since you have it working, you do not need to include inserting the "PleaseSelect" option as he was doing in his example, but you can use the rest.
This again needs to be but in the ItemDataBound event. Basically, what he is saying that if there is a value that needs to be shown in the ddl, select it, otherwise, select the "Please Select" message.
What you need to do is determine the value that needs to be set. What I ususally do, is (I use datagrids, but the idea is the same), have a column with the value you need to select in the ddl. I hide this cell(column). In the ItemDataBound event, I then check that value, grab it, and set it.(Use BarkingFrog's code), otherwise set the index to 0.

Jim
 
My apologies to BarkingFrog - You get a star too...

I think I've got it now. Thanks a million for everyone's help.

mwa
<><
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top