我正在使用编辑和删除选项在 Web 表单中制作记录列表。我已检索文本框字段中的值以进行更新。现在,当用户单击更新按钮时,记录就会更新。
如何将其保留在同一页面上或重定向到另一个页面?
这是我的代码:
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
binddata();
}
}
SqlConnection con;
SqlDataAdapter da;
DataSet ds;
void binddata()
{
string pid = Request.QueryString["Prod_Id"];
con = new SqlConnection("Data Source=.\\sqlexpress; initial catalog=PracticeDb; user id=sa; pwd=manager;");
con.Open();
da = new SqlDataAdapter("Select * from Products where Prod_Id='" + pid + "'", con);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
string P_Name = ds.Tables[0].Rows[0]["Prod_Name"].ToString();
string U_Prce = ds.Tables[0].Rows[0]["Unit_Price"].ToString();
string I_Hnd = ds.Tables[0].Rows[0]["In_Hand"].ToString();
string Fxd = ds.Tables[0].Rows[0]["Fixed"].ToString();
string Stus = ds.Tables[0].Rows[0]["Status"].ToString();
TextBox1.Text = P_Name;
TextBox2.Text = U_Prce;
TextBox3.Text = I_Hnd;
TextBox4.Text = Fxd;
TextBox5.Text = Stus;
}
protected void Unnamed1_Click(object sender, EventArgs e)
{
string pid = Request.QueryString["Prod_Id"];
var Pd_nme = TextBox1.Text;
decimal Uni_prce = decimal.Parse(TextBox2.Text);
int In_hnd = int.Parse(TextBox3.Text);
string Fxd = TextBox4.Text;
string Stus = TextBox5.Text;
SqlConnection con = new SqlConnection();
con.ConnectionString = ("Data Source=.\\sqlexpress; initial catalog=PracticeDb; user id=sa; pwd=manager;");
con.Open();
string qry = "UPDATE PRODUCTS SET Prod_Name='" + Pd_nme + "',Unit_Price='" + Uni_prce + "',In_Hand='" + In_hnd + "',Fixed='" + Fxd + "',Status='" + Stus + "' where Prod_Id='" + pid + "'";
SqlCommand cmd = new SqlCommand(qry, con);
cmd.ExecuteNonQuery();
con.Close();
}
}
}
谢谢。