我正在一個網站上作業,我有一些文本框,其中 ReadOnly 值為 false,即它是可編輯的。在頁面加載時,用戶的原始名稱(值)是從資料庫中加載的,如果其中有一些錯誤,他/她可以更改/更新。但是當我更改值并單擊提交時,它仍然采用以前存盤的值而不是給定的新資料。文本框的.aspx 代碼片段:
<div class="form-group" style="width: 80%">
<div class="input-group mb-2">
<div class="input-group-prepend">
<div class="input-group-text headDetails">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());
}
}
}
}
如何使文本框動態可編輯?
uj5u.com熱心網友回復:
您的頁面加載事件每次都會觸發。
記住,即使是您在網頁上放置的一個簡單按鈕?
每個按鈕點擊,甚至自動回發下拉串列?
PAGE 加載事件首先觸發,每次觸發,每次觸發。
因此,在您的情況下,頁面加載觸發器 - 您加載了文本框。
然后你是用戶在該文本框中鍵入的內容。
然后你點擊按鈕。您的頁面加載 FIRST 再次運行,然后您的按鈕。
那么,對于每個頁面,或者至少 99% 的頁面?
您的頁面加載事件需要這樣:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// YOUR load up code goes here
}
}
因此,您需要一個真實且真實的第一個頁面加載。因此,您必須將頁面加載代碼放在上述代碼存根中。如果您不測驗/檢查!IsPostBack,那么您加載的控制元件代碼將每次運行,然后您將永遠看不到您對控制元件所做的更改,因為該加載事件每次都會觸發,并在之前觸發你的按鈕點擊事件。
所以,頁面加載 - 它每次都運行,并為您單擊的每個按鈕運行。
通過對 !IsPostBack 的測驗,您將獲得“真正的”第一次,一次頁面加載。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/475739.html
