Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error using Page.IsValid from multiple web controls? 1

Status
Not open for further replies.

Ovatvvon

Programmer
Feb 1, 2001
1,514
US
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?


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
 
hmmm, well I'm confused then. From everything I've read up on it, if you hit the enter key, it will submit a form in ASP.NET, however, it just doesn't activate the button1_click() event which is usually where your code will reside to connect. The readings say that when it gets submitted, if you have a textchanged event, it'll sense that and activate the code in that routine. So I'm just way wrong on this then?

-Ovatvvon :-Q
 
if you hit the enter key, it will submit a form in ASP.NET
That's not always the case:


--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top