我有一个生成的复选框列表,它是通过选择另一个复选框列表中的选项生成的:
CheckBoxList typefilter = new CheckBoxList { ID = "typefilter" + CheckBoxList1.Items[i].Text };
typefilter.RepeatDirection = RepeatDirection.Horizontal;
SortedList<int, string> filterValueList = new SortedList<int, string>();
typefilter.DataTextField = "Value";
typefilter.DataValueField = "Key";
typefilter.DataSource = getFilterOptions(int.Parse(CheckBoxList1.Items[i].Value));
typefilter.DataBind();
phSelectedItem.Controls.Add(typefilter);
现在的问题是,如果我想给我生成的复选框列表一个SelectedIndexChanged,我该怎么做呢?
编辑(使用建议的处理程序方法后,单击复选框将销毁整个复选框列表): 整个方法的代码
protected void cbl_RentalCategory_SelectedIndexChanged(object sender, EventArgs e)
{
if (cbl_RentalCategory.Items.Count > 0)
{
Label selectedType = new Label { ID = "Title" };
selectedType.Text = "<h3>Rental Items</h3>";
phSelectedItem.Controls.Add(selectedType);
}
for (int i = 0; i < cbl_RentalCategory.Items.Count; i++)
{
if (cbl_RentalCategory.Items[i].Selected)
{
Label selectedType = new Label { ID = "selectedType" + cbl_RentalCategory.Items[i].Text };
selectedType.Text = cbl_RentalCategory.Items[i].Text + "<br/>";
phSelectedItem.Controls.Add(selectedType);
CheckBoxList typefilter = new CheckBoxList { ID = "typefilter" + cbl_RentalCategory.Items[i].Text };
typefilter.RepeatDirection = RepeatDirection.Horizontal;
SortedList<int, string> filterValueList = new SortedList<int, string>();
typefilter.AutoPostBack = true;
typefilter.SelectedIndexChanged += typefilter_SelectedIndexChanged;
typefilter.DataTextField = "Value";
typefilter.DataValueField = "Key";
typefilter.DataSource = getFilterOptions(int.Parse(cbl_RentalCategory.Items[i].Value));
typefilter.DataBind();
phSelectedItem.Controls.Add(typefilter);
Label nextLine = new Label { ID = "nextLine" + cbl_RentalCategory.Items[i].Text };
nextLine.Text = "<br/>";
phSelectedItem.Controls.Add(nextLine);
}
}
}
getFilterOptions():
private SortedList<int, string> getFilterOptions(int typeID)
{
SortedList<int, string> filterValueList = new SortedList<int, string>();
string strConnectionString = ConfigurationManager.ConnectionStrings["SOMEDB"].ConnectionString;
SqlConnection conn = new SqlConnection(strConnectionString);
using (SqlCommand cmd = new SqlCommand("Select * FROM SOMEWHERE WHERE ID=@ID", conn))
{
conn.Open();
cmd.Parameters.AddWithValue("@ID", typeID);
cmd.ExecuteNonQuery();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
filterValueList.Add(int.Parse(reader["ID"].ToString()), reader["InformationHeader"].ToString());
}
conn.Close();
}
return filterValueList;
}
答案 0 :(得分:1)
以下是如何执行此操作。将Autopostback
设置为true并创建selectedindexchanged
方法。确保仅在页面加载时首次运行此代码。
if(!IsPostBack){
CheckBoxList typefilter = new CheckBoxList { ID = "typefilter" + CheckBoxList1.Items[i].Text };
typefilter.RepeatDirection = RepeatDirection.Horizontal;
SortedList<int, string> filterValueList = new SortedList<int, string>();
typefilter.AutoPostBack = true;
typefilter.SelectedIndexChanged += typefilter_SelectedIndexChanged;
typefilter.DataTextField = "Value";
typefilter.DataValueField = "Key";
typefilter.DataSource = getFilterOptions(int.Parse(CheckBoxList1.Items[i].Value));
typefilter.DataBind();
phSelectedItem.Controls.Add(typefilter);
}
这里是你选择的索引更改方法
public void typefilter_SelectedIndexChanged(object sender, EventArgs e)
{
//your code
}
答案 1 :(得分:0)
如果我正确理解了您的问题,您需要动态创建复选框列表,这个动态生成的复选框列表也应该支持带有SelectedChange事件的AutoPostBack。
运行示例的步骤:
步骤1:在Visual Studio中创建一个简单的网站
步骤2:将下面的代码放在default.aspx.cs文件中,保留使用命名空间。
步骤3:将aspx代码放在default.aspx文件中
预期输出 复选框列表将出现默认页面加载。 选择任何下拉项。 代码将为每个选择生成单独的checkboxlist(子项)列表。 选择单独生成的复选框列表中的任何项目,所选值将显示在屏幕上的标签中。
public partial class _Default : System.Web.UI.Page
{
string[] arrMasterDDLSelItem;
protected void Page_Load(object sender, EventArgs e)
{
if (masterCheckBoxList.SelectedItem != null)
{
arrMasterDDLSelItem = new string[masterCheckBoxList.Items.Count];
ListItem objListItem;
for (int index = 0; index < masterCheckBoxList.Items.Count; index++)
{
objListItem = masterCheckBoxList.Items[index] as ListItem;
if (objListItem.Selected)
arrMasterDDLSelItem[index] = objListItem.Text;
}
if (arrMasterDDLSelItem.Count() > 0)
RecreateControls(arrMasterDDLSelItem);
}
}
protected void typefilter_SelectedIndexChanged(object sender, EventArgs e)
{
CheckBoxList objCheckBoxList = sender as CheckBoxList;
ListItem objListItem;
string selectedItems=string.Empty;
foreach (var item in objCheckBoxList.Items)
{
objListItem=item as ListItem;
if (objListItem.Selected)
selectedItems += " " + objListItem.Text + ",";
}
Label1.Text = selectedItems;
}
private SortedList<int, string> getFilterOptions(string selItem)
{
SortedList<int, string> filterValueList = new SortedList<int, string>();
for (int index = 0; index < 10; index++)
filterValueList.Add(index, "SubItem -" + index.ToString() + " For " + selItem);
return filterValueList;
}
private void RecreateControls(string[] masterSelectedItem)
{
foreach (var item in masterSelectedItem)
{
if (item != null)
{
CheckBoxList typefilter = new CheckBoxList { ID = "typefilter" + item };
typefilter.RepeatDirection = RepeatDirection.Horizontal;
SortedList<int, string> filterValueList = new SortedList<int, string>();
typefilter.AutoPostBack = true;
typefilter.SelectedIndexChanged += new System.EventHandler(typefilter_SelectedIndexChanged);
typefilter.DataTextField = "Value";
typefilter.DataValueField = "Key";
typefilter.DataSource = getFilterOptions(item);
typefilter.DataBind();
ContentPlaceHolder MainContent = (ContentPlaceHolder)this.Master.FindControl("MainContent");
MainContent.Controls.Add(typefilter);
}
}
}
}
ASPX代码:
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content><asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:CheckBoxList ID="masterCheckBoxList" runat="server" AutoPostBack="true">
<asp:ListItem Text="Type-1" Value="1"></asp:ListItem>
<asp:ListItem Text="Type-2" Value="2"></asp:ListItem>
<asp:ListItem Text="Type-3" Value="3"></asp:ListItem>
</asp:CheckBoxList>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:Panel ID="pnlDropDownList" runat="server">
</asp:Panel>
</asp:Content>
道歉代码可能具有在非常有限的时间内编写的优化范围,只能提供解决问题的方法。但经过上述细节测试和工作正常。 注意此示例的参考取自 http://www.aspsnippets.com/Articles/Creating-Dynamic-DropDownList-Controls-in-ASP.Net.aspx
答案 2 :(得分:0)
您是否有任何特定原因要从后面的代码创建CheckBoxList?
据我所知,您的要求是:显示一个主复选框列表(RentalCategory),并为每个复选框选择,显示一个标签(selectedType *)和另一个复选框列表(typefilter *)。
对于这种要求,我将使用在项目模板中具有selectedType Label和typefilter CheckBoxList的转发器控件,仅在设计时为typefilter分配SelectedIndexChanged事件处理程序。
从代码隐藏中,我将执行2个select语句(一个在master table-RentalCategory上,另一个在详细信息table-typefilter上,具有适当的过滤条件:其中@ID in(&lt; RentalCategory list中所选复选框的ID&gt;) )一气呵成[假设:你没有太多的数据]
这将为我提供一个包含2个DataTables的DataSet。我将添加1个DataRelation并使用这个完整的DataSet与主复选框列表以及转发器(使用Row.GetChildRows或CreateChildView)绑定。
了解概念检查: http://support.microsoft.com/kb/306154 http://www.aspnettutorials.com/tutorials/controls/nestedrepeaters-csharp/