Setting DropDownList SelectedValue

Discussion in 'ASP .Net' started by David C, Nov 20, 2007.

  1. David C

    David C Guest

    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
    lngNewID = DBClass.CreateNewtlkpAgent(txtNewText.Text)
    'bind formview
    '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 C, Nov 20, 2007
    1. Advertisements

  2. The error message is pretty clear, "lngNewID" does not exist in the list of
    items. You need to re-populate the DropDownList after adding the new record
    so that the new record is present in the ddl items list.

    Scott Roberts, Nov 20, 2007
    1. Advertisements

  3. David C

    David C Guest

    I thought the DataBind() on the FormView would do that. What am I missing?

    David C, Nov 20, 2007
  4. How is the DDL populated initially? Is it data-bound or populated manually?
    If it's databound, call DataBind() on the DDL. Otherwise, re-populate
    manually (or just add the new record to the Items list).
    Scott Roberts, Nov 20, 2007
  5. David C

    David C Guest

    I tried doing DataBind on varControl and I also tried DataBind on
    SqlDataSource. Both still give me this error. Any other ideas? Do I need
    to do this somewhere other than Page_Load? I'm stumped. Thanks.

    David C, Nov 21, 2007
  6. I'll be honest with you, I don't use SqlDataSource or data-bound controls.
    Primarily because I don't like dealing with stuff like this.

    That said, it sounds like you simply need to refresh the SqlDataSource (not
    sure exactly how to go about that, but I would think it should be pretty
    straight-forward) so that the newly added row is included, then call
    DataBind() on varControl to re-populate it.
    Scott Roberts, Nov 21, 2007
  7. David C

    David C Guest

    Every time I call DataBind() on varControl I get "Databinding methods such
    as Eval(), XPath(), and Bind() can only be used in the context of a
    databound control."

    This really sucks. There has to be a solution to this because it is so easy
    in other languages.
    David C, Nov 21, 2007
  8. So the control is not data-bound after all? You've really not given us much
    to work with.

    How is the DDL populated? What does the control look like in the aspx? If
    you are doing the data-binding in the code-behind, what does that code look

    I'm envisioning something like this:

    myDataSource.Refresh(); // pick up the new record. not sure of the
    varControl.DataSource = myDataSource; // the datasource is not persisted in
    the viewstate. it must be declared in the aspx or re-assigned on each
    varControl.DataBind(); // populate the control.
    Scott Roberts, Nov 21, 2007
  9. David C

    David C Guest

    Below is the DDL control and below it is the DataSource. Hope this helps.


    <asp:DropDownList ID="ddlAgentID" runat="server" DataSourceID="SqltlkpAgent"
    DataTextField="Agent" DataValueField="AgentID" SelectedValue='<%#
    Bind("AgentID") %>' Width="300" AppendDataBoundItems="true">
    <asp:ListItem Value="-1" Text="&lt;&lt;New Agent&gt;&gt;"></asp:ListItem>

    <asp:SqlDataSource ID="SqltlkpAgent" runat="server" ConnectionString="<%$
    ConnectionStrings:FiledataConnectionString %>"
    SelectCommand="SELECT 0 As AgentID, NULL As Agent UNION SELECT [AgentID],
    [Agent] FROM [tlkpAgent] ORDER BY [Agent]">
    David C, Nov 21, 2007
  10. David,

    All you need is "varControl.DataBind()" right after you create a new record.

    If you look closely at your dropdownlist control after inserting a new
    record, I think you'll find that all of your Agents are in the list TWICE.
    The new agent is in there, but it's in the 2nd copy of agents (toward the
    bottom of the list).

    Why is that? You have AppendDataBoundItems="true" which appends items to the
    list instead of refreshing the list. Set AppendDataBoundItems="false" and
    you'll get what you want.


    Scott Roberts, Nov 21, 2007
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.