关于以下代码,当我查询到数据,选择要删除的东西后,再查询数据时不出现任何东西,没有删除的数据也查询不到 只有把页面关掉重新查询才行  为什么? 请指教!! protected void Getdatabind()
    {
        Sda.SelectCommand = new SqlCommand();
        Sda.SelectCommand.CommandTimeout = 180;
        Sda.SelectCommand.Connection = con;
        Sda.SelectCommand.CommandText = "LOTNO1";
        Sda.SelectCommand.CommandType = CommandType.StoredProcedure;        Sda.SelectCommand.Parameters.Add(new SqlParameter("@FDATE", SqlDbType.Char)).Value = TxtDate.Text;
        Sda.SelectCommand.Parameters.Add(new SqlParameter("@Class", SqlDbType.NVarChar)).Value = DDLClass.SelectedValue.Trim();
      
        if (con.State.ToString() == "Closed")
            con.Open();
        Sda.Fill(Ds, "QTYLOT");
        GridView1.DataSource = Ds.Tables["QTYLOT"].DefaultView;
        GridView1.DataBind();        con.Close();    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Sda.DeleteCommand  = new SqlCommand();
        Sda.DeleteCommand .Connection = con;
        Sda.DeleteCommand .CommandType = CommandType.Text;
        Sda.DeleteCommand .CommandText = "DELETE FROM ITEM WHERE LOTNO='" + txtlot.Text + "' ";
        if (con.State.ToString() == "Closed")
            con.Open();
        //SqlDataReader sdr = Sda.DeleteCommand.ExecuteReader();
        int p = Sda.DeleteCommand.ExecuteNonQuery();
        //GridView1.DataSource = sdr;
        //GridView1.DataBind();        con.Close();
        if (txtlot.Text != "")
        {
            labAlert.Visible = true;
            GridView1.Visible = false;
            txtlot.Text = "";
        }
        //else
        //{
        //}
     
    }

