Hello all,
I have for two days now been trying to debug what appears to be a serious logical error in my code, now I think I need to make it public. I am working on a dynamically loaded user control, this usercontrol has two dropdownlists, followed by textboxes.The second dropdownlist is populated based on the selectedindex of the first dropdwonlist.
What I do is that, when the selectedindex of the first dropdownlist changes, I clear the items in the second list, then populate it.based on the selected index of dropdownlistbox.
Now the textboxes are all set to visibility=false. This is because the textboxes to show will depend on the selected index of the second dropdownlist box. The problem is that each time I select an item from the second drop downlistbox, the selected index of the second drop that listbox (this determines the textboxes to reveal) shows -1, instead of the actual selected index of the dropdownlist box, which I expect to be 0, 1,2 etc. and this courses the selected index of the second listbox to always reset back to the first item, causing the textboxes to be revealed wrongly. Idont know if am clear enuff?
Both listboxes are in ajax update panels.

Here a portion of my code if u would like to see:
//This is the first listbox that Loads the second listbox

protected void ListBoxProjects_SelectedIndexChanged(object sender, EventArgs e)
{
//Clear second listbox
ListBoxSources.Items.Clear();
//populate second listbox
LoadControlByProject();

}

//This is the method that loads the second list box, called in selectedIndexChanged above
protected void LoadControlByProject()
{
//Clear second listbox
//ListBoxSources.Items.Clear();

//Call method that returns linq quert result
foreach (var i in ControlLoader.GetSource(lstProjectsId.ElementAt(ListBoxProjects.SelectedIndex)))
{
// ddlSource.Items.Add(i.SourceName);
ListBoxSources.Items.Add(i.SourceName);

}
}

//This is the selectedIndex_changed of the second list box

protected void ListBoxSources_SelectedIndexChanged(object sender, EventArgs e)
{

//This sets a static int that will hold the selected index
lstSelectIndex = ListBoxSources.SelectedIndex;

//This sets the visibility of my textboxes
ToggleControls();
//Clear ListBoxSources
ListBoxSources.Items.Clear();
//This is meant to populate the second listbox again, paasing the
//The selected index as a parameter, so that the selected item
// remains selected (custom method)
LoadControlByProject(lstSelectIndex);

}

Any hint appreciated, thanks

Recommended Answers

All 2 Replies

Hello ykel,

I guess you are using a placeholder to load control.

As you say that you get a value of -1 instead of selected value, it means your ddl control get lost while rendering. To solve this do the followings
1)Please check your UC is maintained in every post back.
2) Now try adding ID for the control you load in placeholder.

Dim myTestUc As Control = LoadControl("myTestUc.ascx")
myTestUc.ID = "myTestUc"
phContainer.Controls.Add(myTestUc)

Hope this works
Thanks

Thanks for the reply, I have had code in the code behind of the parent page that does that already, but the problem still perisists...Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.