gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 16:29:41 25 4
gpt4 key购买 nike

188 点

400 个帖子

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

6 小时 21 分钟前|LINK

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

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

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

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

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

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

最佳答案

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

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

关于c# - 带有下拉列表的动态 Formview 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11219346/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com