【发布时间】: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 属性是否会有所作为。