0

放入 Response.Redirect 时出现问题

例如,数据只记录第一个键,其余所有数据在数据库中没有记录。其次,我使用 Response.Redirect 的目的是刷新网页,在将数据插入数据库后清除数据有什么想法吗?

好心提醒。谢谢你。

    protected void Page_Load(object sender, EventArgs e)
    {

        string stat = Request.QueryString["stat"];
        if (stat == "insert")
        {
            StatLabel.Text = "New Record have been Insert";
        }

    }





    private void BindGrid(int rowcount)
    {

        DataTable dt = new DataTable();

        DataRow dr;

        dt.Columns.Add(new System.Data.DataColumn("Test1", typeof(String)));

        dt.Columns.Add(new System.Data.DataColumn("Test2", typeof(String)));

        dt.Columns.Add(new System.Data.DataColumn("Test3", typeof(String)));



        if (ViewState["CurrentData"] != null)
        {

            for (int i = 0; i < rowcount + 1; i++)
            {

                dt = (DataTable)ViewState["CurrentData"];

                if (dt.Rows.Count > 0)
                {

                    dr = dt.NewRow();

                    dr[0] = dt.Rows[0][0].ToString();



                }

            }

            dr = dt.NewRow();

            dr[0] = TextBox1.Text;

            dr[1] = TextBox2.Text;

            dr[2] = TextBox3.Text;

            dt.Rows.Add(dr);



        }

        else
        {

            dr = dt.NewRow();

            dr[0] = TextBox1.Text;

            dr[1] = TextBox2.Text;

            dr[2] = TextBox3.Text;



            dt.Rows.Add(dr);



        }



        // If ViewState has a data then use the value as the DataSource

        if (ViewState["CurrentData"] != null)
        {

            GridView1.DataSource = (DataTable)ViewState["CurrentData"];

            GridView1.DataBind();

        }

        else
        {

            // Bind GridView with the initial data assocaited in the DataTable

            GridView1.DataSource = dt;

            GridView1.DataBind();



        }

        // Store the DataTable in ViewState to retain the values

        ViewState["CurrentData"] = dt;



    }


    protected void Button1_Click(object sender, EventArgs e)
    {
        // Check if the ViewState has a data assoiciated within it. If
        if (ViewState["CurrentData"] != null)
        {
            DataTable dt = (DataTable)ViewState["CurrentData"];
            int count = dt.Rows.Count;
            BindGrid(count);
        }
        else
        {
            BindGrid(1);
        }
        TextBox1.Text = string.Empty;
        TextBox2.Text = string.Empty;
        TextBox3.Text = string.Empty;

        TextBox1.Focus();
        TextBox2.Focus();
        TextBox3.Focus();
    }



    protected void Button2_Click(object sender, EventArgs e)
    {


        foreach (GridViewRow oItem in GridView1.Rows)
        {
            string str1 = oItem.Cells[0].Text;
            string str2 = oItem.Cells[1].Text;
            string str3 = oItem.Cells[2].Text;
            insertData(str1, str2, str3);
        }
    }

    public void insertData(string str1,string str2,string str3)
    {
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["CIMProRPT01ConnectionString"].ConnectionString);
       string sql = "insert into test (test1,test2,test3) values ('" + str1 + "','" + str2 + "','" + str3 + "')";
       SqlCommand cmd = new SqlCommand(sql, con);
       con.Open();
       cmd.ExecuteNonQuery();
       cmd.Parameters.Clear();
       con.Close();
       Response.Redirect("WebForm1.aspx?stat=insert");
    }
  }
}
4

2 回答 2

0

当您保存到数据库中时,您会在每次插入后立即重定向。因此,不会执行任何后续插入。我会将重定向移出循环。也许您也可以传递查询字符串中插入的记录数。

受保护的无效Button2_Click(对象发送者,EventArgs e){

    foreach (GridViewRow oItem in GridView1.Rows)
    {
        string str1 = oItem.Cells[0].Text;
        string str2 = oItem.Cells[1].Text;
        string str3 = oItem.Cells[2].Text;
        insertData(str1, str2, str3);
    }
    Response.Redirect("WebForm1.aspx?stat=insert");
}

public void insertData(string str1,string str2,string str3)
{
    SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["CIMProRPT01ConnectionString"].ConnectionString);
   string sql = "insert into test (test1,test2,test3) values ('" + str1 + "','" + str2 + "','" + str3 + "')";
   SqlCommand cmd = new SqlCommand(sql, con);
   con.Open();
   cmd.ExecuteNonQuery();
   cmd.Parameters.Clear();
   con.Close();

}
于 2013-06-20T04:31:17.837 回答
0

我看到你的代码我找到了

protected void Button2_Click(object sender, EventArgs e)
{


    foreach (GridViewRow oItem in GridView1.Rows)
    {
        string str1 = oItem.Cells[0].Text;
        string str2 = oItem.Cells[1].Text;
        string str3 = oItem.Cells[2].Text;
        insertData(str1, str2, str3);
    }
}

public void insertData(string str1,string str2,string str3)
{
    SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["CIMProRPT01ConnectionString"].ConnectionString);
   string sql = "insert into test (test1,test2,test3) values ('" + str1 + "','" + str2 + "','" + str3 + "')";
   SqlCommand cmd = new SqlCommand(sql, con);
   con.Open();
   cmd.ExecuteNonQuery();
   cmd.Parameters.Clear();
   con.Close();
   Response.Redirect("WebForm1.aspx?stat=insert");
}

在第一次之后在Button2_Click中的 for 循环中,它将重定向页面,所以现在您的方法将是这样的

替换此方法

protected void Button2_Click(object sender, EventArgs e)
{


    foreach (GridViewRow oItem in GridView1.Rows)
    {
        string str1 = oItem.Cells[0].Text;
        string str2 = oItem.Cells[1].Text;
        string str3 = oItem.Cells[2].Text;
        insertData(str1, str2, str3);
    }
   Response.Redirect("WebForm1.aspx?stat=insert");
}

public void insertData(string str1,string str2,string str3)
{
    SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["CIMProRPT01ConnectionString"].ConnectionString);
   string sql = "insert into test (test1,test2,test3) values ('" + str1 + "','" + str2 + "','" + str3 + "')";
   SqlCommand cmd = new SqlCommand(sql, con);
   con.Open();
   cmd.ExecuteNonQuery();
   cmd.Parameters.Clear();
   con.Close();

}
于 2013-06-20T05:28:59.743 回答