大家好,我是asp.net2.0新手,遇到了一个这样的问题:
我在A页面用repeater绑定数据,在ItemTemplate中加了一个linkbutton执行删除功能,但是第一次删除时必需点击两次,后面只需一次即可。我测试过当只点击一次,回首页,再回到A页面时,数据已删除。也就是说只点击一次时数据已经删除了,但没显示出来,刷新也不行。
部分代码:<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页面: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();
        }
    }
恳请高手指点!

解决方案 »

  1.   

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

  2.   

    或者
    去掉代码文件中的if(!IsPostBack),每一次PageLoad都重新绑定repeater datasource.
      

  3.   

    http://www.cnblogs.com/chenou/articles/1223244.html
      

  4.   

    二楼的方法正确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();
            }
            // <-- 这里重新绑定数据源
        }
    三楼的方法有问题,因为 PageLoad 事件发生在 ItemCommand 之前。
      

  5.   

    //这是绑定Repeater1数据源
     private void bind(string sql)
            {
                SqlConnection con = new SqlConnection(strCon);
                SqlCommand cmd = new SqlCommand(sql, con);
                SqlDataAdapter sda = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                sda.Fill(ds);
                Repeater1.DataSource = ds;
                Repeater1.DataBind();        }你删除之后,再把这个方法调用一次,即再为Repeater1绑定一次数据源即可
    string sql="select * from Goods";
    this.bind(sql);
      

  6.   

    连接字符串加进去.
     string strCon = ConfigurationManager.ConnectionStrings["con"].ToString();

    string strCon ="server=.;database=db_stu;uid=sa;pwd=sa";
      

  7.   

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

  8.   

    用10楼的方法弄好啦,谢谢大家,特别是the_pain,多谢!
    马上结贴
      

  9.   


    PAGELOAD()
    {
     if(!ispostBack)
      {
        this.bindlist();//调用下绑定的方法即可
      }
    }以前我也遇到过这样的问题,在gridview里手动添加数据的时候,就是添加不了,更新也更新不了。