2011-12-16 24 views
2

[OK]を、私は何時間(検索など)の周りを回っています。これは私がやりたいことです、私はテキストボックスにデータをロードしたいと思って、ユーザーがテキストボックス内のテキストを変更する場合、私は新しいテキストを保存することができます。 TxtBox_TextChangedイベントでTextBoxでデータが変更されていない

私の問題txtNarrativeテキストボックスに含まれるデータはtxtNarrativeに含まれるデータ(<> ABCD)ではなくbtnSubmit_Clickイベントで入力したユーザーは、元の値がABCDであることを新しいデータです。

私は何が間違っていますか?背後に

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="WorkBench_VBNet._Default" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
     <fieldset> 
     <span class="title">Entry Form</span> 
      <ul class="pageitem"> 
       <li class="Narrative"> 
        <asp:TextBox EnableViewState=true ID="txtNarrative" placeholder="Narrative" Width="100%" 
          Rows="10" TextMode="multiline" runat="server" Height = "100%" OnTextChanged="TxtBox_TextChanged" > 
        </asp:TextBox></li> 

       <li class="Submit"> 
        <asp:LinkButton ID="btnSubmit" runat="server">Submit</asp:LinkButton> 
       </li> 
      </ul> 
     </fieldset> 
    </div> 
    </form> 
</body> 
</html> 

コード:

Public Class _Default 
    Inherits System.Web.UI.Page 

    Public Event TextChanged As EventHandler 

    Protected Sub TxtBox_TextChanged(ByVal sender As Object, _ 
     ByVal e As System.EventArgs) Handles txtNarrative.TextChanged 


     ViewState("txtNarrative") = txtNarrative.Text ''<-- The text here is the changed text not ABCD 
     txtNarrative.Text = ViewState("txtNarrative").ToString 

    End Sub 

    Private Sub btnSubmit_Click(sender As Object, e As System.EventArgs) Handles btnSubmit.Click 
     Dim Narrative as String = txtNarrative.Text '<-- the text in the text box is still ABCD not what was changed. 

     ''Code to update data in the Database goes here 
    End Sub 


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
     If Not IsPostBack Then 
      txtNarrative.Text = "ABCD" 
     End If 
    End Sub 

End Class 
+0

に役立ちます。ToString'?どうして 'txtNarrative.Text'を使うことができないのですか? –

+0

ViewStateプロパティを使うと違いが出るかどうかを調べました。 –

答えて

0

は、それが必要ではないです、TxtBox_TextChangedサブを取り除く、TextBoxIPostBackDataHandlerはあなたのためにそれを行います。

<asp:TextBox EnableViewState=true ID="txtNarrative" placeholder="Narrative" Width="100%" 
         Rows="10" TextMode="multiline" runat="server" Height = "100%" > 
       </asp:TextBox> 
0

私はありませんあなたの質問を理解してもよろしいですか?

あなたのマークアップに "OnTextChanged"テキストボックスが必要です。

あなたのコードの背後には、alredyがハンドル宣言を持っています。

( "OnTextChanged"をHTMLマークアップで使用し、コード内のコードを処理する)テキストボックスイベントは2回トリガーされます。

Protected Sub TxtBox_TextChanged (ByVal sender As Object, _ 
     ByVal e As System.EventArgs) Handles txtNarrative.TextChanged 


     ViewState ("txtNarrative") = txtNarrative.Text''<-- The text here text is not changed the ABCD 
     txtNarrative.Text = ViewState ("txtNarrative"). ToString 

    end Sub 


    <asp: TextBox EnableViewState = true ID = "txtNarrative" placeholder = "Narrative" Width = "100%" 
          Rows = "10" TextMode = "multiline" runat = "server" Height = "100%"> 
        </ asp: TextBox> 

もコードをテストしました。送信される値は、テキストボックスの新しい値です。 (つまり、私はあなたの質問を理解している場合)

希望を、これはあなたがこのコード `ViewStateの( "txtNarrative")= txtNarrative.Text txtNarrative.Text = ViewStateは( "txtNarrative")を必要とするのはなぜ

関連する問題