【问题标题】:Data not being changed in TextBox文本框中的数据未更改
【发布时间】:2011-12-16 19:01:40
【问题描述】:

好的,我已经为此转了几个小时(搜索等)。这就是我想要做的,我想将数据加载到文本框中,如果用户更改文本框中的文本,我希望能够保存新文本。

我在 TxtBox_TextChanged 事件中的问题是 txtNarrative 文本框中包含的数据是用户输入的新数据 (ABCD),但在 btnSubmit_Click 事件中,txtNarrative 中包含的数据是原始值 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

【问题讨论】:

  • 为什么需要此代码ViewState("txtNarrative") = txtNarrative.Text txtNarrative.Text = ViewState("txtNarrative").ToString?为什么你不能只使用txtNarrative.Text
  • 我这样做是为了看看使用 ViewState 属性是否会有所作为。

标签: asp.net vb.net webforms


【解决方案1】:

摆脱TxtBox_TextChanged Sub,这不是必需的,TextBoxIPostBackDataHandler 将为您完成:

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

【讨论】:

    【解决方案2】:

    我不确定我是否理解你的问题,但是:

    您不需要在标记中添加“OnTextChanged”文本框。

    在你背后的代码中已经有句柄声明。

    如果您同时使用两者(在您的 html 标记中使用“OnTextChanged”,并在您的代码中使用句柄),则文本框事件将触发两次。

       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>
    

    还测试了您的代码,提交的值是文本框中的新值。 (即如果我理解你的问题)

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-17
      • 1970-01-01
      • 1970-01-01
      • 2020-07-15
      • 1970-01-01
      • 1970-01-01
      • 2013-04-12
      • 1970-01-01
      相关资源
      最近更新 更多