Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3323094
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:15:40+00:00 2026-05-17T23:15:40+00:00

I have an ASP .NET page with both asp .net validators and some javascript

  • 0

I have an ASP .NET page with both asp .net validators and some javascript checking too.
I am advancing into the button code behind:

protected void Button2_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            /// ...

Even when the validation fails!
The Page.IsValid check solves it, but it also resets all the JavaScript variables… May there be a way not to advance into the button code?
I have an ajax update panel and some image buttons on the page too… Anything I may look out for?
Thanks in advance!

Here is my aspx:

<%@ Page Language="C#" AutoEventWireup="true" 
CodeBehind="WebForm2.aspx.cs" 
Inherits="WebForm2" %>
<!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 style="width: 500px;">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>

        <script type="text/javascript">
            var nrOptions = 0;
            alert(nrOptions + " - variable initialized");
            function IncrementQuantity() {
                nrOptions++;
                alert(nrOptions);
            }
            function DecrementQuantity() {
                nrOptions--;
                alert(nrOptions);
            }
            function MoreThanTwo() {
                alert(nrOptions);
                if (nrOptions > 1) {
                    alert('okay - you have: ' + nrOptions);
                    return true;
                }
                else {
                    alert('error - must have at least two options, you only have: ' + nrOptions);
                    return false;
                }
            }            
        </script>

        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">
                    <HeaderTemplate>
                        Fill in with two or more options:<br />
                        <table border="1" width="100%">
                    </HeaderTemplate>
                    <ItemTemplate>
                        <tr>
                            <td valign="middle">
                            </td>
                            <td valign="middle">
                                Description:
                                <asp:TextBox ID="TBox1" runat="server" Width="120px" Text='<%# Eval("Desc")%>'></asp:TextBox>
                                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TBox1"
                                    ValidationGroup="InsertVal" ErrorMessage="*"></asp:RequiredFieldValidator>
                                Points:
                                <asp:TextBox ID="TBox2" runat="server" Width="20px" Text='<%# Eval("Pont")%>'></asp:TextBox>
                                <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="TBox2"
                                    ValidationGroup="InsertVal" ErrorMessage="*"></asp:RequiredFieldValidator>
                                <asp:Button ID="ImageButton1" runat="server" Text="x" CommandName="DeleteRow" OnClientClick="DecrementQuantity();" />
                                <asp:RegularExpressionValidator ID="RegularExpressionValidator1" ControlToValidate="TBox2"
                                    ValidationExpression="\d+" ValidationGroup="InsertVal" runat="server"
                                    ErrorMessage="Number >= 0."></asp:RegularExpressionValidator>
                            </td>
                        </tr>
                    </ItemTemplate>
                    <FooterTemplate>
                        <tr>
                            <td colspan="2" align="right">
                                <asp:Button ID="lnkAddRow" runat="server" Text="Add option" OnClientClick="IncrementQuantity();"
                                    CommandName="AddRow" OnClick="lnkAddRow_Click" />
                            </td>
                        </tr>
                        </table>
                    </FooterTemplate>
                </asp:Repeater>
                <br />
                <p style="text-align: right;">
                    <asp:Button ID="Button2" runat="server" Text="Save" OnClick="Button2_Click" OnClientClick="return MoreThanTwo();"
                        ValidationGroup="InsertVal" />
                </p>
            </ContentTemplate>
        </asp:UpdatePanel>
    </div>
    </form>
</body>
</html>

And my code-behind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class WebForm2 : System.Web.UI.Page
{        

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            DTable = EmptyDTOptions();
            Repeater1.DataSource = DTable;
            Repeater1.DataBind();
        }
    }

    protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    {

        if (e.CommandName == "AddRow")
        {
            DTable.Rows.Add(0, "", "");
            Repeater1.DataSource = DTable;
            Repeater1.DataBind();
            return;
        }

        if (e.CommandName == "DeleteRow")
        {

            int idx = e.Item.ItemIndex;
            DTable.Rows.RemoveAt(idx);
            Repeater1.DataSource = DTable;
            Repeater1.DataBind();
            return;
        }

    }

    protected void lnkAddRow_Click(object sender, EventArgs e)
    {
        foreach (RepeaterItem item in Repeater1.Items)
        {
            int idx = item.ItemIndex;

            TextBox tb1 = (TextBox)item.FindControl("TBox1");
            TextBox tb2 = (TextBox)item.FindControl("TBox2");

            DTable.Rows[idx]["Desc"] = tb1.Text;
            DTable.Rows[idx]["Pont"] = tb2.Text;
        }
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {                
            // save!             
        }
    }

    private DataTable DTable
    {
        get
        {
            DataTable _dt = (DataTable)Session["DTable"];
            if (Object.Equals(_dt, null))
            {
                _dt = EmptyDTOptions();
                Session.Add("DTable", _dt);
            }
            return _dt;
        }
        set
        {
            Session["DTable"] = value;
        }
    }

    DataTable EmptyDTOptions()
    {
        DataTable dt = new DataTable();
        DataColumn dc;

        dc = new DataColumn("OptionNr", System.Type.GetType("System.Int32"));
        dt.Columns.Add(dc);
        dc = new DataColumn("Desc");
        dt.Columns.Add(dc);
        dc = new DataColumn("Pont");
        dt.Columns.Add(dc);

        return dt;
    }
}

I think it shows what I’m trying to avoid… Advancing to the button2_click with the failed validation (and resetting the javascript variable)… In order to get a list of two or more pairs of items, one of them being a number.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T23:15:41+00:00Added an answer on May 17, 2026 at 11:15 pm

    Rather than calling your function from the OnClientClick on the button, you can add a CustomValidator that calls your JavaScript function.

    <asp:CustomValidator ID="CheckMoreThanTwo" runat="server"                       
                         ValidationGroup="InsertVal"
                         ClientValidationFunction="MoreThanTwo" />
    

    Then, modify your JavaScript function as follows:

    function MoreThanTwo(source, arguments) {      
        alert(nrOptions);      
        if (nrOptions > 1) {      
            alert('okay - you have: ' + nrOptions);      
            arguments.IsValid=true;
        } else {      
            alert('error - must have at least two options, you only have: '
                      + nrOptions);      
            arguments.IsValid=false;
        }      
    }                  
    

    Doing this allows your custom JavaScript validation to work with all the validation code that ASP.NET uses. For instance, if you change to a validation summary, or change validation groups, this custom validator will continue to work the way the rest of the validators work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ASP.NET page with an asp:button that is not visible. I can't
I have an ASP.NET page which has a button it it. The button click
I have an asp.net page with a button. This button generates and inserts a
I have an ASP.NET page that contains two div's. Both have search fields and
I have a asp.net page, and would like to know whether script1 is already
I have an ASP.Net page that will be hosted on a couple different servers,
I have an ASP.NET page which has a script manager on it. <form id=form1
I have a ASP.Net page using ADO to query MS access database and as
I have a asp.net page where i query a list of url and the
I have an ASP.NET page with a gridview control on it with a CommandButton

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.