Databinding problem

Discussion in 'ASP .Net' started by David C, May 27, 2008.

  1. David C

    David C Guest

    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
    >>' option it runs a Javascript function to get the name of the new item.

    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
    David C, May 27, 2008
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:%...
    >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
    > >>' option it runs a Javascript function to get the name of the new item.

    > 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
    >
    >
    >
    Eliyahu Goldin, May 28, 2008
    #2
    1. Advertising

  3. David C

    David C Guest

    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" <> wrote in
    message news:...
    > 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" <> wrote in message
    > news:%...
    >>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
    >> >>' option it runs a Javascript function to get the name of the new item.

    >> 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
    >>
    >>
    >>

    >
    >
    David C, May 28, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Saunders

    Re: Problem with DataBinding and CheckBoxList

    John Saunders, Aug 10, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    414
    Eric Johannsen
    Aug 11, 2003
  2. Arun K
    Replies:
    0
    Views:
    534
    Arun K
    Dec 29, 2003
  3. volts
    Replies:
    0
    Views:
    492
    volts
    Apr 7, 2004
  4. jobs
    Replies:
    0
    Views:
    1,595
  5. Earl Teigrob
    Replies:
    1
    Views:
    386
    Shravan
    Mar 1, 2004
Loading...

Share This Page