[英]asp.net Recreating Dynamically Added ListBox controls on PostBack
因此,我几乎可以完成这项工作。 我根据用户选择创建动态列表框。 总是在用户首次加载页面时,只有一个显示顶级类别的列表框(没有父项的列表框)。 我有带有子类别的类别。 可能是很多子类别
猫1
猫2
猫2.1
猫2.2
-- Cat 2.2.1 --- Cat 2.2.1.1
等等。
如果用户从已经显示的列表框中选择一个值,则我正在清除列表框。 因此,如果显示了4个列表框,并且用户从第一个列表框中选择了一个新值,该值显示了没有父项的最热门的泪液类别,则所有列表框都应该消失,并且应该出现新的列表框。 如果有4个列表框,并且用户在ListbOx 3中单击一个新项目,则第4个应该将子类别重新呈现为其选定的父级。 我希望我能正确地解释自己。
到目前为止,这是我的代码:公共子类WebForm2:System.Web.UI.Page {private Int32 controlCount = 0; 面板_panel;
private Panel PanelPlaceholder
{
get
{
if (_panel == null && Master != null)
_panel = pnlContainer;
return _panel;
}
}
protected void Page_PreInit(Object sender, EventArgs e)
{
this.EnsureChildControls();
if (IsPostBack)
{
// Re-create controls but not from datasource
// The controlCount value is output in the page as a hidden field during PreRender.
controlCount = Int32.Parse(Request.Form["controlCount"]); // assigns control count from persistence medium (hidden field)
for (Int32 i = 0; i < controlCount; i++)
{
CreateDynamicControlGroup(false);
}
}
}
protected void Page_Load(Object sender, EventArgs e)
{
if (!IsPostBack)
{
int cc = controlCount;
DataTable dt = null;
Dictionary<string, string> Params = new Dictionary<string, string>();
dt = Globals.g_DatabaseHandler.GetRecords(StoredProcedures.GetMainCategories, Params);
CreateDynamicControlGroup(true);
ListBox lb = (ListBox)PanelPlaceholder.Controls[controlCount - 1];
lb.DataSource = dt;
lb.DataValueField = "ID";
lb.DataTextField = "Name";
lb.DataBind();
}
}
protected void Page_PreRender(Object sender, EventArgs e)
{
// persist control count
ClientScript.RegisterHiddenField("controlCount", controlCount.ToString());
}
private void ListBox_SelectedIndexChanged(Object sender, EventArgs e)
{
ListBox lb = sender as ListBox;
Dictionary<string, string> Params = new Dictionary<string, string>();
Params.Add("parentID", lb.SelectedValue);
DataTable Categories = Globals.g_DatabaseHandler.GetRecords(StoredProcedures.GetChildCategories, Params);
if (Categories.Rows.Count > 0)
{
CreateDynamicControlGroup(true);
ListBox newLb = (ListBox)PanelPlaceholder.Controls[controlCount - 1];
newLb.DataSource = Categories; // use the same table
newLb.DataValueField = "ID";
newLb.DataTextField = "Name";
newLb.DataBind();
}
}
private void CreateDynamicControlGroup(Boolean incrementCounter)
{
// Create one logical set of controls do not assign values!
ListBox lb = new ListBox();
lb.AutoPostBack = true;
lb.CssClass = "panel";
PanelPlaceholder.Controls.Add(lb);
// wire event delegate
lb.SelectedIndexChanged += new EventHandler(ListBox_SelectedIndexChanged);
if (incrementCounter)
{
controlCount += 1;
}
}
}
这是我的标记:
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<div class="Column12" id="Form_NewListing">
<h2 class="h2row">Create Your Listing - Step 1 of 2)</h2>
<h3 class="h3row">Select a category</h3>
<div class="panel">
<asp:Panel ID="pnlContainer" runat="server"></asp:Panel>
</div>
</div>
提前致谢。
怎么样添加
int index = PanelPlaceholder.Controls.IndexOf((ListBox)sender);
for (int i = index + 1; i < PanelPlaceholder.Controls.Count; i++)
PanelPlaceholder.Controls.RemoveAt(index + 1);
到您的ListBox_SelectedIndexChanged
方法的开头?
int index = PanelPlaceholder.Controls.IndexOf((ListBox)sender);
for (int i = PanelPlaceholder.Controls.Count - 1; i > index; i--)
{
PanelPlaceholder.Controls.RemoveAt(i);
controlCount--;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.