2011-10-31 136 views
1

我在這個問題上搜索和張貼了很多,但無法得到滿意的答案。我想確保每當我打電話給服務器的值文本框應該是最新的文本框。在asp.net和c#中獲取文本框的更新值#

但是當我打電話給服務器的文本框的舊值仍然存在。我知道這是與回發有關,但我沒有得到確切的方式來獲取我的文本框的更新值。 cs文件。

請告訴我什麼是我應該採取的必要步驟,以始終獲得文本框的最新價值。

這裏是我的代碼是不工作:

protected void Page_Load(object sender, EventArgs e) 
    { 


     int contentID = Convert.ToInt32(Request.QueryString["contentid"]); 
     if (contentID != 0) 
     { 
       if (!this.IsPostBack) 
       { 
       getContentBody(contentID); 
       TextBox1.Text = content; 
       msg_lbl.Text="Inside not PostBack"; 
       } 
       else 
       { 
       getContentBody(contentID); 
       TextBox1.Text = content; 
       msg_lbl.Text="Inside PostBack"; 
       } 
      } 


      else 
       Response.Write("Invalid URL for article"); 


    } 



public void getContentBody(int contentID) 
    { 
     try 
     { 
      //////////////Opening the connection/////////////// 

      mycon.Open(); 
      string str = "select content from content where contentID='" + contentID + "'"; 
      //Response.Write(str); 
      MySqlCommand command1 = mycon.CreateCommand(); 
      command1.CommandText = str; 
      dr = command1.ExecuteReader(); 
      if (dr.Read()) 
      { 
       content = dr[0].ToString(); 
      } 
     } 
     catch (Exception ex) 
     { 
      Response.Write("Exception reading data" + ex); 
     } 
     finally 
     { 
      dr.Close(); 
      mycon.Close(); 
     } 
    } 

protected void Button2_Click(object sender, EventArgs e) 
    { 
      //string textboxvalue = Request.Form[TextBox1.UniqueID]; 

      mycon.Open(); 
      string query = "update content set content='" +TextBox1.Text + "' where contentID= '"+contentID +"'"; 
      msg_lbl.Text = query; 
      try 
      { 
       MySqlCommand command1 = mycon.CreateCommand(); 
       command1.CommandText = query; 
       command1.ExecuteNonQuery(); 
       msg_lbl.Text = "text" + TextBox1.Text; 
      } 
      catch (Exception ex) 
      { 
       msg_lbl.Text = "Exception in saving data" + ex; 
      } 
      finally 
      { 
       mycon.Close(); 

      } 


    } 

這裏是我的aspx頁面代碼:

 <asp:TextBox ID="TextBox1" runat="server" Height="500px" 
      TextMode="MultiLine" Width="90%" AutoPostBack="True"></asp:TextBox> 
    </p> 
    <p> 
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
     <asp:Button ID="Button1" runat="server" Text="Delete post" /> 
     &nbsp; 
     <asp:Button ID="Button2" runat="server" onclick="Button2_Click" 
      Text="Save changes" /> 
&nbsp; 
     <asp:Button ID="Button3" runat="server" Text="Cancel" /> 
    </p> 

也請告訴我,爲什麼它不工作的原因,我怎樣才能使這行得通。

感謝,

愛瑪

+0

您可以發佈你的aspx標記和一些示例數據? – Greg

+0

嘗試設置斷點並調試代碼? – Shoban

+0

嗨格雷格,我添加了aspx代碼,但作爲示例數據,請告訴我你期待什麼? –

回答

1

按照ASP.NET的生命週期,Page_Load中Button2_Click之前執行,所以你必須表明在Button2_Click更新的文字:

protected void Page_Load(object sender, EventArgs e) 
{ 


    contentID = Convert.ToInt32(Request.QueryString["contentid"]); 
    if (contentID != 0) 
    { 
     if (!this.IsPostBack) 
     { 
      getContentBody(contentID); 
      TextBox1.Text = content; 
      msg_lbl.Text = "Inside not PostBack"; 
     } 
    } 
    else 
     Response.Write("Invalid URL for article"); 
} 

public void getContentBody(int contentID) 
{ 
    try 
    { 
     //////////////Opening the connection/////////////// 

     mycon.Open(); 
     string str = "select content from content where contentID='" + contentID + "'"; 
     //Response.Write(str); 
     MySqlCommand command1 = mycon.CreateCommand(); 
     command1.CommandText = str; 
     dr = command1.ExecuteReader(); 
     if (dr.Read()) 
     { 
      content = dr[0].ToString(); 
     } 
    } 
    catch (Exception ex) 
    { 
     Response.Write("Exception reading data" + ex); 
    } 
    finally 
    { 
     dr.Close(); 
     mycon.Close(); 
    } 
} 

protected void Button2_Click(object sender, EventArgs e) 
{ 
    //string textboxvalue = Request.Form[TextBox1.UniqueID]; 

    mycon.Open(); 
    string query = "update content set content='" + TextBox1.Text + "' where contentID= '" + contentID + "'"; 
    msg_lbl.Text = query; 
    try 
    { 
     MySqlCommand command1 = mycon.CreateCommand(); 
     command1.CommandText = query; 
     command1.ExecuteNonQuery(); 
     getContentBody(contentID); 
     TextBox1.Text = content; 

     msg_lbl.Text = "text" + TextBox1.Text; 
    } 
    catch (Exception ex) 
    { 
     msg_lbl.Text = "Exception in saving data" + ex; 
    } 
    finally 
    { 
     mycon.Close(); 

    } 


} 
+0

感謝您的回答,這爲我工作... –