如何从文本框用户将在

问题描述:

我有这样如何从文本框用户将在

<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 [email protected] where OneAlbumID =" + onealbumid + ";", con); 

      command.Parameters.AddWithValue("@Price", TextBox1.Text); 
      con.Open(); 
      command.ExecuteNonQuery(); 
      con.Close(); 
      Response.Redirect("~/BENEinsertOneAlbum3.aspx"); 
     } 
    } 
} 

有一个页/表单,这个页面之前更新值,用户选择用户想要从网格视图中更改的价格

我希望用户在此页的文本框1中编辑新的“价格”。

所以现在正在发生。

例如,我选择“价格” 22这个页面之前,当这个页面打开/负载TextBox1中显示22

所以我在Texbox1更改/类型为40,然后单击按钮。

的价格仍然是22不是40,我检查了数据库,仍然没有改变,还是22

为什么会出现这种情况?

+0

这是因为PostBack需要将值存储在Session变量或公共静态变量或Enable ViewState中。并执行'if(IsPostBack){}'检查 – MethodMan

Page_Load将覆盖您输入的数据。把一个!IsPostBack检查一下,以便当你点击按钮时,数据不会被放回到它在表单加载时的状态。

另外,这个代码中的gridview是哪里?有这样的班级,因此要注意你给出的代码给出的条款。