我有这个asp DropDownList
<td>
<asp:DropDownList ID="ddlCategorySearch" runat="server" OnSelectedIndexChanged="ddlCategorySearch_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList><br />
<asp:DropDownList ID="ddlSubCategorySearch" runat="server" OnSelectedIndexChanged="ddlSubCategorySearch_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>
我想用两种方法触发OnSelectedIndexChanged
ddlCategorySearch_SelectedIndexChanged
和ddlSubCategorySearch_SelectedIndexChanged
因此,如果用户触发OnSelectedIndexChange
,将触发两个值方法。
但是我无法解决这个问题,我怎么能这样做?
答案 0 :(得分:1)
您的问题有点不清楚但是如果我在用户更改类别值时正确理解您要触发这两个事件吗?
您可以通过使用以下语法在主类别方法中显式调用sub方法来完成此操作
ddlSubCategorySearch_SelectedIndexChanged(sender,e)
如果我误解了你的问题,请编辑它以使其更清晰
编辑:根据您上面的评论,您无需调用sub ddl的selectedindex更改来更改其值。
您可以从selectedindexchanged方法的类别下拉列表中将数据源更改为已过滤的数据源。
我希望这是有道理的
答案 1 :(得分:0)
在DataSource
ddlSubCategorySearch
事件中ddlCategorySearch_SelectedIndexChanged
填写DataSource
,首先需要填写ddlCategorySearch
protected void protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindCategorySearch();
}
}
protected void BindCategorySearch()
{
// Fill your ddlCategorySearch here. Write down your database logic to
//get a datasource for ddlCategorySearch
}
protected void ddlCategorySearch_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
BindSubCategory();
}
catch (Exception ex)
{
}
}
protected void BindSubCategory()
{
try
{
int categoryID = Convert.ToInt32(ddlCategorySearch.SelectedValue);
//Write down here your database logic to get datasource for
// ddlSubCategorySearch. For Example select * from SubCategory where
//CategoryID = categoryID
List<SubCategory> listSubcategories = new List<SubCategory>();
//Fill your listCategory on base of categoryID
ListItem li = new ListItem();
li.Text = "--Select Sub Category--";
li.Value = "0";
li.Selected = true;
ddlSubCategories.Items.Insert(0, li);
}
catch (Exception ex)
{
}
}
的{{1}}页面,如下所示。< / p>
SELECT name, id,
MAX(CASE
WHEN( status = 'Missing' AND severity = 'Optional' ) AND ( id=123 )
THEN COALESCE(count(patchid),0)
ELSE 0
END) AS missingoptional,
MAX(CASE
WHEN( status = 'Missing' AND severity = 'Important' ) AND ( id=123 )
THEN COALESCE(count(patchid),0)
ELSE 0
END) as missingimportant
FROM tablename
GROUP BY name, id
ORDER BY id
答案 2 :(得分:0)
如果您想根据另一个下拉填充一个下拉列表,您可以使用以下内容:
public void ddlCategorySearch_SelectedIndexChanged(object sender, Eventargs e){
var param = ddlCategorySearch.SelectedValue;
foreach (var item in myList)//assuming you put the elements you want inside DDL in a list
if(item.ID == param)
ddlSubCategorySearch.Items.Add(new ListItem { Text="",Value=""}); //assign DDL elements here such as item.property
}
}
答案 3 :(得分:0)
删除第二个Dropdown选择的索引更改并自动回发= false 仅使用第一次下拉选择索引更改事件
protected void protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindCategorySearch();
}
}
protected void BindCategorySearch()
{
// Fill your ddlCategorySearch here. Write down your database logic to
//get a datasource for ddlCategorySearch
}
protected void ddlCategorySearch_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
BindSubCategory();
}
catch (Exception ex)
{
}
}
protected void BindSubCategory()
{
try
{
int categoryID = Convert.ToInt32(ddlCategorySearch.SelectedValue);
//Write down here your database logic to get datasource for
// ddlSubCategorySearch. For Example select * from SubCategory where
//CategoryID = categoryID
List<SubCategory> listSubcategories = new List<SubCategory>();
//Fill your listCategory on base of categoryID
ListItem li = new ListItem();
li.Text = "--Select Sub Category--";
li.Value = "0";
li.Selected = true;
ddlSubCategories.Items.Insert(0, li);
}
catch (Exception ex)
{
}
}
答案 4 :(得分:0)
您可以为DropDowns使用单个函数,检查哪个触发事件并相应地执行操作。
protected void DualDropDown_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList dropDownList = sender as DropDownList;
if (dropDownList.ID == "ddlCategorySearch")
{
//do stuff if ddlCategorySearch fired the event
}
else
{
//do stuff if ddlSubCategorySearch fired the event
}
}