解决方案 »

  1.   

    按钮事件最后加上 Getdatabind();
      

  2.   

    protected void Button2_Click(object sender, EventArgs e)
        {
            Sda.DeleteCommand  = new SqlCommand();
            Sda.DeleteCommand .Connection = con;
            Sda.DeleteCommand .CommandType = CommandType.Text;
            Sda.DeleteCommand .CommandText = "DELETE FROM ITEM WHERE LOTNO='" + txtlot.Text + "' ";
            if (con.State.ToString() == "Closed")
                con.Open();
            //SqlDataReader sdr = Sda.DeleteCommand.ExecuteReader();
            int p = Sda.DeleteCommand.ExecuteNonQuery();
            //GridView1.DataSource = sdr;
            //GridView1.DataBind();        con.Close();
            if (txtlot.Text != "")
            {
                labAlert.Visible = true;
                GridView1.Visible = false;
                txtlot.Text = "";
            }
            //else
            //{
            //}
         
            Getdatabind();    }
      

  3.   


    if(!ispostback())  也是不管用啊 
    具体怎么做呢?请指教!
      

  4.   

    删除操作后再Getdatabind()一次 
      

  5.   


    以下是全部代码,请指教public partial class LotNo1 : System.Web.UI.Page
    {
        private SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Con"]);
        private SqlDataAdapter Sda = new SqlDataAdapter();
        private DataSet Ds = new DataSet();    protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
               
                DateTime td = DateTime.Now;            string strMonth = "";
                string strDay = "";            if (td.Month.ToString().Length < 2)
                    strMonth = "0" + td.Month.ToString();
                else
                    strMonth = td.Month.ToString();            if (td.Day.ToString().Length < 2)
                    strDay = "0" + td.Day.ToString();
                else
                    strDay = td.Day.ToString();            TxtDate.Text = td.Year.ToString() + "/" + strMonth + "/" + strDay;
                TxtDate.Attributes.Add("onclick", "fPopCalendar(this)");
                BindClass();
                //Getdatabind();
            }
            
        }
        private void BindClass()
        {
            DDLClass.Items.Clear();        ListItem lld_1 = new ListItem();
            lld_1.Text = "全部";
            lld_1.Value = "";
            DDLClass.Items.Add(lld_1);        ListItem lld_2 = new ListItem();
            lld_2.Text = "A";
            lld_2.Value = "A";
            DDLClass.Items.Add(lld_2);        ListItem lld_3 = new ListItem();
            lld_3.Text = "B";
            lld_3.Value = "B";
            DDLClass.Items.Add(lld_3);        ListItem lld_4 = new ListItem();
            lld_4.Text = "C";
            lld_4.Value = "C";
            DDLClass.Items.Add(lld_4);    }    protected void Getdatabind()
        {
            Sda.SelectCommand = new SqlCommand();
            Sda.SelectCommand.CommandTimeout = 180;
            Sda.SelectCommand.Connection = con;
            Sda.SelectCommand.CommandText = "LOTNO1";
            Sda.SelectCommand.CommandType = CommandType.StoredProcedure;        Sda.SelectCommand.Parameters.Add(new SqlParameter("@FDATE", SqlDbType.Char)).Value = TxtDate.Text;
            Sda.SelectCommand.Parameters.Add(new SqlParameter("@Class", SqlDbType.NVarChar)).Value = DDLClass.SelectedValue.Trim();
          
            if (con.State.ToString() == "Closed")
                con.Open();
            Sda.Fill(Ds, "QTYLOT");
            GridView1.DataSource = Ds.Tables["QTYLOT"].DefaultView;
            GridView1.DataBind();        con.Close();    }
        protected void Button2_Click(object sender, EventArgs e)
        {
            Sda.DeleteCommand  = new SqlCommand();
            Sda.DeleteCommand .Connection = con;
            Sda.DeleteCommand .CommandType = CommandType.Text;
            Sda.DeleteCommand .CommandText = "DELETE FROM ITEM WHERE LOTNO='" + txtlot.Text + "' ";
            if (con.State.ToString() == "Closed")
                con.Open();
            //SqlDataReader sdr = Sda.DeleteCommand.ExecuteReader();
            int p = Sda.DeleteCommand.ExecuteNonQuery();
            //GridView1.DataSource = sdr;
            //GridView1.DataBind();        con.Close();
            if (txtlot.Text != "")
            {
                labAlert.Visible = true;
                GridView1.Visible = false;
                txtlot.Text = "";
            }
            //else
            //{
            //}
            Getdatabind();
         
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            Getdatabind();
        }
       
    }
      

  6.   

       Sda.DeleteCommand .CommandText = "DELETE FROM ITEM WHERE LOTNO='" + txtlot.Text + "' ";这样写会被注入
      

  7.   

    你最好在Page_Load的方法上打个断点 看第二次页面加载的时候 是走到哪里跳出了 还是就没有执行绑定...很多问题都是调试才知道错误的...
      

  8.   

    Getdatabind() 这个东西你明明写的绑定不绑定 怎么显示 我汗。。
    至于SQL注入方面,我觉得 拼字符串的好使。
      

  9.   

    你的代码里面加上try catch,应该是你删除失败了,数据库没有close还是处于open状态,下次在点击查询的时候,你判断通过 if (con.State.ToString() == "Closed"),这句话根本进不去,所以就没显示结果了。
      

  10.   

    删除完重新BIND,然后在页面中设置enableViewState=true
      

  11.   

    Getdatabind()最看设断点看一下啊~!
      

  12.   

    亲爱的同志们我知道我错在哪里了!! if (txtlot.Text != "")
         {
              labAlert.Visible = true;
            GridView1.Visible = false; 把这句去掉就行了 color]           
                txtlot.Text = "";
            }谢谢大家!!!
      

  13.   

    亲爱的同志们我知道我错在哪里了!! if (txtlot.Text != "") 
        { 
              labAlert.Visible = true; 
          GridView1.Visible = false; 把这句红色的去掉就行了          
                txtlot.Text = ""; 
            } 谢谢大家!!!