Advanced DropDownList in DetailsView binding to ArrayList

Last post 04-18-2007 10:13 PM by se_gordon. 8 replies.

Sort Posts:

  • Advanced DropDownList in DetailsView binding to ArrayList

    12-18-2006, 10:04 PM
    • Member
      5 point Member
    • ngenius
    • Member since 12-19-2006, 2:33 AM
    • Posts 8

    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


     

  • Re: Advanced DropDownList in DetailsView binding to ArrayList

    12-18-2006, 10:52 PM
    • Star
      7,685 point Star
    • agolden
    • Member since 08-03-2002, 10:56 AM
    • Houston, TX
    • Posts 1,036

    The key here is that the at Page_Load (before postback), only the controls in the template of DetailsViews for the default mode are instanciated.  I expect if you ran your code with the default mode set to edit, it would find the control.  Assuming the default mode is ReadOnly, the controls in the EditItemTemplate are instanciated after the mode is changed to edit and the DetailsView is databound.

    Two options you could try:

    1. Try to find the control in the DetailsView OnDataBound event if the mode is edit

    2. Populate the data in one of the DropDownList's events (e.g. load)

    Hope that helps.

    Aaron

    Don't forget to click "Mark as Answer" on the post that helped you. This credits that member, earns you a point and marks your thread as Resolved so everyone will know you have been helped.
  • Re: Advanced DropDownList in DetailsView binding to ArrayList

    12-18-2006, 11:02 PM
    Answer
    • Star
      9,373 point Star
    • rexlin
    • Member since 07-17-2006, 8:43 AM
    • Posts 1,751

    Hi, ngenius :

    Is it the main problem that you can not access the ddl in the insert and edit template?

    If so, may this helps:

    foreach (DetailsViewRow dr in this.DetailsView1.Rows)
            {
                if (dr.RowType == DataControlRowType.DataRow)
                {
                    if (dr.RowState == DataControlRowState.Edit || dr.RowState == DataControlRowState.Insert)
                    {
                        DropDownList DDL = (DropDownList)dr.FindControl("DropDownListID");
                        globalList.bindListControlToGlobalSelectionList(DDL);
                    }
                }
            }



    Best Regards,
    __________________________________________________
    Sincerely,
    Rex Lin
    Microsoft Online Community Support

    This posting is provided "AS IS" with on warranties, and confers no rights.
  • Re: Advanced DropDownList in DetailsView binding to ArrayList

    12-19-2006, 10:11 AM
    • Member
      5 point Member
    • ngenius
    • Member since 12-19-2006, 2:33 AM
    • Posts 8

    Thanks gang. Both responses provided some good insight into the issue.

    I guess now my main question is exactly which event is the best event to place the code in?

     Should I be using the ItemCreate Event of the DetailsView control, the or the OnLoad event of the DropDownList?

     Is there a performance issue with using one of the above or any other variation?

     Thanks in advance.

  • Re: Advanced DropDownList in DetailsView binding to ArrayList

    12-19-2006, 9:24 PM
    Answer
    • Star
      7,685 point Star
    • agolden
    • Member since 08-03-2002, 10:56 AM
    • Houston, TX
    • Posts 1,036
    I'd probably go with the OnLoad on the DropDownList, which will fire no more than once per post.  The DetailsView OnItemCreated can fire several times.  For example, on any post that changes the mode (edit, update, cancel), it's fired once before ProcessPostData with the original mode, then again after the mode is changed with the newnew mode.
    Don't forget to click "Mark as Answer" on the post that helped you. This credits that member, earns you a point and marks your thread as Resolved so everyone will know you have been helped.
  • Re: Advanced DropDownList in DetailsView binding to ArrayList

    01-18-2007, 4:15 AM
    • Member
      79 point Member
    • bhav27
    • Member since 06-07-2006, 4:10 PM
    • Posts 96

    I'm in the situation. I think calling this code in ItemCreated is much preferable. Also if you call this even everytime you request edit to then use OnModeChange event.

    I'll test this and post back the results of my test.

    Thanks guys.
     

  • Re: Advanced DropDownList in DetailsView binding to ArrayList

    01-18-2007, 5:44 AM
    • Member
      79 point Member
    • bhav27
    • Member since 06-07-2006, 4:10 PM
    • Posts 96
    rexlin:

    Hi, ngenius :

    Is it the main problem that you can not access the ddl in the insert and edit template?

    If so, may this helps:

    foreach (DetailsViewRow dr in this.DetailsView1.Rows)
            {
                if (dr.RowType == DataControlRowType.DataRow)
                {
                    if (dr.RowState == DataControlRowState.Edit || dr.RowState == DataControlRowState.Insert)
                    {
                        DropDownList DDL = (DropDownList)dr.FindControl("DropDownListID");
                        globalList.bindListControlToGlobalSelectionList(DDL);
                    }
                }
            }

     

    You can also use ModeChanging event like this

     

     

      protected void dtlSample_ModeChanging(object sender, DetailsViewModeEventArgs e)
      {
        if (e.NewMode == DetailsViewMode.Edit)
        {
          DropDownList ddlGetAge = (DropDownList)dtlSample.FindControl("ddlAge");
          LoadDropdownbox(ddlGetAge);
        }
      }
    
      But the only problem is
    (DropDownList)dtlSample.FindControl("ddlAge"); return null. Does anyone know the reason why DetailsView cannot find controls within it?
  • Re: Advanced DropDownList in DetailsView binding to ArrayList

    01-18-2007, 9:32 AM
    • Star
      7,685 point Star
    • agolden
    • Member since 08-03-2002, 10:56 AM
    • Houston, TX
    • Posts 1,036

    The problem has to do with when the DropDownList is created.  When ModeChanging is fired, it's before the transition to edit mode, the DetailsView is still in ReadOnly mode, and controls in the EditItemTemplates have not been created.  Even ModeChanged, which is after the transition to Edit mode, is before the controls in the EditItemTemplates are created.

    To really see all this in action, I set up a simple test page with a DetailsView with methods for all of these various events that checks for the existence of the ItemTemplate or EditItem template controls and writes a status to the Trace, so you can see the whole sequence.

    <%@ Page Language="C#" Trace="true" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <script runat="server">
    
    	protected void DetailsView1_DataBound(object sender, EventArgs e)
    	{
    		Label StartedLabel = (Label)DetailsView1.FindControl("StartedLabel");
    		TextBox StartTextbox = (TextBox)DetailsView1.FindControl("StartTextbox");
    
    		string message = "";
    		if (StartedLabel != null) message += "label: " + StartedLabel.Text;
    		if (StartTextbox != null) message += "textbox: " + StartTextbox.Text;
    		Trace.Warn("DetailsView1_DataBound", message);
    	}
    
    	protected void Page_Load(object sender, EventArgs e)
    	{
    		Label StartedLabel = (Label)DetailsView1.FindControl("StartedLabel");
    		TextBox StartTextbox = (TextBox)DetailsView1.FindControl("StartTextbox");
    
    		string message = "";
    		if (StartedLabel != null) message += "label: " + StartedLabel.Text;
    		if (StartTextbox != null) message += "textbox: " + StartTextbox.Text;
    		Trace.Warn("Page_Load", message);
    	}
    
    	protected void DetailsView1_ModeChanged(object sender, EventArgs e)
    	{
    		Label StartedLabel = (Label)DetailsView1.FindControl("StartedLabel");
    		TextBox StartTextbox = (TextBox)DetailsView1.FindControl("StartTextbox");
    
    		string message = "";
    		if (StartedLabel != null) message += "label: " + StartedLabel.Text;
    		if (StartTextbox != null) message += "textbox: " + StartTextbox.Text;
    		Trace.Warn("DetailsView1_ModeChanged", message);
    	}
    
    	protected void StartTextbox_Load(object sender, EventArgs e)
    	{
    		TextBox StartTextbox = (TextBox)sender;
    
    		string message = "";
    		if (StartTextbox != null) message += "textbox: " + StartTextbox.Text;
    		Trace.Warn("StartTextbox_Load", message);
    	}
    
    	protected void DetailsView1_ItemCreated(object sender, EventArgs e)
    	{
    		Label StartedLabel = (Label)DetailsView1.FindControl("StartedLabel");
    		TextBox StartTextbox = (TextBox)DetailsView1.FindControl("StartTextbox");
    
    		string message = "";
    		if (StartedLabel != null) message += "label: " + StartedLabel.Text;
    		if (StartTextbox != null) message += "textbox: " + StartTextbox.Text;
    		Trace.Warn("DetailsView1_ItemCreated", message);
    	}
    
    	protected void DetailsView1_Load(object sender, EventArgs e)
    	{
    		Label StartedLabel = (Label)DetailsView1.FindControl("StartedLabel");
    		TextBox StartTextbox = (TextBox)DetailsView1.FindControl("StartTextbox");
    
    		string message = "";
    		if (StartedLabel != null) message += "label: " + StartedLabel.Text;
    		if (StartTextbox != null) message += "textbox: " + StartTextbox.Text;
    		Trace.Warn("DetailsView1_Load", message);
    	}
    </script>
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
     <title>Untitled Page</title>
     <style type="text/css">
    		body {
    			font: 1em Verdana;
    		}
     </style>
    </head>
    <body>
     <form id="form1" runat="server">
     <div>
    		<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
    			SelectCommand="SELECT [ID], [Name], [Started] FROM [DateTest2]" 
    			UpdateCommand="UPDATE DateTest2 SET Name=@Name, Started=@Started WHERE ID=@ID" 
    			InsertCommand="INSERT INTO DateTest2 (Name, Started) VALUES (@Name, @Started)">
    			<UpdateParameters>
    				<asp:Parameter Name="Name" />
    				<asp:Parameter Name="Started" />
    				<asp:Parameter Name="ID" />
    			</UpdateParameters>
    			<InsertParameters>
    				<asp:Parameter Name="Name" />
    				<asp:Parameter Name="Started" />
    			</InsertParameters>
    		</asp:SqlDataSource>
    		<asp:DetailsView ID="DetailsView1" runat="server" AllowPaging="True" AutoGenerateRows="False"
    			DataSourceID="SqlDataSource1" Height="50px" DataKeyNames="ID" DefaultMode="ReadOnly"
    			AutoGenerateEditButton="true" AutoGenerateInsertButton="true" Width="500px" PagerSettings-Mode="NextPreviousFirstLast"
    			OnDataBound="DetailsView1_DataBound" OnModeChanged="DetailsView1_ModeChanged" OnItemCreated="DetailsView1_ItemCreated" OnLoad="DetailsView1_Load">
    			<Fields>
    				<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True"
    					SortExpression="ID" />
    				<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
    				<asp:TemplateField HeaderText="Started" SortExpression="Started">
    					<EditItemTemplate>
    						<asp:TextBox ID="StartTextbox" runat="server" Text='<%# Bind("Started") %>' OnLoad="StartTextbox_Load"></asp:TextBox>
    					</EditItemTemplate>
    					<InsertItemTemplate>
    						<asp:TextBox ID="StartTextbox" runat="server" Text='<%# Bind("Started") %>' OnLoad="StartTextbox_Load"></asp:TextBox>
    					</InsertItemTemplate>
    					<ItemTemplate>
    						<asp:Label ID="StartedLabel" runat="server" Text='<%# Bind("Started") %>'></asp:Label>
    					</ItemTemplate>
    				</asp:TemplateField>
    			</Fields>
    		</asp:DetailsView>
     
     </div>
     </form>
    </body>
    </html>
    
     
    Don't forget to click "Mark as Answer" on the post that helped you. This credits that member, earns you a point and marks your thread as Resolved so everyone will know you have been helped.
  • Re: Advanced DropDownList in DetailsView binding to ArrayList

    04-18-2007, 10:13 PM
    • Member
      17 point Member
    • se_gordon
    • Member since 02-07-2007, 4:21 PM
    • Posts 48

    I had an analogous problem

    http://forums.asp.net/1672073/ShowThread.aspx#1672073

     which was solved by reading, and re-reading, this thread.  Thank you to everyone who participated here.  In researching my problem I found this thread has been valuable to a number of other posts as well.

     

Page 1 of 1 (9 items)