DropDownList DataBinding firing twice. Please help!

H

hwiechers

I have two drop down lists on a page. Each one is hooked up to a
separate SqlDataSource. The second data source has a ControlParameter
set to the selected value of the first drop down. When I set the
SelectedValue of the first drop down in Page_Load, the DataBinding
event of the second fires twice. This causes the second drop down to
have duplicate values. Try as I might I haven't been able to get this
to work. Does anyone have a solution for this problem?
 
C

Cowboy \(Gregory A. Beamer\)

The first thing is the problem: dupe values. You can cure this by clearing
the list in each bind. It will still fire twice, which should be solved, but
you will avoid the dupes. (BTW, dupes are not really a bad thing for system
operation, as you will end up with the right answer; but, they sure are a
major annoyance to the people who pay our paychecks ;->).

The second thing is firgure out what is causing the double binig. My guess
is you have the first dropdown set to populate the second onChange and you
are binding the second first, with a default set of values, and then the
first, which then fires its event. Have not seen this in ASP.NET, but I
regard it as a possibility. If this is correct, you need to reorder the
bindings OR attach the postback event after you have bound control #1.

Without having code to debug through, I can only guess, however.
 
H

hwiechers

I think I've solved the problem. I wasn't actually calling DataBind at
all, just relying on the automatic databinding. I found that if I
called DataBind myself the automatic databinding wouldn't fire and the
thing would work. So now I just call DataBind on each DropDownList as I
set them. (This is only when !IsPostBack. It seems to work correctly
during postbacks.)
 
Joined
Dec 6, 2010
Messages
2
Reaction score
0
Hi,

I'm also having this problem. I have two DropDownLists in a user control, the second with a Controlparameter in the SqlDataSource feeding from the SelectedValue of the first. When I set the SelectedValues of the DropDownLists , the second one has duplicate entries. I have tried clearing the entries and databinding again, but get an error saying the selectedvalue is not in the list of items. I tried this in the Page_Load of the user control as well as the SelectedIndexChanged of the first DropDownList, still the same error.

Any further ideas would be great.
Edit: I tried removing the DropDownLists from the user control and placing them into a normal .aspx page and it worked fine, which suggests that the user control is the issue.
 
Last edited:
Joined
Dec 7, 2010
Messages
6
Reaction score
0
Here is a simple solution
Just call you drop down bind functionality in isPostback function

If isPostback = false then
' Function to bind Data to Dropdown
End if.
 
Joined
Dec 7, 2010
Messages
6
Reaction score
0
Here is a simple solution
Just call you drop down bind functionality in isPostback function

If isPostback = false then
' Function to bind Data to Dropdown
End if.
 
Joined
Dec 6, 2010
Messages
2
Reaction score
0
patil.sandiip said:
Here is a simple solution
Just call you drop down bind functionality in isPostback function

If isPostback = false then
' Function to bind Data to Dropdown
End if.

If I put that in my Page_Load I get the following error:

'ddlCode' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value.

When the user arrives at this page it is because they have selected a record to edit. I get the record details from the database and populate all controls. I am setting the SelectedValue of this DropDownList, but when I do it has duplicate entries. Even if I don't call DataBind and I don't set the value in the DropDownList, it still has duplicate entries. When I set the value in the first DropDownList it is calling DataBind twice on the second DropDownList.
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top