Why event not firing right away

T

tfs

I have a small page I am using to play around with events and am
confused why the "onTextChanged" event doesn't fire until I press the
button?

I am trying to allow the user to put something in Textbox 1 and then
when they exit it, have the event fire to make the Textbox 2 =
TextBox 1.

Why doesn't it happen right away? And what would be the best way to
make it do this?

Here is the page:


<%@ Page Language="VB" Trace="True"
Debug="true" ContentType="text/html"
ResponseEncoding="iso-8859-1" %>
<html>
<head>
<title>Contour Intranet</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
</head>
<body>
<script runat="server">
Sub Page_Init (Sender as Object, E as EventArgs)
trace.warn("In Page_init - lblAverage = " &
lblAverage.text & "<br>")
End Sub

Sub Page_PreRender (Sender as Object, E as EventArgs)
trace.warn("In Page_PreRender<br>")
End Sub

Sub Page_Load (Sender as Object, E as EventArgs)
trace.warn("in Page_load<br>")
if IsPostBack
trace.warn("in Page_load -
IsPostBack<br>")
else
trace.warn("in Page_load - Not
IsPostBack<br>")
end if
trace.warn("Exit Page_load<br>")
end sub

Sub Page_Unload (Sender as Object, E as EventArgs)
trace.warn("In Page Unload<br>")
End Sub

Sub changeText2 (s as Object, E as EventArgs)
trace.warn("In changeText2<br>")
lblAverage2.Text = lblAverage.Text
End Sub

Sub OnButton_Click (Sender as Object, E as EventArgs)
trace.warn("In OnButton_Click<br>")
end sub

</script>

<form runat="server">
Average Time to Process:
<asp:textbox
ID="lblAverage"
onTextChanged="changeText2"
text="Textbox 1"
Runat="Server" />

<asp:textbox
ID="lblAverage2"
text="Textbox 2"
Runat="Server" />

<br><br>
Would you like to continue processing?<br><br>
<asp:button ID="btnContinue"
onClick="OnButton_Click" runat="server"
Text="Process" />

</form>
</body>
</html>


Thanks,

Tom
 
G

Guest

What may be the problem is that you do not have AutoPostBack property set to true on the TextBox. If you have the code that transfers the contents of textbox1 to textbox2 on the server-side (codebehind), the textchanged event will only occur there, so the page needs to return to the server to accomplish that. It works when you click the button because the button causes a postback. AutoPostBack will automatically cause the page to reload when the text in the textbox has changed.

Before you go ahead and turn AutoPostBack to true for the textbox, consider adding javascript code to the TextChanged event of the textbox so that the transfer occurs on the client. This way the page does not have to reload and is much better for performance and it will have a more seemless feel.

hope this helps,
John
 
K

Ken Cox [Microsoft MVP]

Hi Tom,

Try settting AutoPostback="True" in the textboxes and see what that does?

<asp:textbox
ID="lblAverage2"
text="Textbox 2"
Runat="Server"
AutoPostback="true"/>

The server doesn't know anything until the data has been sent to it. The
button click accomplishes the serverside event as you would expect.

A way of making the event happen earlier is Autopostback. It fires a
client-side event when you tab away from the changed textbox. The
client-side event forces a server-side event that can be processed.

You might want to do View > Source on the page when AutoPostback="True".
You'll see some interesting JavaScript emited by ASP.NET.

Ken
Microsoft MVP [ASP.NET]
 
T

tfs

That worked.

Now when would I do the AutoPostBack and when the Javascript code?

Why is one better than the other?

I assume that if this is the Internet, I would want to use the
Javascript as going back to the server might take a while and go
through many routers and servers.

But what about an intranet? Would it make a difference there?

Also, couldn't I also use VBScript for my clientside code?

Thanks,

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,481
Members
44,900
Latest member
Nell636132

Latest Threads

Top