I am some what of a newbie to ASP.NET 2.0 but no stranger to .Net. I am stumped on implementing a template dropdownlist when attempting to bind to an ArrayList object the DropDownList to a object via code behind.
I am trying to create a global method in a class that allows a listControl to be passed in, have its listItem properties filled and then return the object to the calling page with a populated list.
Here is my class (globalList.cs):
public static class globalList
{
static globalList()
{
}
public static void bindListControlToGlobalSelectionList(ListControl listControl)
{
listControl.Items.Add(new ListItem("No", "0"));
listControl.Items.Add(new ListItem("Yes", "1"));
}
}
The drive here is to be able to manipulate the List (DropDownList) object in the DetailsView object using only the codebehind.
The problem is that this works absolutely fine in a standard web form even when using the codebehind but I cannot get it to function while the List (DropDownList) object is in the <InsertITemTemplate> ir tge <EditItemTemplate> of the DetailView.
Here is the code that works (A simple web form and a simple method calling script in the code behind):
<asp:DropDownList ID="testDD" runat="Server"></asp:DropDownList>
protected void Page_Load(object sender, EventArgs e)
{
globalList.bindListControlToGlobalSelectionList(testDD);
}
.....And that works beautifully. Which is why I DO NOT understand why I can't get it to work in the DetailsView scenario as broken down below:
<asp:TemplateField HeaderText="Active:">
<EditItemTemplate>
<asp:DropDownList ID="updActiveDD" runat="server" '>
<asp:ListItem Value="True">Just Trying To Ad An Item To See What Happens</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<InsertItemTemplate>
<asp:DropDownList ID="insActiveDD" runat="server"></asp:DropDownList>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="label1" runat="server" text='<%# Bind("isActive")>' />
</ItemTemplate>
</asp:TemplateField> ANd in the codebehind...
protected void Page_Load(object sender, EventArgs e)
{
DropDownList myDD = (DropDownList)detailsViewUsers.FindControl("updActiveDD"); //for edit template
globalList.bindListControlToGlobalSelectionList(myDD);
}
I've listed the code above for brevity but basically I cannot get the template to add the listitems to the dropdownlist at all in the edit or insert templates.
I have looked at a lot of near similar inquiries to this an none are using an ArrayList via class object to bind the List control like I am trying to do.
Is this impossible to do using a DetailsView control and should I be using a FormView?
Can someone please provide an answer or some direction on this? Some full blown example code would be more than welcome.
Again, I understand how to bind the list using a datasource control as well as a db datasource locally but this was an aim at a more object oriented approach.
Thanks for the help.
ngenius