473,405 Members | 2,421 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Use of autopostback in VB.NET

Question: Why does a button event (ie: Button1_Click) get executed on the
first click for a textbox control which has 'autopostback=false', but
doesn't get executed until a second click when 'autopostback=true'?

For example, I set up a textbox control with autopostback=false, and a
command button. If I run my page (main.aspx) I can type data into the
textbox control, then click on the button. The Button1_Click event fires
immediately.

But if I just change the autopostback=true for the textbox and repeat the
same events the Button1_Click event doesn't fire until the second time I
click on the button.

Why is this?

Here is my stuff:

Partial Class Main

Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load

If (Not IsPostBack) Then

ddAnimals.Items.Add("Mouse")

ddAnimals.Items.Add("Kangaroo")

ddAnimals.Items.Remove("Moose")

Else

Response.Write("This is a Page_Load due to PostBack.")

End If

End Sub

Protected Sub ddAnimals_SelectedIndexChanged(ByVal sender As Object, ByVal e
As System.EventArgs) Handles ddAnimals.SelectedIndexChanged

My.Response.Write("Selected index changed in ddAnimals. Index value = " & _

ddAnimals.SelectedIndex.ToString & ". Data value = " & _

ddAnimals.Items(ddAnimals.SelectedIndex).ToString & ".")

'Response.Write(Request.Form)

End Sub

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click

Response.Write("Button clicked")

'Response.Write(Request.Form)

End Sub

Protected Sub txtBookname_TextChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles txtBookname.TextChanged

'Response.Write("Text Changed in textbox! New value = " & txtBookname.Text)

End Sub

End Class



<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Main.aspx.vb"
Inherits="main" %>

<!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>Untitled Page</title>

</head>

<body>

<form id="form1" runat="server">

<div>

&nbsp;<asp:TextBox ID="txtBookname" runat="server"
AutoPostBack="True"></asp:TextBox>

&nbsp;

<asp:RequiredFieldValidator ID="rfvBookname" runat="server"
ControlToValidate="txtBookname"

ErrorMessage="Required input." ToolTip="Validation of Book Name field"
Width="1px">*</asp:RequiredFieldValidator>

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;

<asp:DropDownList ID="ddAnimals" runat="server" AutoPostBack="False">

<asp:ListItem>Dog</asp:ListItem>

<asp:ListItem>Pig</asp:ListItem>

<asp:ListItem>Horse</asp:ListItem>

<asp:ListItem Value="Bull">Cow</asp:ListItem>

<asp:ListItem>Moose</asp:ListItem>

</asp:DropDownList>

<br />

<br />

<br />

<br />

<asp:Menu ID="mnuMain" runat="server">

<Items>

<asp:MenuItem Navigateurl="search.aspx" target="_blank" Text="SEARCH"
ToolTip="Search contracting out notices"

Value="SEARCH"></asp:MenuItem>

</Items>

</asp:Menu>

&nbsp;&nbsp;<br />

<br />

<asp:ValidationSummary ID="vsMainPage" runat="server" Height="197px"
Width="468px" />

<asp:Button ID="Button1" runat="server" Text="Button" /></div>

</form>

</body>

</html>
Apr 19 '06 #1
2 11555

Tabbing off the textbox with autopostback generates a post to the server.

Tom Edelbrok wrote:
Question: Why does a button event (ie: Button1_Click) get executed on the
first click for a textbox control which has 'autopostback=false', but
doesn't get executed until a second click when 'autopostback=true'?

For example, I set up a textbox control with autopostback=false, and a
command button. If I run my page (main.aspx) I can type data into the
textbox control, then click on the button. The Button1_Click event fires
immediately.

But if I just change the autopostback=true for the textbox and repeat the
same events the Button1_Click event doesn't fire until the second time I
click on the button.

Why is this?

Here is my stuff:

Partial Class Main

Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load

If (Not IsPostBack) Then

ddAnimals.Items.Add("Mouse")

ddAnimals.Items.Add("Kangaroo")

ddAnimals.Items.Remove("Moose")

Else

Response.Write("This is a Page_Load due to PostBack.")

End If

End Sub

Protected Sub ddAnimals_SelectedIndexChanged(ByVal sender As Object, ByVal e
As System.EventArgs) Handles ddAnimals.SelectedIndexChanged

