Hello all. I'm having a bit of a problem when running a DataReader when submitting the form through both the submit button and allowing the user to use the Enter key (i.e. textchanged method).
Below is my code. Everything works great until I change the txtCustomerID text field to include the OnTextChanged="txtCustomerID_TextChange". Then I start getting errors like: "Page.IsValid cannot be called before validation has taken place. It should be queried in the event handler for a control with CausesValidation=True or after a call to Page.Validate."
Why would I receive this error message after enabling the users to submit the form via the Enter/Return key?
-Ovatvvon :-Q
Below is my code. Everything works great until I change the txtCustomerID text field to include the OnTextChanged="txtCustomerID_TextChange". Then I start getting errors like: "Page.IsValid cannot be called before validation has taken place. It should be queried in the event handler for a control with CausesValidation=True or after a call to Page.Validate."
Why would I receive this error message after enabling the users to submit the form via the Enter/Return key?
Code:
<%@ Page Language="VB" %>
<script runat="server">
Function GetBooks(ByVal bookID As Integer) As System.Data.SqlClient.SqlDataReader
Dim connectionString As String = "server='localhost'; user id='sa'; password='password'; Database='DB_Sample'"
Dim sqlConnection As System.Data.SqlClient.SqlConnection = New System.Data.SqlClient.SqlConnection(connectionString)
Dim queryString As String = "SELECT [Books].* FROM [Books] WHERE ([Books].[BookID] = @BookID)"
Dim sqlCommand As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(queryString, sqlConnection)
sqlCommand.Parameters.Add("@BookID", System.Data.SqlDbType.Int).Value = bookID
sqlConnection.Open
Dim dataReader As System.Data.SqlClient.SqlDataReader = sqlCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
Return dataReader
End Function
Sub RetrieveBook()
If Page.IsValid Then
dgBooks.DataSource = GetBooks(txtCustomerID.Text)
dgBooks.DataBind()
dgBooks.Visible = True
Else
dgBooks.Visible = False
End If
End Sub
Sub btnSubmit_Click(sender As Object, e As EventArgs)
RetrieveBook()
End Sub
Sub txtCustomerID_TextChanged(sender As Object, e As EventArgs)
RetrieveBook()
End Sub
</script>
<html>
<head>
</head>
<body>
<form runat="server">
<p align="center">
Desired Customer ID<br />
<asp:TextBox id="txtCustomerID" runat="server" OnTextChanged="txtCustomerID_TextChanged" Columns="4"></asp:TextBox>
</p>
<p align="center">
<asp:RequiredFieldValidator id="reqCustomerID" runat="server" ErrorMessage="You must enter a customer ID in the textbox." Display="Dynamic" ControlToValidate="txtCustomerID"></asp:RequiredFieldValidator>
</p>
<p align="center">
<asp:Button id="btnSubmit" onclick="btnSubmit_Click" runat="server" Text="Retrieve Book"></asp:Button>
</p>
<p align="center">
<asp:DataGrid id="dgBooks" runat="server" Width="485px" BorderWidth="1px" BorderColor="#DEBA84" BorderStyle="None" BackColor="#DEBA84" CellPadding="3" CellSpacing="2" AutoGenerateColumns="False">
<FooterStyle forecolor="#8C4510" backcolor="#F7DFB5"></FooterStyle>
<SelectedItemStyle font-bold="True" forecolor="White" backcolor="#738A9C"></SelectedItemStyle>
<ItemStyle forecolor="#8C4510" backcolor="#FFF7E7"></ItemStyle>
<HeaderStyle font-bold="True" forecolor="White" backcolor="#A55129"></HeaderStyle>
<Columns>
<asp:BoundColumn DataField="Title" HeaderText="Title"></asp:BoundColumn>
<asp:BoundColumn DataField="Author" HeaderText="Author"></asp:BoundColumn>
<asp:BoundColumn DataField="Price" HeaderText="Price" DataFormatString="{0:c}"></asp:BoundColumn>
<asp:HyperLinkColumn Text="More Information" DataNavigateUrlField="URL" HeaderText="More Information"></asp:HyperLinkColumn>
</Columns>
<PagerStyle horizontalalign="Center" forecolor="#8C4510" mode="NumericPages"></PagerStyle>
</asp:DataGrid>
</p>
</form>
</body>
</html>
-Ovatvvon :-Q