好的,我一直在四处走动(搜索等)。这就是我想要做的,我想将数据加载到文本框中,如果用户更改了文本框中的文本,我希望能够保存新文本。
我在TxtBox_TextChanged事件中的问题是txtNarrative文本框中包含的数据是用户输入的新数据(<> ABCD),但在btnSubmit_Click事件中,txtNarrative中包含的数据是原始值ABCD。 / p>
我做错了什么?
<%@ 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 :(得分:0)
摆脱TxtBox_TextChanged
Sub,不需要,TextBox
的{{3}}会为您做到这一点:
<asp:TextBox EnableViewState=true ID="txtNarrative" placeholder="Narrative" Width="100%"
Rows="10" TextMode="multiline" runat="server" Height = "100%" >
</asp:TextBox>
答案 1 :(得分:0)
我不确定我是否理解你的问题但是:
您的标记中不需要“OnTextChanged”文本框。
在您的代码中,alredy处理声明。
如果同时使用两者(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>
还测试了您的代码,提交的值是文本框中的新值。 (即如果我理解你的问题)
希望这有帮助