Dropdown List - Check if value exists in list prior to selection

Discussion in 'ASP .Net' started by =?Utf-8?B?QnJlbmRlbiBCaXhsZXI=?=, Mar 10, 2007.

  1. Hello.

    I am reading a value from a table and trying to determine if that value
    exists in a list of values associated with a dropdownlist. If so, I select
    the value, otherwise, I don't. I haven't been able to figure this operation
    out so far.

    Code:

    if (reader["region_id"] != System.DBNull.Value)
    {
    if
    (ddlApplicantRM.Items.Contains(ddlApplicantRM.Items.FindByValue(reader["region_id"].ToString())) == true)
    {
    ddlApplicantRM.SelectedValue =
    reader["region_id"].ToString();
    }
    else
    {
    ddlApplicantRM.SelectedValue = "0";
    }
    }

    Even though the value clearly exists in the list of values attached to the
    dropdown, it never selects the value in the dropdown correctly. Is this the
    proper way of doing what I am trying to accomplish? Does it matter if
    region_id is an INT and the comparison function requires a string?

    Thanks in advance for any suggestions or help.
    =?Utf-8?B?QnJlbmRlbiBCaXhsZXI=?=, Mar 10, 2007
    #1
    1. Advertising

  2. Hi,
    Try either of this code:

    Method 1 :
    DropDownList1.SelectedIndex =
    DropDownList1.Items.IndexOf(DropDownList1.Items.FindByValue(reader["region_id"].ToString()));

    //DropDownList1.SelectedIndex =
    DropDownList1.Items.IndexOf(DropDownList1.Items.FindByText(<Text>));

    or

    Method 2 :
    DropDownList1.Items.FindByValue(reader["region_id"].ToString()).Selected =
    true ;

    //DropDownList1.Items.FindByText("<Text>").Selected = true ;

    If my answer helped you,then please do press Yes below.
    Thanks and Regards.
    Manish Bafna.
    MCP and MCTS.



    "Brenden Bixler" wrote:

    > Hello.
    >
    > I am reading a value from a table and trying to determine if that value
    > exists in a list of values associated with a dropdownlist. If so, I select
    > the value, otherwise, I don't. I haven't been able to figure this operation
    > out so far.
    >
    > Code:
    >
    > if (reader["region_id"] != System.DBNull.Value)
    > {
    > if
    > (ddlApplicantRM.Items.Contains(ddlApplicantRM.Items.FindByValue(reader["region_id"].ToString())) == true)
    > {
    > ddlApplicantRM.SelectedValue =
    > reader["region_id"].ToString();
    > }
    > else
    > {
    > ddlApplicantRM.SelectedValue = "0";
    > }
    > }
    >
    > Even though the value clearly exists in the list of values attached to the
    > dropdown, it never selects the value in the dropdown correctly. Is this the
    > proper way of doing what I am trying to accomplish? Does it matter if
    > region_id is an INT and the comparison function requires a string?
    >
    > Thanks in advance for any suggestions or help.
    =?Utf-8?B?TWFuaXNoIEJhZm5h?=, Mar 11, 2007
    #2
    1. Advertising

  3. RE: Dropdown List - Check if value exists in list prior to selecti

    I appreciate you taking a stab at the solution, both both failed.

    The first simply did not work. The second causes the page to render
    incorrectly.

    "Manish Bafna" wrote:

    > Hi,
    > Try either of this code:
    >
    > Method 1 :
    > DropDownList1.SelectedIndex =
    > DropDownList1.Items.IndexOf(DropDownList1.Items.FindByValue(reader["region_id"].ToString()));
    >
    > //DropDownList1.SelectedIndex =
    > DropDownList1.Items.IndexOf(DropDownList1.Items.FindByText(<Text>));
    >
    > or
    >
    > Method 2 :
    > DropDownList1.Items.FindByValue(reader["region_id"].ToString()).Selected =
    > true ;
    >
    > //DropDownList1.Items.FindByText("<Text>").Selected = true ;
    >
    > If my answer helped you,then please do press Yes below.
    > Thanks and Regards.
    > Manish Bafna.
    > MCP and MCTS.
    >
    >
    >
    > "Brenden Bixler" wrote:
    >
    > > Hello.
    > >
    > > I am reading a value from a table and trying to determine if that value
    > > exists in a list of values associated with a dropdownlist. If so, I select
    > > the value, otherwise, I don't. I haven't been able to figure this operation
    > > out so far.
    > >
    > > Code:
    > >
    > > if (reader["region_id"] != System.DBNull.Value)
    > > {
    > > if
    > > (ddlApplicantRM.Items.Contains(ddlApplicantRM.Items.FindByValue(reader["region_id"].ToString())) == true)
    > > {
    > > ddlApplicantRM.SelectedValue =
    > > reader["region_id"].ToString();
    > > }
    > > else
    > > {
    > > ddlApplicantRM.SelectedValue = "0";
    > > }
    > > }
    > >
    > > Even though the value clearly exists in the list of values attached to the
    > > dropdown, it never selects the value in the dropdown correctly. Is this the
    > > proper way of doing what I am trying to accomplish? Does it matter if
    > > region_id is an INT and the comparison function requires a string?
    > >
    > > Thanks in advance for any suggestions or help.
    =?Utf-8?B?QnJlbmRlbg==?=, Mar 11, 2007
    #3
  4. Re: Dropdown List - Check if value exists in list prior to selecti

    On Mar 11, 7:36 pm, Brenden <> wrote:
    > I appreciate you taking a stab at the solution, both both failed.
    >
    > The first simply did not work. The second causes the page to render
    > incorrectly.
    >
    >
    >
    > "Manish Bafna" wrote:
    > > Hi,
    > > Try either of this code:

    >
    > > Method 1 :
    > > DropDownList1.SelectedIndex =
    > > DropDownList1.Items.IndexOf(DropDownList1.Items.FindByValue(reader["region_­id"].ToString()));

    >
    > > //DropDownList1.SelectedIndex =
    > > DropDownList1.Items.IndexOf(DropDownList1.Items.FindByText(<Text>));

    >
    > > or

    >
    > > Method 2 :
    > > DropDownList1.Items.FindByValue(reader["region_id"].ToString()).Selected =
    > > true ;

    >
    > > //DropDownList1.Items.FindByText("<Text>").Selected = true ;

    >
    > > If my answer helped you,then please do press Yes below.
    > > Thanks and Regards.
    > > Manish Bafna.
    > > MCP and MCTS.

    >
    > > "Brenden Bixler" wrote:

    >
    > > > Hello.

    >
    > > > I am reading a value from a table and trying to determine if that value
    > > > exists in a list of values associated with a dropdownlist. If so, I select
    > > > the value, otherwise, I don't. I haven't been able to figure this operation
    > > > out so far.

    >
    > > > Code:

    >
    > > > if (reader["region_id"] != System.DBNull.Value)
    > > > {
    > > > if
    > > > (ddlApplicantRM.Items.Contains(ddlApplicantRM.Items.FindByValue(reader["reg­ion_id"].ToString())) == true)
    > > > {
    > > > ddlApplicantRM.SelectedValue =
    > > > reader["region_id"].ToString();
    > > > }
    > > > else
    > > > {
    > > > ddlApplicantRM.SelectedValue = "0";
    > > > }
    > > > }

    >
    > > > Even though the value clearly exists in the list of values attached to the
    > > > dropdown, it never selects the value in the dropdown correctly. Is this the
    > > > proper way of doing what I am trying to accomplish? Does it matter if
    > > > region_id is an INT and the comparison function requires a string?

    >
    > > > Thanks in advance for any suggestions or help.- Hide quoted text -

    >
    > - Show quoted text -


    ddlApplicantRM.Items.FindByValue(reader["reg­
    ion_id"].ToString()).Selected = true;

    should work! If it doesn't - check what value the reader has there.

    try {
    ddlApplicantRM.Items.FindByValue(reader["reg­
    ion_id"].ToString()).Selected = true;
    } catch {
    ddlApplicantRM.SelectedValue = "0";
    }

    Regarding your code

    Items.Contains requires a ListItem, so:

    if (ddlApplicantRM.Items.Contains(new ListItem("...text here...",
    reader["reg­ion_id"].ToString()))) {
    ...........

    where "...text here..." is the text of the list item, I don't know if
    it's the same as reader["reg­ion_id"], or not...
    Alexey Smirnov, Mar 11, 2007
    #4
  5. Re: Dropdown List - Check if value exists in list prior to selecti

    Hi,
    Last thing you can try is using Trim():
    ddlApplicantRM.Items.FindByValue(reader["reg-
    > ion_id"].ToString().Trim()).Selected = true;


    please let me know if now it is working or not
    --
    If my answer helped you,then please do press Yes below.
    Thanks and Regards.
    Manish Bafna.
    MCP and MCTS.



    "Alexey Smirnov" wrote:

    > On Mar 11, 7:36 pm, Brenden <> wrote:
    > > I appreciate you taking a stab at the solution, both both failed.
    > >
    > > The first simply did not work. The second causes the page to render
    > > incorrectly.
    > >
    > >
    > >
    > > "Manish Bafna" wrote:
    > > > Hi,
    > > > Try either of this code:

    > >
    > > > Method 1 :
    > > > DropDownList1.SelectedIndex =
    > > > DropDownList1.Items.IndexOf(DropDownList1.Items.FindByValue(reader["region_-id"].ToString()));

    > >
    > > > //DropDownList1.SelectedIndex =
    > > > DropDownList1.Items.IndexOf(DropDownList1.Items.FindByText(<Text>));

    > >
    > > > or

    > >
    > > > Method 2 :
    > > > DropDownList1.Items.FindByValue(reader["region_id"].ToString()).Selected =
    > > > true ;

    > >
    > > > //DropDownList1.Items.FindByText("<Text>").Selected = true ;

    > >
    > > > If my answer helped you,then please do press Yes below.
    > > > Thanks and Regards.
    > > > Manish Bafna.
    > > > MCP and MCTS.

    > >
    > > > "Brenden Bixler" wrote:

    > >
    > > > > Hello.

    > >
    > > > > I am reading a value from a table and trying to determine if that value
    > > > > exists in a list of values associated with a dropdownlist. If so, I select
    > > > > the value, otherwise, I don't. I haven't been able to figure this operation
    > > > > out so far.

    > >
    > > > > Code:

    > >
    > > > > if (reader["region_id"] != System.DBNull.Value)
    > > > > {
    > > > > if
    > > > > (ddlApplicantRM.Items.Contains(ddlApplicantRM.Items.FindByValue(reader["reg-ion_id"].ToString())) == true)
    > > > > {
    > > > > ddlApplicantRM.SelectedValue =
    > > > > reader["region_id"].ToString();
    > > > > }
    > > > > else
    > > > > {
    > > > > ddlApplicantRM.SelectedValue = "0";
    > > > > }
    > > > > }

    > >
    > > > > Even though the value clearly exists in the list of values attached to the
    > > > > dropdown, it never selects the value in the dropdown correctly. Is this the
    > > > > proper way of doing what I am trying to accomplish? Does it matter if
    > > > > region_id is an INT and the comparison function requires a string?

    > >
    > > > > Thanks in advance for any suggestions or help.- Hide quoted text -

    > >
    > > - Show quoted text -

    >
    > ddlApplicantRM.Items.FindByValue(reader["reg-
    > ion_id"].ToString()).Selected = true;
    >
    > should work! If it doesn't - check what value the reader has there.
    >
    > try {
    > ddlApplicantRM.Items.FindByValue(reader["reg-
    > ion_id"].ToString()).Selected = true;
    > } catch {
    > ddlApplicantRM.SelectedValue = "0";
    > }
    >
    > Regarding your code
    >
    > Items.Contains requires a ListItem, so:
    >
    > if (ddlApplicantRM.Items.Contains(new ListItem("...text here...",
    > reader["reg-ion_id"].ToString()))) {
    > ...........
    >
    > where "...text here..." is the text of the list item, I don't know if
    > it's the same as reader["reg-ion_id"], or not...
    >
    >
    >
    =?Utf-8?B?TWFuaXNoIEJhZm5h?=, Mar 12, 2007
    #5
  6. Re: Dropdown List - Check if value exists in list prior to selecti

    None of the solutions provided previously worked, but I finally figured it
    out. Thanks to all for helping me reach success.

    On the front-end page (file.aspx), I have the dropdown control bound to a
    SQL Data Source. The portion of my code that checks for the value in the
    dropdown list (in the code behind, file.aspx.cs) is always encountering an
    empty dropdown list, because the code to pull the values into the dropdown
    list hasn't yet executed.

    To fix, I did the following:

    // nullify the existing connection
    ddlApplicantRM.DataSourceID = null;

    // reconnect in the code-behind
    ddlApplicantRM.DataSource = sqlRM2;

    // force the dropdown to load the items
    ddlApplicantRM.DataBind();

    // Now, check for the value against a full list of values
    try
    {

    ddlApplicantRM.Items.FindByValue(reader["region_id"].ToString().Trim()).Selected = true;
    }
    catch
    {
    ddlApplicantRM.SelectedValue = "0";
    }
    =?Utf-8?B?QnJlbmRlbg==?=, Mar 20, 2007
    #6
    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. Oleg Ogurok

    Reformat TextBox value prior validation

    Oleg Ogurok, Aug 19, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    3,491
    Oleg Ogurok
    Aug 20, 2005
  2. Phoenix
    Replies:
    3
    Views:
    1,294
    Joris Gillis
    Dec 30, 2004
  3. Jason Stacy
    Replies:
    2
    Views:
    4,640
    Roedy Green
    Apr 27, 2008
  4. Ulf Meinhardt
    Replies:
    8
    Views:
    6,089
  5. curiousity

    HOWTO autopost the selection list upon selection

    curiousity, Nov 21, 2003, in forum: ASP .Net Mobile
    Replies:
    0
    Views:
    255
    curiousity
    Nov 21, 2003
Loading...

Share This Page