这是我的第一篇文章。我正在使用Visual开发一个Azure应用程序。
我想做一个“更新页面”。
这是我想要实现的步骤:
1)用户从DropDownList中选择一个ID
2)用户按下HTML按钮
3) "System“使用SQL语句中的信息填充一些TextBoxs : Select...where Id= DropDownList1.SelectedValue.ToString()
4)用户可以更改TextBoxs上的一些信息,并按ASP
5)根据TextBoxs的信息,编写SQL更新语句。
我有One DropDownList
<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();最后,我执行了更新语句。
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>");
}
}发布于 2012-09-08 16:41:45
只有当用户第一次请求页面时,才需要绑定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事件处理程序上。这样,只有当有人单击更新按钮时,它才会运行。
https://stackoverflow.com/questions/12332507
复制相似问题