0
votes

I have a aspx page containing a text box and an image button for search. I have used compare validator (to check for integer values) with the textbox. But the page reloads on the image button click even if I enter alphanumeric characters, along with showing the error message.

I tried using a regularexpressionvalidator instead but the problem persists.

But when i used a simple asp:button instead and binded it with textbox validation, its working fine (i.e. postback does not occur on incorrect value of textbox) and same is true with dropdownlist also (no postback occuring).

Please suggest.

Here's the code-

@peroija : Here's the code

<asp:ImageButton ID="btnSearch" runat="server" OnClick="btnSearch_Click" 
    ToolTip="Search" ValidationGroup="valControl" CausesValidation="true" />
<asp:TextBox ID="txtWidth" CssClass="TextFont" runat="server" 
    Width="233px" MaxLength="20" 
    ValidationGroup="valControl" CausesValidation="true"></asp:TextBox>
<asp:CompareValidator runat="server" ID="cmpValWidth" 
    ErrorMessage="Please enter integer values" ControlToValidate="txtWidth" Display="Dynamic" 
    Operator="DataTypeCheck" ValidationGroup="valControl"Type="Integer"/>
2
can you post the code for your textbox, image button, and compare validator? it sounds like you may have assigned one of them but not the other a validation group - peroija
don't forget to accept an answer if it helped you... - Nikola Bogdanović

2 Answers

0
votes

Sounds to me like you need to write

if(!isPostBack)
{

"your code"

}

in the code behind. To prevent the code from being run if the page is not viewed for the first time

0
votes

Remove this from your textbox, you only need it on the validator and the button:

ValidationGroup="valControl" CausesValidation="true"

If javascript is disabled, then there will be no client side validation, so always check the validity on the server side also:

if(Page.IsValid)
{
    "your btnSearch_Click code"
}