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

G

Guest

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

Guest

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

Guest

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 said:
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 said:
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.
 
G

Guest

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 said:
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>));

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

Guest

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.



Anon User said:
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 said:
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>));

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.

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.

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

Guest

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";
}
 

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

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top