0

这是我的第一篇文章。我正在使用 Visual Studio 制作 Azure 应用程序。

我想做一个“更新页面”。

这是我要实施的步骤:

1) 用户从 DropDownList 中选择一个 ID

2) 用户按下 HTML 按钮

3)“系统”用 SQL 语句中的信息填充一些文本框:Select...where Id= DropDownList1.SelectedValue.ToString()

4) 用户可以更改文本框上的一些信息并按下 ASP 按钮

5) 系统根据文本框的信息执行 SQL UPDATE 语句

我有一个下拉列表,IDPRODUCT

<asp:DropDownList ID="DropDownList1" OnSelectedIndexChanged="index_Changed"  runat="server" ></asp:DropDownList> 

我从“页面加载”上的 SQL 语句填充它

DropDownList1.Items.Clear();
            DropDownList1.Items.Add(" ");

            string consultaComboIdCompra = "SELECT DISTINCT IdCompra FROM Compras";
            SqlCommand sqlCommandComboIdCompra = new SqlCommand(consultaComboIdCompra, sqlConnection);
            sqlConnection.Open();
            SqlDataReader readerComboIdCompra = sqlCommandComboIdCompra.ExecuteReader();


            if (readerComboIdCompra.HasRows)
            {
                while (readerComboIdCompra.Read())
                {
                    DropDownList1.Items.Add(readerComboIdCompra.GetString(0));
                }
            }

            sqlConnection.Close();

我有一个 HTML 按钮,它的功能应该是用 SQL 语句的结果填充一些 TextBox,例如:

 string consulta2 = "SELECT Unidades FROM Productos WHERE IdProducto = '" + DropDownList1.SelectedValue.ToString() + "'";
 SqlCommand sqlCommand2 = new SqlCommand(consulta2, sqlConnection);
 sqlConnection.Open();
 SqlDataReader reader2 = sqlCommand2.ExecuteReader();

 if (reader2.HasRows)
 {
   while (reader2.Read())
   {

       TextBox4.Text = reader2.GetString(0);
   }
 }
 sqlConnection.Close();

最后我已经实现了 UPDATE 语句

if (IsPostBack)
            {
string idCompra = DropDownList1.SelectedValue.ToString();

string consulta3 = "UPDATE Compras SET Unidades = '" + TextBox1.Text + "' WHERE IdCompra = '" + idCompra + "'";
                SqlCommand sqlCommand3 = new SqlCommand(consulta3, sqlConnection);
                sqlConnection.Open();
                SqlDataReader reader3 = sqlCommand3.ExecuteReader();
                sqlConnection.Close();

}

我不能“自动回答”,所以我编辑了我的问题由于 nunespascal,最终有效的代码是下一个:

 public partial class WebFormProductosOpcion5 : System.Web.UI.Page
        {
            static string strSQLconnection = *********
            static SqlConnection sqlConnection = new SqlConnection(strSQLconnection);
            protected void Page_Load(object sender, EventArgs e)
            {
                if(!IsPostBack)
                {
                DropDownList1.Items.Clear();
                DropDownList1.Items.Add(" ");

                string consultaComboIdProducto = "SELECT DISTINCT IdProducto FROM Productos";
                SqlCommand sqlCommandComboIdProducto = new SqlCommand(consultaComboIdProducto, sqlConnection);
                sqlConnection.Open();
                SqlDataReader readerComboIdProducto = sqlCommandComboIdProducto.ExecuteReader();


                if (readerComboIdProducto.HasRows)
                {
                    while (readerComboIdProducto.Read())
                    {
                        DropDownList1.Items.Add(readerComboIdProducto.GetString(0));
                    }
                }

                sqlConnection.Close();



            }

            }

            protected void html_click(object sender, EventArgs e)
            {
                TextBox2.Text = "HOLA";
                Debug.WriteLine("HOLA");



            }



            protected void HTML_Button_Click(object sender, EventArgs e)
            {
                string idProductoSeleccionado = DropDownList1.SelectedValue.ToString();
                string consultaUltimo = "SELECT * FROM Productos WHERE IdProducto = '" + idProductoSeleccionado + "'";
                SqlCommand sqlCommandUltimo = new SqlCommand(consultaUltimo, sqlConnection);
                sqlConnection.Open();
                SqlDataReader readerUltimo = sqlCommandUltimo.ExecuteReader();

                if (readerUltimo.HasRows)
                {
                    while (readerUltimo.Read())
                    {
                        //Put the name
                        TextBox2.Text = readerUltimo.GetString(1);
                    }
                }

                sqlConnection.Close();
                //Label10.Text = col1;
                sqlConnection.Close();
            }



            protected void Button1_Click(object sender, EventArgs e)
            {

                    //string consulta = "select * from Productos where IdProducto ='";
                    //consulta = consulta + idP + "'";
                    SqlCommand sqlCommand = new SqlCommand(consulta, sqlConnection);
                    sqlConnection.Open();
                    SqlDataReader reader = sqlCommand.ExecuteReader();
                    sqlConnection.Close();
                    //Mensaje Modificación Correcta
                    Response.Write("<script language=javascript>alert('Modificado con éxito');</script>");




            }
        }
4

1 回答 1

0

DropDownList您只需要在用户第一次请求页面时绑定 a 。在页面上绑定之前检查用户是否没有回帖Load

if(!IsPostBack)
{
        DropDownList1.Items.Clear();
        DropDownList1.Items.Add(" ");

        string consultaComboIdCompra = "SELECT DISTINCT IdCompra FROM Compras";
        SqlCommand sqlCommandComboIdCompra = new SqlCommand(consultaComboIdCompra, sqlConnection);
        sqlConnection.Open();
        SqlDataReader readerComboIdCompra = sqlCommandComboIdCompra.ExecuteReader();


        if (readerComboIdCompra.HasRows)
        {
            while (readerComboIdCompra.Read())
            {
                DropDownList1.Items.Add(readerComboIdCompra.GetString(0));
            }
        }

        sqlConnection.Close();
}

由于您正在更新按钮,因此您无需检查IsPostBack. 将该代码放在按钮Click事件处理程序上。这样它只会在有人点击你的更新按钮时运行

于 2012-09-08T16:41:45.520 回答