Databinding problem

  • Thread starter Thread starter David C
  • Start date Start date
D

David C

I have a drop down list control where I set AppendDataBound to true and
The new name is stored in a hidden TextBox and in the Load event I use it to
add the record to the lookup table. Below is the block of code in the Load
event of the FormView.

Dim row As FormViewRow = fvTrademark.Row
Dim varControl
Dim lngNewID As Long = 0
Dim lngTradeID As Long = fvTrademark.DataKey.Value

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, lngTradeID, False)
'set dropdownlist selectedvalue to new agentid
varControl.SelectedValue = lngNewID.ToString
txtNewText.Text = ""
Exit Select
End If

The DBClass function creates a new record in the lookup table (which is used
as the DataSource for the drop down list (ddlAgentID). The error occurs
trying to set the SelectedValue of the control. The error is below:

'ddlAgentID' has a SelectedValue which is invalid because it does not exist
in the list of items.
David
 
That worked, but the new text did not show up in the dropdown list. It
still shows <<new>> instead of the value it should. When I try to use a
DataBind() on the control I get an error "Databinding methods such as
Eval(), XPath(), and Bind() can only be used in the context of a databound
control."

How do I avoid this error so the new text value is displayed?

David
Eliyahu Goldin said:
SelectedValue property is intented for selecting an existing ddl item with
matching value. It can't be used for modifying items. You need to use
SelectedItem.Value.

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


David C said:
I have a drop down list control where I set AppendDataBound to true and
added a list item of '<< add new>>' with a value of -1 to use for adding
items to the list during data entry. When the user selects the '<< add
new
The new name is stored in a hidden TextBox and in the Load event I use it
to add the record to the lookup table. Below is the block of code in the
Load event of the FormView.

Dim row As FormViewRow = fvTrademark.Row
Dim varControl
Dim lngNewID As Long = 0
Dim lngTradeID As Long = fvTrademark.DataKey.Value

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, lngTradeID, False)
'set dropdownlist selectedvalue to new agentid
varControl.SelectedValue = lngNewID.ToString
txtNewText.Text = ""
Exit Select
End If

The DBClass function creates a new record in the lookup table (which is
used as the DataSource for the drop down list (ddlAgentID). The error
occurs trying to set the SelectedValue of the control. The error is
below:

'ddlAgentID' has a SelectedValue which is invalid because it does not
exist in the list of items.
David
 
Back
Top