C# 更新中继器内生成的DropDownList的值
我已经对此进行了搜索,但似乎没有一个符合我需要的确切要求。以下是场景:C# 更新中继器内生成的DropDownList的值,c#,asp.net,drop-down-menu,repeater,itemdatabound,C#,Asp.net,Drop Down Menu,Repeater,Itemdatabound,我已经对此进行了搜索,但似乎没有一个符合我需要的确切要求。以下是场景: 我有一个包含以下内容的列表:File1、File2、File3和File4。此列表绑定到中继器 每个中继器项都包含一个DropDownList 我使用repeater的ItemDataBound事件遍历列表,创建并填充列表中存储的所有项目的下拉列表 最终结果是,它将为我生成一系列下拉列表,其中包含页面中该列表项的特定值 不过,需求发生了变化,变化涉及以下方面: 如果列表的当前迭代是File1,它将仅为File1创建一个下拉列
那么,有没有一种方法可以获取File1的ID,以便在为File2和File3触发ItemDataBound事件时,只更新File1的DropDownList?或者我必须寻找另一种方法来实现这一点?在ItemDataBound事件处理程序中,您可以获取对DropDownList的引用并将其存储在成员变量中。您可以使用该变量保留对后续ItemDataBound事件的DropDownList的访问权限 听起来你需要这样做:
public partial class _Default : System.Web.UI.Page
{
// Dummy data class. We'll bind a list of these to the repeater.
class File
{
public string Name { get; set; }
public int ID { get; set; }
public List<string> AListOfStrings { get; set; }
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
rptrTest.ItemDataBound +=
new RepeaterItemEventHandler(rptrTest_ItemDataBound);
}
private DropDownList file1DropDown;
void rptrTest_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
// Find the DropDownList in the repeater's ItemTemplate
// so we can manipulate it.
DropDownList ddlSelect =
e.Item.FindControl("ddlSelect") as DropDownList;
File dataItem = (File)e.Item.DataItem;
DropDownList currentDropDownList;
switch (dataItem.ID)
{
case 1:
// Store the current item's DropDownList for later...
file1DropDown = ddlSelect;
currentDropDownList = file1DropDown;
break;
case 2:
case 3:
currentDropDownList = file1DropDown;
break;
default:
currentDropDownList = ddlSelect;
break;
}
// Get all of the strings starting with the current ID and
// add them to whichever DropDownList we need to modify.
currentDropDownList.Items.AddRange((
from s
in dataItem.AListOfStrings
where s.StartsWith(dataItem.ID.ToString())
select new ListItem(s)).ToArray());
}
protected void Page_Load(object sender, EventArgs e)
{
// Just build up a list of strings which we can filter later.
List<string> stringList = new List<string>();
foreach (string number in (new[] { "1", "2", "3", "4" }))
{
foreach (string letter in (new[] { "a", "b", "c", "d", "e" }))
{
stringList.Add(number + " " + letter);
}
}
List<File> myObjects = new List<File>(new[]
{
new File { ID = 1, Name = "Foo", AListOfStrings = stringList },
new File { ID = 2, Name = "Bar", AListOfStrings = stringList },
new File { ID = 3, Name = "Baz", AListOfStrings = stringList },
new File { ID = 4, Name = "Quux", AListOfStrings = stringList }
});
rptrTest.DataSource = myObjects;
rptrTest.DataBind();
}
}
<asp:Repeater runat="server" ID="rptrTest">
<ItemTemplate>
ID: <%#DataBinder.Eval(Container.DataItem, "ID")%>
<br />
Name: <%#DataBinder.Eval(Container.DataItem, "Name")%>
<br />
Select: <asp:DropDownList runat="server" ID="ddlSelect" />
<br /><br />
</ItemTemplate>
</asp:Repeater>
public分部类\u默认值:System.Web.UI.Page
{
//虚拟数据类。我们将这些类的列表绑定到中继器。
类文件
{
公共字符串名称{get;set;}
公共int ID{get;set;}
公共列表字符串{get;set;}
}
受保护的覆盖无效OnInit(事件参数e)
{
碱基.奥尼特(e);
rprtest.ItemDataBound+=
新的RepeaterItemEventHandler(rptrTest_ItemDataBound);
}
私有下拉列表文件1下拉列表;
void rptrTest_ItemDataBound(对象发送方,RepeaterItemEventArgs e)
{
//在中继器的ItemTemplate中查找DropDownList
//所以我们可以操纵它。
下拉列表选择=
e、 Item.FindControl(“ddlSelect”)作为下拉列表;
文件dataItem=(文件)e.Item.dataItem;
下拉列表当前下拉列表;
开关(dataItem.ID)
{
案例1:
//存储当前项目的DropDownList以备以后使用。。。
file1DropDown=ddlSelect;
currentDropDownList=file1DropDown;
打破
案例2:
案例3:
currentDropDownList=file1DropDown;
打破
违约:
currentDropDownList=ddlSelect;
打破
}
//获取以当前ID开头的所有字符串,并
//将它们添加到我们需要修改的下拉列表中。
currentDropDownList.Items.AddRange((
从s
在dataItem.alistOfstring中
其中s.StartsWith(dataItem.ID.ToString())
选择新列表项().ToArray();
}
受保护的无效页面加载(对象发送方、事件参数e)
{
//只需建立一个字符串列表,我们可以稍后进行筛选。
List stringList=新列表();
foreach(字符串编号在(新[]{“1”、“2”、“3”、“4”}))
{
foreach(字符串字母in(新[]{“a”、“b”、“c”、“d”、“e”}))
{
字符串列表。添加(数字+“”+字母);
}
}
List myObjects=新列表(新[]
{
新文件{ID=1,Name=“Foo”,AListOfStrings=stringList},
新文件{ID=2,Name=“Bar”,AListOfStrings=stringList},
新文件{ID=3,Name=“Baz”,AListOfStrings=stringList},
新文件{ID=4,Name=“qux”,AListOfStrings=stringList}
});
rprtest.DataSource=myObjects;
rprtest.DataBind();
}
}
。。。您的ASPX页面将包含一个类似以下内容的转发器:
public partial class _Default : System.Web.UI.Page
{
// Dummy data class. We'll bind a list of these to the repeater.
class File
{
public string Name { get; set; }
public int ID { get; set; }
public List<string> AListOfStrings { get; set; }
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
rptrTest.ItemDataBound +=
new RepeaterItemEventHandler(rptrTest_ItemDataBound);
}
private DropDownList file1DropDown;
void rptrTest_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
// Find the DropDownList in the repeater's ItemTemplate
// so we can manipulate it.
DropDownList ddlSelect =
e.Item.FindControl("ddlSelect") as DropDownList;
File dataItem = (File)e.Item.DataItem;
DropDownList currentDropDownList;
switch (dataItem.ID)
{
case 1:
// Store the current item's DropDownList for later...
file1DropDown = ddlSelect;
currentDropDownList = file1DropDown;
break;
case 2:
case 3:
currentDropDownList = file1DropDown;
break;
default:
currentDropDownList = ddlSelect;
break;
}
// Get all of the strings starting with the current ID and
// add them to whichever DropDownList we need to modify.
currentDropDownList.Items.AddRange((
from s
in dataItem.AListOfStrings
where s.StartsWith(dataItem.ID.ToString())
select new ListItem(s)).ToArray());
}
protected void Page_Load(object sender, EventArgs e)
{
// Just build up a list of strings which we can filter later.
List<string> stringList = new List<string>();
foreach (string number in (new[] { "1", "2", "3", "4" }))
{
foreach (string letter in (new[] { "a", "b", "c", "d", "e" }))
{
stringList.Add(number + " " + letter);
}
}
List<File> myObjects = new List<File>(new[]
{
new File { ID = 1, Name = "Foo", AListOfStrings = stringList },
new File { ID = 2, Name = "Bar", AListOfStrings = stringList },
new File { ID = 3, Name = "Baz", AListOfStrings = stringList },
new File { ID = 4, Name = "Quux", AListOfStrings = stringList }
});
rptrTest.DataSource = myObjects;
rptrTest.DataBind();
}
}
<asp:Repeater runat="server" ID="rptrTest">
<ItemTemplate>
ID: <%#DataBinder.Eval(Container.DataItem, "ID")%>
<br />
Name: <%#DataBinder.Eval(Container.DataItem, "Name")%>
<br />
Select: <asp:DropDownList runat="server" ID="ddlSelect" />
<br /><br />
</ItemTemplate>
</asp:Repeater>
身份证件:
姓名:
选择:
因此,在代码隐藏文件中的
rptrTest\u ItemDataBound
事件处理程序中,有一个switch语句检查file.ID
以查看它绑定到哪个文件实例。如果ID为1,DropDownList将被分配到file1DropDown
。然后在后续事件中,开关块其余部分中的逻辑决定我们需要修改哪个DropDownList。此场景由aspnetawesome ajaxdropdown控件解决,但这不是免费的,但我需要使用默认的ASP.NET下拉列表,我也不允许在解决方案中添加第三方项目。先生,你太棒了!!!太糟糕了,我还不能添加投票,但谢谢!这一个有效:)我现在所要做的就是隐藏在传输过程中变为空的下拉列表。再次感谢!