nested dropdownlist loses values - only in one instance

B

Bryan Ax

I have a page that has two copies of the same control in it. The
control is:

BorrowerControl

the two references to it in the page are

bControl1
cobControl1

The control contains a dropdownlist that is populated from xml. I only
bind this data on page_load, not on subsequent postbacks (shouldn't
need to). Code to bind (where this.States is a dataset on the page)
is:

foreach(DataRow d in this.States.Tables[0].Rows)
{
string txt = d["abv"].ToString();
string val = d["abv"].ToString();
li = new ListItem(txt, val);
control.Items.Add(li);
}
control.DataBind();

I'm getting some REALLY strange behavior. The dropdownlist in
bControl1 maintains its values on postback, but the dropdownlist in
cobControl1 does not. I haven't found anything that says I can't have
multiple copies of the same control on the page, or that would explain
this type of erratic behavior. Can someone shed some light on this
situation? I can provide more detail if necessary. And no, I don't
have enableViewState = false on either instance of the control.

Sincerely,

Bryan Ax
 
N

Natty Gur

Hi,

Is that custom server control? if so, did you implement IStateManager,
and how.

by the way why you are using DataBind. you add explicitly items to items
collection. you don't bind your control to any datasource.

Natty Gur[MVP]

blog : http://weblogs.asp.net/ngur
Mobile: +972-(0)58-888377
 
B

Bryan Ax

It's a user control (.ascx), not a custom server control. It does use
a custom implementation of viewstate, saving to a database. However,
that doesn't explain to me why one instance of it would work ok, and
the other not.

Databind was commented out from an earlier attempt. Forgot to include
that.

Bryan
 
B

Bryan Ax

More info....

I removed the custom viewstate implementation, just went back to
having the page extend from System.Web.UI.Page. Still didn't work. Now
the page stores viewstate just like any other, and it is still not
functional.
 
B

Bryan Ax

OK, I think I've identified where the problem happens, but I don't
understand the why. Hopefully, someone can fill me in.

The following code fails. All I'm trying to do is populate two
dropdownlists from the same set of items (in my real case, a list of
states, but this is just a test).

When I execute the following, the first one fills just fine, but the
second one does not, i.e. the line this.loadDdl(this.DropDownList2)
does not populate the dropdownlist with items. Can someone please
explain to me why it doesn't work? I'm really missing something that
must be obvious.


private void loadDdl(DropDownList control)
{
ListItem li = new ListItem("Hello", "world");
control.Items.Add(li);
li = new ListItem("Goo", "ber");
control.Items.Add(li);
li = new ListItem("gee", "whiz");
control.Items.Add(li);
}

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if (! this.Page.IsPostBack)
{
this.loadDdl(this.DropDownList1);
this.loadDdl(this.DropDownList2);
}
}
 
B

Bryan Ax

Actually, my last post was incorrect. That did work OK - just hadn't
recompiled.

However, this time I have figured it out. On the user control that's
problematic, I'm removing a validation control, i.e.

this.Controls.Remove(this.ValidationControl);

during the page_load event.

That line of code causes the problem. No clue as to why removing one
control would affect others on the page, and ONLY dropdownlists on the
page (other controls like textboxes are not affected). The validation
control does not apply to the dropdownlist at all.

I tried moving that line to prerender, still broke it. I've gotten
around it by just setting the enabled of it to false, but it still
doesn't explain the why to me.

Anyone?
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top