gpt4 book ai didi

c# - 将 SelectedIndexChanged 添加到生成的复选框列表

转载 作者:行者123 更新时间:2023-11-30 17:45:24 24 4
gpt4 key购买 nike

我有一个生成的复选框列表,它是通过选择另一个复选框列表上的选项生成的:

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,我该怎么做呢?

编辑(使用建议的handler方法后,点击checkbox会销毁整个checkboxlist):整个方法的代码

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

最佳答案

这里是你如何做到这一点。将 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
}

关于c# - 将 SelectedIndexChanged 添加到生成的复选框列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27645270/

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