日期:2014-05-17  浏览次数:20487 次

后台获取 Repeater里的ID
//前台

<asp:Repeater ID="Repeater1" runat="server" >
                <ItemTemplate>
                    <a href="newsView.aspx?ClassID=<%#Eval("ID")%>" target="_blank">
                        <%#Eval("ID")%></a><%#Eval("标题")%><%#Eval("简介")%><%#Eval("来源")%>
                       <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="删除"    /> <br/>
                </ItemTemplate>
 </asp:Repeater>

//后台

  protected void Button1_Click(object sender, EventArgs e)
    {
       
        try
        {
           
            var obj = (from o in context.新闻
                       where o.ID == 这里应该添加什么才能获取Repeater里的ID值,完成删除功能
                       select o).Single();
            context.DeleteObject(obj);
            context.SaveChanges();
            JS.Alert(this, "删除成功!");
        }
        catch (DataServiceRequestException ex)
        {
            throw new ApplicationException(
                "删除时发生错误", ex);
        }
    }

------解决方案--------------------
不是用Button1_Click事件 
而是用Repeater_ItemCommand事件 
具体参考代码:repeater增删改查
------解决方案--------------------

 <asp:Button ID="Button1" runat="server"  Text="删除 OnClientClick="return confirm('确定要删除吗?');" CommandName="del" CommandArgument='<%#Eval("ID")%>'    /> <br/>


protected void Repeater1_ItemCommand(object sender, RepeaterCommandEventArgs e)
 {
            if (e.CommandName == "del")
            {
                int id=Convert.ToInt32(e.CommandArgument);
  &nb