Databinding problem

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
 
D

David C

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Setting DropDownList SelectedValue 9
Adding records to a DropDown control 1
Vaiable without AS 2
NullReferenceException question 2
Best way to add to DDL 2
Menu DataBinding issue 0
Databinding 1
DataBinding 1

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top