2

188 分

400 个帖子

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

6 小时 21 分钟前|LINK

我脑子里有一个概念,我正在努力实现它,但它不起作用。这是需要发生的事情:用户从列表中选择一个项目。根据选择动态构建表单视图。formview 需要是动态的,因为查询将返回一个在非空值中包含空值的数据集,例如:

4600, 1, 4, 空, 空, 68 ....

“4600”是型号,其他都是对应于组件的 ID。Null 值表示该字段不属于 4600。

此后,然后构建表单视图,以便将“4600”馈送到标签。对于每个非空值,我需要构建一个下拉列表,每个都有一个单独的数据源,不是 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(); 
    } 
}
4

1 回答 1

0

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

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

于 2012-07-08T19:53:14.077 回答