带有下拉列表的动态Formview失败

时间:2012-06-27 04:20:37

标签: c# asp.net

188积分

400帖子

带有下拉列表的动态表单视图无法完成。

6小时,21分钟前|链接

我脑子里有一个概念,我正在努力实现它并没有起作用。以下是需要发生的事情:用户从列表中选择一个项目。根据选择,动态构建formview。 formview需要是动态的,因为查询将返回一个非空值的数据集,如下所示:

4600,1,4,NULL,NULL,68 ....

“4600”是型号,其他所有内容都是与组件对应的ID。 Null值表示此字段不属于4600.

此后,构建视图以使“4600”被馈送到标签。对于每个非null值,我需要构建一个下拉列表,每个列表都有一个单独的数据源,而不是ODS的东西,而是对BLL类的调用。然后将非空值分配给ddl的selected value属性。

很简单,不是吗?所以这是代码,它的失败可怕。实际上只是在无限循环中超时。超时发生在ddlTonerBlack_DataBinding方法中。有人能告诉我我做错了什么吗?谢谢。 EJM

aspx标记:

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

    <asp:PlaceHolder id="DetailsViewPlaceHolder" runat="server"/> 

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

现在代码文件:

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); 
    } 
}

模板类:

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(); 
    } 
}

1 个答案:

答案 0 :(得分:0)

你得到一个无限循环,因为在ddlTonerBlack_DataBinding方法中,你在刚刚触发数据绑定事件的控件上调用一个DataBind方法。

只需在相同控件的数据绑定事件处理程序中触发数据绑定事件,这就是无限循环的原因。