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

repeater的linkbutton中删除数据问题?(在线等)
大家好,我是asp.net2.0新手,遇到了一个这样的问题:
我在A页面用repeater绑定数据,在ItemTemplate中加了一个linkbutton执行删除功能,但是第一次删除时必需点击两次,后面只需一次即可。我测试过当只点击一次,回首页,再回到A页面时,数据已删除。也就是说只点击一次时数据已经删除了,但没显示出来,刷新也不行。
部分代码:
XML code
 
<asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand" EnableViewState="True">
    <ItemTemplate>
            <td>商品价格: <%# DataBinder.Eval(Container.DataItem, "Price")%> </td>
            <td> <asp:Label ID="Label1" runat="server" Text=' <%# DataBinder.Eval(Container.DataItem, "GoodsId")%>' Visible="false"> </asp:Label> </td>
            <td> <asp:LinkButton  id="lbtn1" runat="server" Text="删除" CommandName="del" OnClientClick="return confirm('确定删除吗?');"> </asp:LinkButton> </td>
其中Label1只是绑定要删除的数据的ID

.CS页面:
C# code

protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "del")
        {
            Label lab = (Label)e.Item.FindControl("Label1");
            string goodsid = lab.Text;
            string strCon = ConfigurationManager.ConnectionStrings["InfoWebConnectionString"].ConnectionString;
            SqlConnection Conn = new SqlConnection(strCon);
            Conn.Open();
            SqlCommand cmd = new SqlCommand("DELETE FROM Goods WHERE GoodsId = '" + goodsid + "'", Conn);
            cmd.ExecuteNonQuery();
            Conn.Close();
        }
    }



恳请高手指点!

------解决方案--------------------
在删除事件中,删除动作结束后repeater重新绑定一下就好了 

------解决方案--------------------
你那里判断了点击删除按钮的次数呢?
------解决方案--------------------
http://www.cnblogs.com/chenou/articles/1223244.html
------解决方案--------------------
二楼的方法正确
C# code

protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "del")
        {
            Label lab = (Label)e.Item.FindControl("Label1");
            string goodsid = lab.Text;
            string strCon = ConfigurationManager.ConnectionStrings["InfoWebConnectionString"].ConnectionString;
            SqlConnection Conn = new SqlConnection(strCon);
            Conn.Open();
            SqlCommand cmd = new SqlCommand("DELETE FROM Goods WHERE GoodsId = '" + goodsid + "'", Conn);
            cmd.ExecuteNonQuery();
            Conn.Close();
        }
        // <-- 这里重新绑定数据源
    }

------解决方案--------------------
二楼的方法正确 

------解决方案--------------------
我认为你的错误是没有绑定,最重要的是
Repeater1.DataSource = ds;
Repeater1.DataBind();
这两句话