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>
<asp:TextBox ID="txtBookname" runat="server"
AutoPostBack="True"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvBookname" runat="server"
ControlToValidate="txtBookname"
ErrorMessage="Required input." ToolTip="Validation of Book Name field"
Width="1px">*</asp:RequiredFieldValidator>
<aspropDownList 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>
</aspropDownList>
<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>
<br />
<br />
<asp:ValidationSummary ID="vsMainPage" runat="server" Height="197px"
Width="468px" />
<asp:Button ID="Button1" runat="server" Text="Button" /></div>
</form>
</body>
</html>
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>
<asp:TextBox ID="txtBookname" runat="server"
AutoPostBack="True"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvBookname" runat="server"
ControlToValidate="txtBookname"
ErrorMessage="Required input." ToolTip="Validation of Book Name field"
Width="1px">*</asp:RequiredFieldValidator>
<aspropDownList 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>
</aspropDownList>
<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>
<br />
<br />
<asp:ValidationSummary ID="vsMainPage" runat="server" Height="197px"
Width="468px" />
<asp:Button ID="Button1" runat="server" Text="Button" /></div>
</form>
</body>
</html>