473,836 Members | 1,560 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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=f alse', but
doesn't get executed until a second click when 'autopostback=t rue'?

For example, I set up a textbox control with autopostback=fa lse, 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=tr ue 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.P age

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArg s)
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_Selec tedIndexChanged (ByVal sender As Object, ByVal e
As System.EventArg s) Handles ddAnimals.Selec tedIndexChanged

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

ddAnimals.Selec tedIndex.ToStri ng & ". Data value = " & _

ddAnimals.Items (ddAnimals.Sele ctedIndex).ToSt ring & ".")

'Response.Write (Request.Form)

End Sub

Protected Sub Button1_Click(B yVal sender As Object, ByVal e As
System.EventArg s) Handles Button1.Click

Response.Write( "Button clicked")

'Response.Write (Request.Form)

End Sub

Protected Sub txtBookname_Tex tChanged(ByVal sender As Object, ByVal e As
System.EventArg s) Handles txtBookname.Tex tChanged

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

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.dt d">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>Untitl ed Page</title>

</head>

<body>

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

<div>

&nbsp;<asp:Text Box ID="txtBookname " runat="server"
AutoPostBack="T rue"></asp:TextBox>

&nbsp;

<asp:RequiredFi eldValidator ID="rfvBookname " runat="server"
ControlToValida te="txtBookname "

ErrorMessage="R equired input." ToolTip="Valida tion of Book Name field"
Width="1px">*</asp:RequiredFie ldValidator>

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

<asp:DropDownLi st ID="ddAnimals" runat="server" AutoPostBack="F alse">

<asp:ListItem>D og</asp:ListItem>

<asp:ListItem>P ig</asp:ListItem>

<asp:ListItem>H orse</asp:ListItem>

<asp:ListItem Value="Bull">Co w</asp:ListItem>

<asp:ListItem>M oose</asp:ListItem>

</asp:DropDownLis t>

<br />

<br />

<br />

<br />

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

<Items>

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

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

</Items>

</asp:Menu>

&nbsp;&nbsp; <br />

<br />

<asp:Validation Summary 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 11631

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=f alse', but
doesn't get executed until a second click when 'autopostback=t rue'?

For example, I set up a textbox control with autopostback=fa lse, 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=tr ue 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.P age

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArg s)
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_Selec tedIndexChanged (ByVal sender As Object, ByVal e
As System.EventArg s) Handles ddAnimals.Selec tedIndexChanged

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

ddAnimals.Selec tedIndex.ToStri ng & ". Data value = " & _

ddAnimals.Items (ddAnimals.Sele ctedIndex).ToSt ring & ".")

'Response.Write (Request.Form)

End Sub

Protected Sub Button1_Click(B yVal sender As Object, ByVal e As
System.EventArg s) Handles Button1.Click

Response.Write( "Button clicked")

'Response.Write (Request.Form)

End Sub

Protected Sub txtBookname_Tex tChanged(ByVal sender As Object, ByVal e As
System.EventArg s) Handles txtBookname.Tex tChanged

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

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.dt d">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>Untitl ed Page</title>

</head>

<body>

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

<div>

&nbsp;<asp:Text Box ID="txtBookname " runat="server"
AutoPostBack="T rue"></asp:TextBox>

&nbsp;

<asp:RequiredFi eldValidator ID="rfvBookname " runat="server"
ControlToValida te="txtBookname "

ErrorMessage="R equired input." ToolTip="Valida tion of Book Name field"
Width="1px">*</asp:RequiredFie ldValidator>

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

<asp:DropDownLi st ID="ddAnimals" runat="server" AutoPostBack="F alse">

<asp:ListItem>D og</asp:ListItem>

<asp:ListItem>P ig</asp:ListItem>

<asp:ListItem>H orse</asp:ListItem>

<asp:ListItem Value="Bull">Co w</asp:ListItem>

<asp:ListItem>M oose</asp:ListItem>

</asp:DropDownLis t>

<br />

<br />

<br />

<br />

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

<Items>

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

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

</Items>

</asp:Menu>

&nbsp;&nbsp; <br />

<br />

<asp:Validation Summary 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
6588
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
9371
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 list, and they all need to set autopostback property to be true, in order to make the event of the control fires. I know this is the must, but I dont know why. Please advise! Thanks!
4
9052
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 to the server WITHOUT the validators invoking their validation. It seems that the easiest solution would have been for MS to simply give the DropDownList control a "CausesValidation" property, but it does not have one. How can I invoke the...
6
327
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 th Web Form on the Browser. While If an AutoPostBack is False then the Web Form Stays on the browser and does not take a trip to the server Please let me know If I am saying correct Any help much appreciated
0
1250
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 down has two items and AutoPostBack is set to true. When a user selects an item in the first drop down, the values for the second drop down are filled in the SelectedIndexChanged event depending on what the user selected. Simple stuff. The pages...
3
4043
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 autopostback on, the autopostback does not trigger. The autopostback will trigger if I hit the enter key while in the textbox but not when I leave focus. Here is the code for the textbox. <asp:textbox class="required" AutoPostBack="True"...
2
5310
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 displays a single TextBox. There's also a CheckBox in this ASPX page within the <formtags. Note that the CheckBox IS NOT a part of the user control. It has been explicitly added to the Form existing in the ASPX page. The AutoPostBack property of...
0
3743
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 dropdownlist uses that control's selected value to drive what it shows. They're all set to autopostback. Everything works fine except where one of the levels only has one entry. This seems to mess up the next level down and you see the wrong
6
4850
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 = new TextBox(); txt.MaxLength = parm.MaxLength; txt.ID = parm.ParameterNameID; txt.Text = "12345";
0
9668
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10840
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10546
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10588
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10254
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9371
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7790
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
2
4013
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3112
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.