'SelectedIndex' and 'SelectedValue' attributes are mutually exclusive (part 2)

S

Stimp

Hi all,

I've come back to this problem again and I've identified which part of
my code is producing the error:

"'SelectedIndex' and 'SelectedValue' attributes are mutually exclusive"


I have a dropdown 'Year' and it contains a list of years from 2000 to
2005. This dropdown is populated on page load.

When I select an article to edit (which has a year value associated), I
select the appropriate year dropdown value as follows...

ddYear.SelectedValue = objRS("VYear")


This is causing the above error, I THINK it's because the 'VYear' value
is an integer as opposed to a string.

I've also tried: ddYear.SelectedValue = CInt(objRS("VYear"))
and ddYear.SelectedValue = CStr(objRS("VYear"))

with the same error resulting.

Any ideas?

Thanks.
Peter
 
G

Greg Burns

Does this work?

Dim li As ListItem = ddYear.Items.FindByValue(CStr(objRS("VYear")))
If Not li Is Nothing Then
li.Selected = True
End If

Greg
 
S

Stimp

Does this work?

Dim li As ListItem = ddYear.Items.FindByValue(CStr(objRS("VYear")))
If Not li Is Nothing Then
li.Selected = True
End If

Greg

doesn't seem to... is there a way to set the SelectedIndex instead of
the SelectedValue if all I have is the text value?
 
G

Greg Burns

Put a breakpoint on "li.Selected = True". Does the debugger ever even get
there? If not, then it is not even finding that value in your current
listitems, hence cannot select it.
 
S

Stimp

Put a breakpoint on "li.Selected = True". Does the debugger ever even get
there? If not, then it is not even finding that value in your current
listitems, hence cannot select it.

my mistake, it DOES indeed work.. the new error I was receiving was
separate to the original problem, which your code fixed.

Thanks!

It's still very unusual that you need to use this method to set the
selectedvalue when the text field in your dropdownlist is numerical
*shrug*
 
G

Greg Burns

Not sure I understand, but the Value and Text property of you ListItem are
both strings. You have to convert whatever object you want to be a ListItem
into a string to add it to the collection.

ddYear.Items.Insert(0, New ListItem("displayedtext", "actualvalue"))

This code insert in position 0 a new new ListItem. The constructor for
ListItem only takes strings.

To tell you the truth, I am not sure why you are having a problem with:

ddYear.SelectedValue = objRS("VYear").ToString

Greg
 

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

Top