Home All Groups Group Topic Archive Search About

Setting DropDownList SelectedValue

Author
20 Nov 2007 10:49 PM
David C
I have an ASP.Net page that has a FormView with multiple controls. One of
those controls is a DropDownList that is bound to a SQL table which is the
DataSource.  I added an option to the DropDownList to choose <<Add>> with a
value of -1.  I have a VB class function that inserts a new record and
returns the identity.  This works but when I try to set the SelectedValue to
this new identity I get "'ddlAgentID' has a SelectedValue which is invalid
because it does not exist in the list of items."

The code to do this is in the Page_Load sub as follows:

        varControl = row.FindControl("ddlAgentID")
        If varControl.SelectedValue = -1 Then
            'Run class function to create new record in tlkpAgent and return
ID
            lngNewID = DBClass.CreateNewtlkpAgent(txtNewText.Text)
            'bind formview
            fvPatents.DataBind()
            'set dropdownlist selectedvalue to new agentid
            varControl.SelectedValue = lngNewID
            'txtNewText.Text = lngNewID.ToString
            txtNewText.Text = ""
        End If


The new record is getting created but the setting of SelectedValue is giving
error.  Can anyone help?  Thanks.

David

Bookmark and Share


Post Thread options