Click here to Skip to main content
15,899,754 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
when I click the image button the cursor goes back to the first textbox.
in the behind code I have this?
C#
protected void imgRegister_Click(object sender, ImageClickEventArgs e)
       {

           System.Threading.Thread.Sleep(2000);

           if (Convert.ToInt32(search_age_start.SelectedValue) > Convert.ToInt32(search_age_end.SelectedValue))
           {
               lblMessage.Text = "Minimum age should be less than or equal to maximum age.";
               search_age_start.Focus();
               return;
           }

           if (ctrlCaptcha != null)
           {
               if (!ctrlCaptcha.ValidateCaptcha())
               {
                   lblMessage.Text = "Invalid Access Code, try again.";
                   return;
               }
           }

           if (Page.IsValid)
           {


XML
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:ImageButton ID="imgRegister" runat="server" onmouseover="~/Images/Buttons/rollover2.jpg"
                onmouseout="~/Images/Buttons/rollover1.jpg" Font-Bold="True" ImageUrl="~/Images/Buttons/rollover1.jpg"
                OnClick="imgRegister_Click" />
        </ContentTemplate>
    </asp:UpdatePanel>
    <asp:UpdateProgress ID="UpdateProgress1" runat="server">
        <ProgressTemplate>
            <img alt="" runat="server" src="../../Images/ajax-loader.gif" />
            Loading ...
        </ProgressTemplate>
    </asp:UpdateProgress>
Posted
Comments
Yusuf 10-Feb-11 16:28pm    
Don't put your comments/question in the 'Answer' box. There is 'Add Comment' to the side. Did you see that?

1 solution

Hi

One thing to look at your page is, there is no CausesValidation="true" for the image button.

Here is an example

On the aspx page

XML
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:ScriptManager ID="script1" runat="server">
    </asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server" Visible="true">
        <ContentTemplate>
            <asp:Label  runat="server" Text="This is a required field" ></asp:Label>
            <asp:TextBox ID="text1" runat="server" ></asp:TextBox>
              <asp:Label ID="validation_result"  runat="server" ></asp:Label>
            <asp:CustomValidator ID="CustomValidator1" runat="server"
                 ControlToValidate="text1"  OnServerValidate ="validateTextBox"></asp:CustomValidator>
                <asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server"
                ControlToValidate="Text1"
                ErrorMessage="Password Should Not Be Empty"
                Display="Dynamic">
            </asp:RequiredFieldValidator>
            <asp:ImageButton ID="imgRegister" runat="server"
                 Font-Bold="True" ImageUrl="~/App_Data/arrow.png"
                OnClick="imgRegister_Click"  CausesValidation="true"/>
         </ContentTemplate>
    </asp:UpdatePanel>

</asp:Content>


In the code behind...

C#
protected void imgRegister_Click(object sender, ImageClickEventArgs e)
{
    if (Page.IsValid == true)
    {
        this.validation_result.Text = "Validated";
    }
    else
    {
        this.validation_result.Text = "Password should be 8 or more in characters";
    }
}
protected void validateTextBox(object sender, ServerValidateEventArgs e)
{
    if (Convert.ToString(e.Value).Length > 7)
    {
        e.IsValid = true;
    }
    else
    {
        e.IsValid = false;
    }
}
 
Share this answer
 
v2
Comments
Yusuf 10-Feb-11 16:27pm    
Message from OP:
still no working.
I tried this setting it to tru

I'm only a messenger: moved OP 'Answer' to here.
Albin Abel 10-Feb-11 16:50pm    
Hi,

If the controls in the current validation group (to which the image button belongs to) validated successfully then page.IsValid should result true. If you no need to validate all the controls then put the image button in a validation group for only the necessary controls.
macupryk 10-Feb-11 16:53pm    
does not work.
Albin Abel 11-Feb-11 1:01am    
try the example above?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900