The headline is not good. I don’t know how anything better to write in the headline. But here it comes.
I am making a soccerfield with all the different positions. On each position I have a dropdownlist that shows the players in the team.
I made parts of the dropdownlists and came to realised that I was doing it wrong.
try
{
int pos = list[i].positionsID;
if (pos == 1)
{
ddlGoal.Visible = true;
lblGoal.Visible = true;
ddlGoal.DataSource = list1;
ddlGoal.DataValueField = "id";
ddlGoal.DataTextField = "name";
ddlGoal.DataBind();
}
} catch ...
It should only show the position of there are any players on that position. It all works fine… but i have 28 positions, and there for 28 of the above try/catch things.
I want to scale it down, by only making one.
If i let all the dropdownlists have the same name as the postions saved on my database I should be able to make just one. So i have tried that:
try
{
int pos = list[i].positionsID;
if (pos != 0)
{
string ddl = "ddl" + list3[i].positionsNavn.ToString();
ddl.Visible = true;
lblGoal.Visible = true;
ddlGoal.DataSource = list1;
ddlGoal.DataValueField = "id";
ddlGoal.DataTextField = "name";
ddlGoal.DataBind();
}
} catch...
It doesn’t work. How do i get hold of the dropdownlist with ID ddlGoalKeeper when i am creating the ID as:
string ddl = "ddl" + list3[i].positionsNavn.ToString();
Any ideas or help??
I think this is what you need. Given the ID of any control you can use
http://msdn.microsoft.com/en-us/library/system.web.ui.control.findcontrol.aspx
to get any control in the control tree. You wil need to use it on the naming container that contains the control i.e.
or use the recursive version wriiten by a certain Jeff Atwood
http://www.codinghorror.com/blog/2005/06/recursive-pagefindcontrol.html
Alternatively – construct a Dictionary collection will all your dropdowns mapped to IDs and get them for this. Could be more efficient with more setup