0

これは私の最初の投稿です。Visual Studio を使用して Azure アプリケーションを作成しています。

「更新ページ」をやりたい。

これは私が実装したいステップです:

1) ユーザーは DropDownList から 1 つの ID を選択します

2) ユーザーが HTML ボタンを押す

3) 「システム」は、SQL 文からの情報でいくつかの TextBox を埋めます: Select...where Id= DropDownList1.SelectedValue.ToString()

4) ユーザーは TextBox の情報を変更し、ASP ボタンを押すことができます。

5) システムは、TextBox の情報を使用して SQL UPDATE 文を実行します。

私は1 つの DropDownList、 IDPRODUCT を持っています

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

「Page Load」に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 ボタンを 1 つ持っています。その機能、次のような 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ユーザーが最初にページを要求したときにのみバインドする必要があります。ページにバインドする前に、ユーザーがポストバックしていないことを確認します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 に答える