Dynamic Formview with a dropdown list fails
<p>188 Points</p>

<p>400 Posts</p>

<p>Dynamic Formview with a dropdown list fails horribly.</p>

<p>6 hours, 21 minutes ago|LINK</p>

<p>I've got concept in my head that I'm trying to materialize and it's not working. Here is what needs to happen: User selects a item from a list. Based on the selection a formview is built dynamically. The formview needs to be dynamic because the query will return a dataset with null values among non-null values such as this: </p>

<p>4600, 1, 4, NULL, NULL, 68 .... </p>

<p>The "4600" is model number and everything else is a ID that corresponds to a component. The Null values means that this filed does not belong to the 4600. </p>

<p>Henceforth, the formview then is build such that "4600" is fed to a label. For each non-null value I need to build a dropdown list, each with a separate datascource, not the ODS stuff, but a call to BLL class. Then the non-null value is assigned to the ddl's selected value property. </p>

<p>Simple enough, no? So here is the code and it's failing horribly. Actually just times out in an infinite loop. The time out happens in the ddlTonerBlack_DataBinding method. Can someone tell me what I'm doing wrong? Thanks. EJM</p>

<p>aspx markup:</p>

<pre><code>&lt;form id="form1" runat="server"&gt; 
&lt;div&gt; 
    &lt;asp:DropDownList runat="server" ID="ddlPrinterModels" AppendDataBoundItems="True" 
        DataTextField="Hardware_Model" DataValueField="Hardware_Model" 
        width="246px" CssClass="AssetMngnt-smallFont" AutoPostBack="true" &gt; 
            &lt;asp:ListItem Value="-1" Selected="True"&gt;-- Select Printer Model --&lt;/asp:ListItem&gt; 
    &lt;/asp:DropDownList&gt; 
    &lt;hr /&gt; 

    &lt;asp:PlaceHolder id="DetailsViewPlaceHolder" runat="server"/&gt; 

&lt;/div&gt; 
&lt;!-- NOT A COMPLETE QUERY --&gt; 
&lt;asp:sqldatasource id="ODSTonerBlackByModel" 
    selectcommand="SELECT [Hardware_Model], [Ident_Black], [Ident_Cyan], [Ident_Yellow] FROM [cPrinters_Toners] WHERE ([Hardware_Model] = @Hardware_Model)" 
    connectionstring="&lt;%$ ConnectionStrings:CISF_Asset_Management %&gt;" 
    runat="server"&gt; 
    &lt;SelectParameters&gt; 
        &lt;asp:ControlParameter ControlID="ddlPrinterModels" Name="Hardware_Model" 
            PropertyName="SelectedValue" Type="String" /&gt; 
    &lt;/SelectParameters&gt; 
&lt;/asp:SqlDataSource&gt;  
&lt;/form&gt;
</code></pre>

<p>Now the code file:</p>

<pre><code>public partial class Default2 : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
        if (!IsPostBack)  
        { 
            LoadData_PrinterModels();  
        } 

        FormView printerModelFormView = new FormView(); 

        dalConsumables_TonerBlack x = new dalConsumables_TonerBlack(); 

        printerModelFormView.ID = "fvPrinterModel"; 
        printerModelFormView.DataSourceID = "ODSTonerBlackByModel"; 

        printerModelFormView.PagerSettings.Mode = PagerButtons.NextPrevious; 
        printerModelFormView.HeaderText = "Printer Model"; 

        printerModelFormView.ItemTemplate = new FormViewTemplate(); 

        DetailsViewPlaceHolder.Controls.Add(printerModelFormView); 
    } 

    protected void LoadData_PrinterModels() 
    { 
        Printer_ModelsList x = new Printer_ModelsList(); 

        ddlPrinterModels.DataSource = x.GetPrinetr_Models(); 
        ddlPrinterModels.DataBind(); 
    } 

    protected void Page_Init(object sender, EventArgs e)  
    { 
        SqlDataSource sqlDS = new SqlDataSource(); 
        sqlDS.ConnectionString = ConfigurationManager.ConnectionStrings["CISF_Asset_Management"].ConnectionString; 
        sqlDS.SelectCommand = "SELECT dbo.cCartridge_Black.Ident_Black, dbo.cCartridge_Black.Model_Black, " + 
                               "dbo.cCartridge_Black.Desc_Black, dbo.cCartridge_Black.Qty_Black,  " + 
                               "dbo.cCartridge_Black.Black_Reorder_Limit, dbo.cCartridge_Black.Notes,  " + 
                               "dbo.cCartridge_Black.UpdatedBy, dbo.cPrinters_Toners.Hardware_Model " + 
                               "FROM   dbo.cCartridge_Black LEFT OUTER JOIN " + 
                               "dbo.cPrinters_Toners ON dbo.cCartridge_Black.Ident_Black  " + 
                               "= dbo.cPrinters_Toners.Ident_Black"; 
        form1.Controls.Add(sqlDS); 
        DropDownList ddl = new DropDownList(); 
        ddl.ID = "ddlTonerBlack"; 
        ddl.DataSource = sqlDS; 
        ddl.DataTextField = "Model_Black"; 
        ddl.DataValueField = "Ident_Black"; 
        form1.Controls.Add(ddl); 
    } 
}
</code></pre>

<p>And the template class:</p>

<pre><code>public class FormViewTemplate : System.Web.UI.ITemplate 
{ 
    void System.Web.UI.ITemplate.InstantiateIn(System.Web.UI.Control container) 
    { 
        Label lblPrinterModel = new Label(); 
        lblPrinterModel.ID = "lblHardwareModel"; 
        lblPrinterModel.DataBinding += new EventHandler(PrinterModelLabel_DataBinding); 

        container.Controls.Add(lblPrinterModel); 

        DropDownList ddlTonerBlack = new DropDownList(); 
        ddlTonerBlack.ID = "ddlTonerBlack"; 
        ddlTonerBlack.DataBinding +=new EventHandler(ddlTonerBlack_DataBinding); 

        container.Controls.Add(ddlTonerBlack); 
    } 

    private void PrinterModelLabel_DataBinding(Object sender, EventArgs e) 
    { 
        Label lblPrinterModel = (Label)sender; 

        FormView formViewContainer = (FormView)lblPrinterModel.NamingContainer; 
        DataRowView rowView = (DataRowView)formViewContainer.DataItem; 

        lblPrinterModel.Text = rowView["Hardware_Model"].ToString(); 

    } 

    private void ddlTonerBlack_DataBinding(Object sender, EventArgs e) 
    { 
        DropDownList ddlTonerBlack = (DropDownList)sender; 
        FormView formViewContainer = (FormView)ddlTonerBlack.NamingContainer; 
        DataRowView rowView = (DataRowView)formViewContainer.DataItem; 

        dalConsumables_TonerBlack x = new dalConsumables_TonerBlack(); 
        ddlTonerBlack.DataSource = x.GetListTonersBlack(); 
        ddlTonerBlack.DataBind(); 

        ddlTonerBlack.SelectedValue = rowView["Ident_Black"].ToString(); 
    } 
}
</code></pre>
