如何更新用户输入的文本框的值
How to update Value from the textbox user puts in
我有这样的代码
<form id="form1" runat="server">
<asp:Label ID="Label1" runat="server" Visible="False" Text="Please update the price"></asp:Label>
<br />
<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</form>
而 C# 是
namespace WebApplication6
{
public partial class WebForm20 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["onealbumid"] != null)
{
Label1.Visible = true;
int onealbumid = Convert.ToInt32(Session["onealbumid"]);
String Artists = System.Configuration.ConfigurationManager.ConnectionStrings["FleetManagementConnectionString"].ConnectionString;
System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(Artists);
SqlCommand cmd = new SqlCommand("Select Price from OneAlbum where OneALbumID='" + onealbumid + "'", con);
con.Open();
TextBox1.Text = cmd.ExecuteScalar().ToString();
con.Close();
}
else {
Response.Redirect("~/BENEinsertOneAlbum3.aspx");
}
}
protected void Button1_Click(object sender, EventArgs e)
{
String Artists = System.Configuration.ConfigurationManager.ConnectionStrings["FleetManagementConnectionString"].ConnectionString;
System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(Artists);
int onealbumid = Convert.ToInt32(Session["onealbumid"]);
SqlCommand command = new SqlCommand(@"UPDATE [dbo].[OneAlbum]
SET Price=@Price where OneAlbumID =" + onealbumid + ";", con);
command.Parameters.AddWithValue("@Price", TextBox1.Text);
con.Open();
command.ExecuteNonQuery();
con.Close();
Response.Redirect("~/BENEinsertOneAlbum3.aspx");
}
}
}
在这个页面之前有一个page/form,用户select用户想从gridview中更改的价格
我希望用户在此页面的文本框 1 中编辑新的 "Price"。
所以这正在发生。
比如我在本页前选择"Price"22,当本页opens/loadstextbox1显示22时
所以我在 Texbox1 中 change/type 到 40,然后单击按钮。
价格还是22,不是40 我查了数据库,还是没变,还是22
为什么会这样?
Page_Load
将覆盖您输入的数据。进行 !IsPostBack
检查,这样当您单击按钮时,数据不会恢复到加载表单时的状态。
此外,此代码中的 gridview
在哪里?有这样一个 class,因此请注意您在给出的代码中使用的术语。
我有这样的代码
<form id="form1" runat="server">
<asp:Label ID="Label1" runat="server" Visible="False" Text="Please update the price"></asp:Label>
<br />
<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</form>
而 C# 是
namespace WebApplication6
{
public partial class WebForm20 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["onealbumid"] != null)
{
Label1.Visible = true;
int onealbumid = Convert.ToInt32(Session["onealbumid"]);
String Artists = System.Configuration.ConfigurationManager.ConnectionStrings["FleetManagementConnectionString"].ConnectionString;
System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(Artists);
SqlCommand cmd = new SqlCommand("Select Price from OneAlbum where OneALbumID='" + onealbumid + "'", con);
con.Open();
TextBox1.Text = cmd.ExecuteScalar().ToString();
con.Close();
}
else {
Response.Redirect("~/BENEinsertOneAlbum3.aspx");
}
}
protected void Button1_Click(object sender, EventArgs e)
{
String Artists = System.Configuration.ConfigurationManager.ConnectionStrings["FleetManagementConnectionString"].ConnectionString;
System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(Artists);
int onealbumid = Convert.ToInt32(Session["onealbumid"]);
SqlCommand command = new SqlCommand(@"UPDATE [dbo].[OneAlbum]
SET Price=@Price where OneAlbumID =" + onealbumid + ";", con);
command.Parameters.AddWithValue("@Price", TextBox1.Text);
con.Open();
command.ExecuteNonQuery();
con.Close();
Response.Redirect("~/BENEinsertOneAlbum3.aspx");
}
}
}
在这个页面之前有一个page/form,用户select用户想从gridview中更改的价格
我希望用户在此页面的文本框 1 中编辑新的 "Price"。
所以这正在发生。
比如我在本页前选择"Price"22,当本页opens/loadstextbox1显示22时
所以我在 Texbox1 中 change/type 到 40,然后单击按钮。
价格还是22,不是40 我查了数据库,还是没变,还是22
为什么会这样?
Page_Load
将覆盖您输入的数据。进行 !IsPostBack
检查,这样当您单击按钮时,数据不会恢复到加载表单时的状态。
此外,此代码中的 gridview
在哪里?有这样一个 class,因此请注意您在给出的代码中使用的术语。