开发者

Validation not working for ImageButton click

开发者 https://www.devze.com 2023-02-18 11:36 出处:网络
I have an image button for login in my system. so as the most common thing i m having two text boxes txt_username and txt_password.

I have an image button for login in my system. so as the most common thing i m having two text boxes txt_username and txt_password. i have put required field validator for both the text boxes. and set the validation group "a" and respective control to validate for both of them. the source code for my ImageButton is as follows:-

<td align="right" class="simple_text">&nbsp;</td>
<td align="left">
     <asp:ImageButton ID="ImageButton1" runat="server" CausesValidation="true"
          ImageUrl="~/images/signin_button.gif" onclick="ImageButton1_Click" 
          ValidationGroup="a" />
 </td>

now I am fed up as when I run my application and click the ImageButton it doesn't respond to the validation at all and redirects to the next page. Can anyone say why is it so? this is the first time wherein the validators are not working.

this is source code:-

<td align="left"><table width="480" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="178" align="right" class="simple_text">User Name :&nbsp;</td>
            <td width="302" align="left">

                &nbsp;<asp:TextBox ID="txt_username" runat="server" 
                    CssClass="text_box_username" Width="180px" AutoPostBack="True" 
                    ontextchanged="txt_username_TextChanged" ValidationGroup="a" 
                    CausesValidation="True"></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
                    ControlToValidate="txt_username" ErrorMessage="RequiredFieldValidator" 
                    ValidationGroup="a">**</asp:RequiredFieldValidator>
          </td>
          </tr>
          <tr>
            <td align="right" class="simple_text">&nbsp;</td>
            <td align="left">&nbsp;</td>
          </tr>
          <tr>
            <td align="right" class="simple_text">Password :&nbsp;</td>
            <td align="left">
                <asp:TextBox ID="txt_password" runat="server" CssClass="text_box_password" 
                    TextMode="Password" Width="180px" ValidationGroup="a" 
                    CausesValidation="True"></asp:TextBox>
                                        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 
                    ControlToValidate="txt_password" ErrorMessage="RequiredFieldValidator" 
     开发者_Go百科               ValidationGroup="a">**</asp:RequiredFieldValidator>
                                        </td>
          </tr>
          <tr>
            <td align="right" class="simple_text">&nbsp;</td>
            <td align="left">&nbsp;</td>
          </tr>
          <tr>
            <td align="right" class="simple_text">&nbsp;</td>
            <td align="left">
                <asp:ImageButton ID="ImageButton1" runat="server" CausesValidation="true"
                    ImageUrl="~/images/signin_button.gif" onclick="ImageButton1_Click" 
                    ValidationGroup="a" />
              </td>
          </tr>
        </table></td>
      </tr>
      <tr>
        <td align="left">&nbsp;</td>
      </tr>
      <tr>


hi just paste this code will work surely for you

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <td align="left"><table width="480" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="178" align="right" class="simple_text">User Name :&nbsp;</td>
            <td width="302" align="left">

                &nbsp;<asp:TextBox ID="txt_username" runat="server" 
                     Width="180px" AutoPostBack="True" 
                     ValidationGroup="a" 
                    CausesValidation="True" ontextchanged="txt_username_TextChanged"></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
                    ControlToValidate="txt_username" ErrorMessage="RequiredFieldValidator" 
                    ValidationGroup="a">**</asp:RequiredFieldValidator>
          </td>
          </tr>
          <tr>
            <td align="right" class="simple_text">&nbsp;</td>
            <td align="left">&nbsp;</td>
          </tr>
          <tr>
            <td align="right" class="simple_text">Password :&nbsp;</td>
            <td align="left">
                <asp:TextBox ID="txt_password" runat="server" 
                    TextMode="Password" Width="180px" ValidationGroup="a" 
                    CausesValidation="True"></asp:TextBox>
                                        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 
                    ControlToValidate="txt_password" ErrorMessage="RequiredFieldValidator" 
                    ValidationGroup="a">**</asp:RequiredFieldValidator>
                                        </td>
          </tr>
          <tr>
            <td align="right" class="simple_text">&nbsp;</td>
            <td align="left">&nbsp;</td>
          </tr>
          <tr>
            <td align="right" class="simple_text">&nbsp;</td>
            <td align="left">
                <asp:ImageButton ID="ImageButton1" runat="server" CausesValidation="true"
                    ImageUrl="~/images/signin_button.gif" onclick="ImageButton1_Click" 
                    ValidationGroup="a" />
              </td>
          </tr>
        </table></td>
      </tr>
      <tr>
        <td align="left">&nbsp;</td>
      </tr>
      <tr>

    </div>
    </form>
</body>
</html>


Hope you provided the validationGroup property properly.

Specifying Validation Groups


How about this:

<asp:ImageButton runat="server" CausesValidation="true" ImageUrl="..." />

CausesValidation="true" might force the validation to run.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号