I am working on a website where i have some textbox with ReadOnly value as false i.e. it is editable. On page load the original name(value) of the user is loaded from the database which he/she can change/update if there is some mistake in it. But when i change the value and click on submit, it still takes the value that was previously stored instead of the new data given. The .aspx code snippet of the textbox:
<div style="width: 80%">
<div >
<div >
<div >Trainee Name</div>
</div>
<asp:TextBox ID="TextBox2" runat="server" CssClass="form-control"></asp:TextBox>
</div>
</div>
.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
string UserId = Session["new2"].ToString();
Response.Write(UserId);
try
{
mycon = new MySqlConnection("server=localhost;database=trainee;user id=abcd;password=abcde@gea;");
MyCm = new MySqlCommand("select * from trainee_master where trainee_master.Reference_Number='" UserId.ToString() "'", mycon);
myad = new MySqlDataAdapter(MyCm);
DataTable dt = new DataTable();
myad.Fill(dt);
if (dt.Rows.Count > 0)
{
TextBox1.Text = dt.Rows[0][0].ToString();
TextBox2.Text = dt.Rows[0][1].ToString();
DropDownList1.Text = dt.Rows[0][2].ToString();
DropDownList2.Text = dt.Rows[0][3].ToString();
TextBox5.Text = dt.Rows[0][9].ToString();
TextBox6.Text = dt.Rows[0][4].ToString();
TextBox7.Text = dt.Rows[0][5].ToString();
TextBox8.Text = dt.Rows[0][6].ToString();
TextBox9.Text = dt.Rows[0][7].ToString();
TextBox10.Text = dt.Rows[0][8].ToString();
}
//TextBox2.Focus();
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
protected void Button1_Click(object sender, EventArgs e) //Submit button
{
try
{
string s1 = TextBox1.Text;
Response.Write(s1);
string s2 = TextBox2.Text;
Response.Write(s2);
string s3 = DropDownList1.Text;
Response.Write(s3);
string s4 = DropDownList2.Text;
Response.Write(s4);
string s5 = TextBox5.Text;
Response.Write(s5);
string s6 = TextBox6.Text;
Response.Write(s6);
string s7 = TextBox7.Text;
Response.Write(s7);
string s8 = TextBox8.Text;
Response.Write(s8);
string s9 = TextBox9.Text;
Response.Write(s9);
string s10 = TextBox10.Text;
Response.Write(s10);
string sk = "update trainee_master set Name='" TextBox2.Text "',Discipline='" DropDownList1.Text "',Branch='" DropDownList2.Text "',Mobile_No='" TextBox6.Text "',Email='" TextBox7.Text "',Guide_PNo='" TextBox8.Text "',Guide_Name='" TextBox9.Text "',Project_Title='" TextBox10.Text "',College_Name='" TextBox5.Text "' where trainee_master.Reference_Number='" TextBox1.Text "'";
cmd1 = new MySqlCommand(sk, mycon);
mycon.Open();
cmd1.ExecuteNonQuery();
Response.Write("<script>window.alert('Record updated Successfully')</script>");
mycon.Close();
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
}
}
How to make the textbox dynamically editable?
CodePudding user response:
Your page load event triggers EACH time.
Remember, even for a simple button you place on the web page?
EVERY button click, even auto post-back for a drop down list?
The PAGE load event triggers first, and EVERY time, and EACH time.
So, in your case page load triggers - you load up the text box.
Then YOU the user types into that text box.
Then YOU click on the button. Your page load FIRST runs again, and THEN your button.
So, for EVERY page, or at least 99% of them?
Your page load event needs this:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// YOUR load up code goes here
}
}
So, you need a REAL and TRUE first only page load. So, you have to put your on-page load code inside of the above code stub. If you don't test/check for !IsPostBack, then your load up the control(s) code will run every time, and then you never see your changes you done into controls, since that load event triggers EACH time, and triggers BEFORE your button click event.
So, page load - it runs each and every time, and runs for each and every button you click.
With a test for !IsPostBack, then you get a "real" first time, one time page load.