469,594 Members | 2,111 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,594 developers. It's quick & easy.

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 10643

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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Susan van Houen | last post: by
4 posts views Thread by Scott M. | last post: by
6 posts views Thread by Sunil | last post: by
reply views Thread by Scott | last post: by
3 posts views Thread by Brad | last post: by
reply views Thread by andy | last post: by
6 posts views Thread by Peter | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.