My.Response.Write("Selected index changed in ddAnimals. Index value = " & _

ddAnimals.SelectedIndex.ToString & ". Data value = " & _

ddAnimals.Items(ddAnimals.SelectedIndex).ToString & ".")

'Response.Write(Request.Form)

End Sub

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click

Response.Write("Button clicked")

'Response.Write(Request.Form)

End Sub

Protected Sub txtBookname_TextChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles txtBookname.TextChanged

'Response.Write("Text Changed in textbox! New value = " & txtBookname.Text)

End Sub

End Class



<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Main.aspx.vb"
Inherits="main" %>

<!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>Untitled Page</title>

</head>

<body>

<form id="form1" runat="server">

<div>

&nbsp;<asp:TextBox ID="txtBookname" runat="server"
AutoPostBack="True"></asp:TextBox>

&nbsp;

<asp:RequiredFieldValidator ID="rfvBookname" runat="server"
ControlToValidate="txtBookname"

ErrorMessage="Required input." ToolTip="Validation of Book Name field"
Width="1px">*</asp:RequiredFieldValidator>

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;

<asp:DropDownList ID="ddAnimals" runat="server" AutoPostBack="False">

<asp:ListItem>Dog</asp:ListItem>

<asp:ListItem>Pig</asp:ListItem>

<asp:ListItem>Horse</asp:ListItem>

<asp:ListItem Value="Bull">Cow</asp:ListItem>

<asp:ListItem>Moose</asp:ListItem>

</asp:DropDownList>

<br />

<br />

<br />

<br />

<asp:Menu ID="mnuMain" runat="server">

<Items>

<asp:MenuItem Navigateurl="search.aspx" target="_blank" Text="SEARCH"
ToolTip="Search contracting out notices"

Value="SEARCH"></asp:MenuItem>

</Items>

</asp:Menu>

&nbsp;&nbsp;<br />

<br />

<asp:ValidationSummary ID="vsMainPage" runat="server" Height="197px"
Width="468px" />

<asp:Button ID="Button1" runat="server" Text="Button" /></div>

</form>

</body>

</html>

Apr 19 '06 #2
To add to John's answer,

For a Textbox, when you set the AutoPostback property to True, it means
that a PostBack to the server will occur when the Text has been
changed. But the TextChange is recognized only if you press Enter after
changing the text, or the Textbox loses focus.

This is probably the reason for the behaviour you are seeing.

Regards,

Cerebrus.

Apr 20 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Susan van Houen | last post by:
Hi All, How do I intercept an autopostback on the client side and prevent it from executing the submit? In classic ASP I used to intercept the on_submit and just return false;
8
by: Matthew Louden | last post by:
why need to set autopostback property to be true?? I know autopostback event means to send the form to the server automatically. I tried checkbox, checkbox list, radio button, and radio button...
4
by: Scott M. | last post by:
If I put RequiredFiledValidators on a page and set them up with corresponding TextBoxes everything works just fine. If I add a DropDownList and set its AutoPostBack to True, I am able to post data...
6
by: Sunil | last post by:
Dear All I am a bit confused about AutoPost Back. My concept about AutoPostBack is that If AutoPostBack i "TRUE" for a Web Form the Web Form Goes back to the server and displays a fresh copy of...
0
by: Scott | last post by:
Hi. I'm having some problems with AutoPostBack in my asp.net pages running on Windows 2003/IIS 6. The pages are really simple. For example, one of them contains two dropDownLists. The firstdrop...
3
by: Brad | last post by:
The first text on my form is a numeric field. I have a javascript that runs on this field for onkeyup (validate the key strokes and modifies fields on the screen) but when I do this and have the...
2
by: rn5a | last post by:
Assume that a user control has a TextBox (Page1.ascx) <asp:TextBox ID="txtName" runat="server"/> I am encapsulating the above user control in a ASPX page named Page1.aspx i.e. the ASPX page...
0
by: andy | last post by:
Hi, I have a form uses several dropdownlists to narrow a set of criteria. ( This is in turn used to control what is shown on a gridview. ) With each, the user selects an entry and then the next...
6
by: Peter | last post by:
ASP.NET 2.0 Visual Studio 2008 I have the following code and when the textbox displays and I press Enter while in the text box I get AutoPostBack, how do I stop AutoPostBack? TextBox txt =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.