日期:2014-05-18  浏览次数:20539 次

在后台获取前台Repeater里面的Button控件
<asp:Repeater ID="Repeater1" runat="server" >
  <ItemTemplate >
  <fieldset >
  <legend ><%#Container.ItemIndex +1 %>
  </legend>
  <table width="100%" align="center" >
  <tr>
  <td align="left" ><strong>留言者网名:</strong><%#Eval("name")%></td>
  </tr>
  <tr>
  <td align="left"><strong>留言的内容:</strong><%#Eval("content")%></td>
  </tr>
  <tr>
  <td align="right" ><strong>留言的时间:</strong><%#Eval("time")%></td>
  </tr>
  <tr>
  <td align="left" >
  <asp:Button ID="Btnup" runat="server" Text="往上移" />
  <asp:Button ID="Btndown" runat="server" Text="往下移" />
  <asp:Button ID="Btndelete" runat="server" Text="删除" />
  </td>
  </tr>
  </table>
  </fieldset>
  </ItemTemplate>
  </asp:Repeater>

如题:我现在要在后台编程中获取到 Btnup,Btndown,Btndelete这三个Button控件并对他们编辑让他们各自实现自己的功能,有什么方法简单又清楚的呢?求教啊

------解决方案--------------------
Repeater加ItemCommmand事件 Button加CommandName属性和CommandArgument属性
调用如下 
C# code

protected void RPTNewsClass_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        var result =new OperationResult<NewsClass>();//我自己的逻辑组件 不用管
        if (e.CommandName == "Edit")//判断哪个按钮处罚的
        {
            result= this.Edit(e);
        }
        if (result.IsSuccess)
        {
            Utility.Alert(this.Page, "操作成功!");
        }
        else
        {
            Utility.Alert(this.Page, "操作失败!");
        }
    }
 private OperationResult<NewsClass> Edit(RepeaterCommandEventArgs e)
    {
        var service = ObjectFactory.GetInstance<NewsClassService>();
        var tbName = e.Item.FindControl("TBName") as TextBox;//通过这句找控件
        var ddlRootClass = e.Item.FindControl("DDLClass") as DropDownList;
        var entity = new NewsClass()
        {
            Class_name = tbName.Text,
            Class_belongto = Convert.ToInt32(ddlRootClass.SelectedValue),
            Class_id = Convert.ToInt32(e.CommandArgument)
        };
        return service.Update(entity);
    }

------解决方案--------------------
主要是FindControl("txtSortID")这个方法

几个主要的数据绑定控件都差不多这样
------解决方案--------------------
(Item.FindControl("ControlId") as controlType)...

去网上搜搜多的很
------解决方案--------------------
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "UP")
{
//上移
}
else if (e.CommandName == "DOWN")
{
//下移
}
else if (e.CommandName == "DEL")

//删除
}
}