Use of autopostback in VB.NET

T

Tom Edelbrok

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>
 
C

Cerebrus

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.
 

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

Top