如何在嵌套转发器中保留记录的痕迹?

时间:2010-06-18 15:30:47

标签: c# asp.net itemdatabound nested-repeater itemcommand

我有以下实现:

alt text http://i47.tinypic.com/2v16fde.png

正如您所看到的,我有一个转发器(列出机器)和一个嵌套转发器(在每台机器中列出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内)与操作有关的对是什么。

最好的方法是什么?

请不要犹豫,要求进一步澄清!

由于

1 个答案:

答案 0 :(得分:1)

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