这是mi的第一篇文章。 我使用Visual Studio制作Azure应用程序。
我想做一个“更新页面”。
这是我要实施的步骤:
1)用户从DropDownList
中选择一个ID2)用户按下HTML按钮
3)“系统”用SQL语句填充一些TextBoxs信息: 选择...其中Id = DropDownList1.SelectedValue.ToString()
4)用户可以更改TextBox上的一些信息并按下ASP按钮
5)系统使用TextBoxs的信息执行SQL UPDATE语句
我有 One DropDownList ,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();
}
我不能“autoanswer”,所以我编辑我的问题 由于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>");
}
}
答案 0 :(得分: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
事件处理程序上。这样,它只会在有人点击您的更新按钮时运行