我有以下实现:

如您所见,我有一个中继器(列出了机器)和一个嵌套的中继器(列出了每台机器内的WindowsServices )。对于每个Windows服务,我都可以使用按钮执行操作。但是,要执行此操作,我需要知道哪个机器和哪个WindowsService是相关的。
这是我的代码:
protected void Page_Init(object sender, EventArgs e)
{
rptMachine.ItemDataBound += new RepeaterItemEventHandler(rptMachine_ItemDataBound);
}
protected void Page_Load(object sender, EventArgs e)
{
// bind the Machine repeater
rptMachine.DataSource = _monitoringService.Machines;
rptMachine.DataBind();
}
protected void rptMachine_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Repeater nestedRepeater = (Repeater) e.Item.FindControl("rptWindowsService");
nestedRepeater.DataSource = ((IMachine) e.Item.DataItem).WindowsServices;
nestedRepeater.DataBind();
Button btnActionInner = null;
// bind the action button situated inside the nested repeater
foreach(RepeaterItem ri in nestedRepeater.Items)
{
if((Button)ri.FindControl("btnAction") != null)
{
btnActionInner = (Button) ri.FindControl("btnAction");
btnActionInner.CommandName = "ActionState";
btnActionInner.CommandArgument = strWindowsService;
}
}
}
}
protected void rptWindowsService_ItemCommand(object source, RepeaterCommandEventArgs e)
{
// do the specific action stop/run for the windows service
if (e.CommandName == "ActionState")
{
if(((Button)(e.CommandSource)).Text.Equals("Stop"))
{
}
else if(((Button)(e.CommandSource)).Text.Equals("Run"))
{
}
}
}
}
}因此,基本上我需要知道(在rptWindowsService_ItemCommand内部)操作所关注的对是什么。
那么最好的方法是什么呢?
请不要犹豫,要求更多的澄清!
谢谢
发布于 2010-06-18 23:40:17
在DataboundItem上,在代码隐藏中设置一个临时属性为“当前”计算机/窗口服务,然后绑定到它们。
<asp:Repeater DataSource="<%# MachineList %>" OnItemDataBound="Machine_DataBound">
<asp:Repeater DataSource="<%# ((Machine)Container.DataItem).Services %>">
<asp:Button id="whatever" Text='<%# string.Format("Kill Service ({0}.{1})", CurrentMachine.Name, ((Service)Container.DataItem).Name); %>' />
</asp:Repeater>
</asp:Repeater>代码隐藏:
private Machine CurrentMachine { get; set; }
public void Machine_DataBound(object sender, RepeaterItemEventArgs e)
{
CurrentMachine = e.Item as Machine;
}https://stackoverflow.com/questions/3070972
复制相似问题