SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Evangelist cms9651's Avatar
    Join Date
    Mar 2010
    Posts
    407
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Disable the RequiredFieldValidator where condition

    Disable the RequiredFieldValidator where condition

    Hello there.
    First of all I must say that I am a newbie when it comes to net (C#).

    Here is my problem.

    I need enable or disable the RequiredFieldValidator in my aspx page where condition.
    1. If the TextBox ID="StartDateFag" and TextBox ID="StartEndFag" are empty or full I need disable the RequiredFieldValidator with ID="StartDateHoe1";
    2. If the TextBox ID="StartDateHoe" and TextBox ID="StartEndHoe" are empty or full I need disable the RequiredFieldValidator with ID="StartEndFag1";

    I tried this code but in the all condition I have RequiredFieldValidator enables.

    Can you help me?

    If you have link for similar task, please give it me.
    Can you explain any one or any sample code related this.

    Your help would be very appreciated.
    Thanks in advance for your time and hints.

    Cheers.

    Code:
                <asp:TextBox ID="StartDateFag" runat="server" Width="70">
                </asp:TextBox>
                <asp:TextBox ID="StartEndFag" runat="server" Width="70">
                </asp:TextBox>
                <asp:RequiredFieldValidator ID="StartEndFag1" runat="server" Enabled="true" ControlToValidate="StartEndFag"
                    ErrorMessage=Error 1"></asp:RequiredFieldValidator>
    
    
                <asp:TextBox ID="StartDateHoe" runat="server" Width="70">
                </asp:TextBox>
                <asp:TextBox ID="StartEndHoe" runat="server" Width="70">
                </asp:TextBox>
                <asp:RequiredFieldValidator ID="StartEndHoe1" runat="server" Enabled="true" ControlToValidate="StartEndHoe"
                    ErrorMessage=Error 2"></asp:RequiredFieldValidator>
    
            if (s == "button1")
            {
                if (!string.IsNullOrEmpty(StartDateFag.Text) && !string.IsNullOrEmpty(StartEndFag.Text))
                {
                    StartDateHoe1.Enabled = false;
                }
                else if (string.IsNullOrEmpty(StartDateFag.Text) == true && string.IsNullOrEmpty(StartDateFag.Text) == true)
                {
                    StartDateHoe1.Enabled = false;
                }
            }
    
    
    
            if (s == "button2")
            {
                if (!string.IsNullOrEmpty(StartDateHoe.Text) && !string.IsNullOrEmpty(StartEndHoe.Text))
                {
                    StartEndFag1.Enabled = true;
                }
                else if (string.IsNullOrEmpty(StartDateHoe.Text) == true && string.IsNullOrEmpty(StartEndHoe.Text) == true)
                {
                    StartEndFag1.Enabled = false;
                }
            }
    Thanks you very much for your help
    I'm really happy for your quickly answer.
    Good bye

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,162
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    My gut says to use a CustomValidator instead of a RequiredFieldValidator so you can put some code behind it

    Example of usage:
    Code:
    <asp:CustomValidator ID="StartDateHoeValidator" runat="server" ErrorMessage="This field is required."
    OnServerValidate="StartDateHoeValidator_Validate" ></asp:CustomValidator>
    Code behind
    Code:
    protected void StartDateHoeValidator_Validate(object sender, ServerValidateEventArgs args)
    {
    	if (StartDateFag.Value != "" && StartEndFag.Value != "") {
    		if (StartDateHoe1.Text == "") {
    			args.IsValid = false;
    		}
    	}
    }

  3. #3
    SitePoint Evangelist cms9651's Avatar
    Join Date
    Mar 2010
    Posts
    407
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi cpradio, thank you for help and tips.

    I tried this code, but:
    1. If the field StartDate is null I don't have alert;
    2. If the field EndDate is null I don't have alert;
    3. If the fields StartDate and EndDate are full I have alert: StartDate is required

    Can you help me?

    Code-behind
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System;
    using System.Text;
    
    public partial class SqlServer_button : System.Web.UI.Page
    {
    
        protected void StartDateValidator_Validate(object sender, ServerValidateEventArgs args)
        {
            if (StartDate.Text != "" && EndDate.Text != "")
            {
                if (StartDate1.Text == "")
                {
                    args.IsValid = false;
                }
            }
        }
    
        protected void StartDate1Validator_Validate(object sender, ServerValidateEventArgs args)
        {
            if (StartDate1.Text != "" && EndDate1.Text != "")
            {
                if (StartDate.Text == "")
                {
                    args.IsValid = false;
                }
            }
        }
        
        protected void Page_Load(object sender, EventArgs e)
        {
            
    
        }
        
        protected void Button1_Click(Object sender, EventArgs e)
        {
    
        }
    
        protected void Button2_Click(Object sender, EventArgs e)
        {
    
        }
    }
    Page.aspx
    Code:
    <!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 id="Head1" runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:TextBox ID="StartDate" runat="server" Width="70"></asp:TextBox>
            <asp:TextBox ID="EndDate" runat="server" Width="70"></asp:TextBox>
            <asp:CustomValidator ID="StartDateValidator" runat="server" ErrorMessage="StartDate is required."
                OnServerValidate="StartDateValidator_Validate"></asp:CustomValidator>
            <asp:ImageButton ID="Button1" ImageUrl="images/cerca_icon.gif" runat="server" OnClick="Button1_Click"
                OnClientClick="if(!confirm('Are you sure?')) return false;" />
            <br />
            <br />
            <asp:TextBox ID="StartDate1" runat="server" Width="70"></asp:TextBox>
            <asp:TextBox ID="EndDate1" runat="server" Width="70"></asp:TextBox>
            <asp:CustomValidator ID="StartDate1Validator" runat="server" ErrorMessage="StartDate1 is required."
                OnServerValidate="StartDate1Validator_Validate"></asp:CustomValidator>
            <asp:ImageButton ID="Button2" ImageUrl="images/cerca_icon.gif" runat="server" OnClick="Button2_Click"
                OnClientClick="if(!confirm('Are you sure?')) return false;" />
        </div>
        </form>
    </body>
    </html>
    Thanks you very much for your help
    I'm really happy for your quickly answer.
    Good bye

  4. #4
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,162
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Okay, my initial understanding to your problem was different than what seems to be outlined in your ASPX page.

    Okay, so the first thing you will want to do is assign a validationgroup on your first set of textboxes, customvalidator, and the imagebutton like so:
    Code:
            <asp:TextBox ID="StartDate" runat="server" Width="70" ValidationGroup="Group1"></asp:TextBox>
            <asp:TextBox ID="EndDate" runat="server" Width="70" ValidationGroup="Group1"></asp:TextBox>
            <asp:CustomValidator ID="StartDateValidator" runat="server" ErrorMessage="StartDate is required."
                OnServerValidate="StartDateValidator_Validate" ValidationGroup="Group1"></asp:CustomValidator>
            <asp:ImageButton ID="Button1" ImageUrl="images/cerca_icon.gif" runat="server" ValidationGroup="Group1" OnClick="Button1_Click"
                OnClientClick="if(!confirm('Are you sure?')) return false;" />
    Then update your validator code for this group
    Code:
        protected void StartDateValidator_Validate(object sender, ServerValidateEventArgs args)
        {
            if (string.IsNullOrEmpty(StartDate.Text) || string.IsNullOrEmpty(EndDate.Text))
            {
                args.IsValid = false;
            }
        }
    Then you will do the same thing for your second set of textboxes, customvalidator, and imagebutton except give it a ValidationGroup of Group2

  5. #5
    SitePoint Evangelist cms9651's Avatar
    Join Date
    Mar 2010
    Posts
    407
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks you very much for your help

    I'm really happy for your quickly answer, now your code working.
    Good bye
    Thanks you very much for your help
    I'm really happy for your quickly answer.
    Good bye

  6. #6
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,162
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    From my quick glance, you forgot the ValidationGroup attribute on the second ImageButton, it should be ValidationGroup="Group2"

  7. #7
    SitePoint Evangelist cms9651's Avatar
    Join Date
    Mar 2010
    Posts
    407
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    It's true!

    Thanks you very much for your help.
    I'm really happy for your quickly answer, now your code working.

    Good bye my friend!
    Thanks you very much for your help
    I'm really happy for your quickly answer.
    Good bye


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •