Archives

Archives / 2004 / December
  • Simulating ValidationGroup (of ASP.NET 2.0) feature in ASP.NET 1.X

    I got an email asking about simulating ValidationGroup business case in ASP.NET 1.X.

     

    Was not sure how many ways are there to simulate that behavior in ASP.NET 1.X. But I have given one simple and easy solution to him. In case if any one of you needs it, here is the source code for it! (A Very simple and self explanatory example)

    **************
    <%@ Page language="c#" %>
    <
    script language="c#" runat="server">
    private
    void Page_Load(object sender, System.EventArgs e)
    {
       
    // If it's 2.0, we can use OnClientClick for client side funciton calls!!   

       Button1.Attributes.Add("onclick","enableGroupOne();");
       Button2.Attributes.Add("onclick","enableGroupTwo();");
    }

    private void B1(object sender, System.EventArgs e)
    {
       //two.IsValid=true;
       
    lblResult.Text = "Group ONE Textbox One value is: " + txtGrpOne.Text;
    }

    private void B2(object sender, System.EventArgs e)
    {
       //one.IsValid=true;
       
    lblResult.Text = "Group TWO Textbox One value is: " + txtGrpTwo.Text;
    }

    </script>
    <
    HTML>
    <HEAD>
       
    <title>ValidationGroup Simulation using ASP.NET 1.X from w3coder</title>
    </HEAD>
    <body>
       
    <form id="Form1" method="post" runat="server">
       
    <h3>ValidationGroup Simulation using ASP.NET 1.X</h3><br><br>

          Group One Textbox:
          
    <asp:TextBox id="txtGrpOne" runat="server"></asp:TextBox>    
          <
    asp:RequiredFieldValidator id="one" runat="server" ControlToValidate="txtGrpOne" ErrorMessage="Button ONE is Clicked, Hence this is required"></asp:RequiredFieldValidator> <br>
    <asp:Button id="Button1" runat="server" Text="Validate Group One" onClick="B1"></asp:Button> <br>
    <br><br>

       Group Two Textbox:
       
    <asp:TextBox id="txtGrpTwo" runat="server"></asp:TextBox>    
       <
    asp:RequiredFieldValidator id="two" runat="server" ControlToValidate="txtGrpTwo" ErrorMessage="Button TWO is Clicked, Hence this is required"></asp:RequiredFieldValidator> <br>
       
    <asp:Button id="Button2" runat="server" Text="Validate Group Two" onClick="B2"></asp:Button> <br>
    <br><br>
       
    <asp:Label id="lblResult" runat="server" ></asp:Label>
       
    <br>
    </form>

    <script language="javascript">
    // Disable all Validation cointrols using this!
    // As it has to run first did not made as function, but you can make it as function and call it on form load client side event..
    //function disableScriptfunction()
    // {
    for(i=0;i< Page_Validators.length;i++)
    {
    ValidatorEnable(Page_Validators[i], false);
    }
    // }
    // end of the Disable funciton

    // Making only Group ONE is Enable for Validation and Disable other validation controls.
    function enableGroupOne()
    {
       for(i=0;i< Page_Validators.length;i++)
       {
          ValidatorEnable(Page_Validators[i],
    false);
       }
     ValidatorEnable(one,
    true);
    }

    // Making only Group TWO is Enable for Validation and Disable other validation controls.
    function enableGroupTwo()
    {
    for(i=0;i< Page_Validators.length;i++)
      {
         ValidatorEnable(Page_Validators[i],
    false);
       
    }
    ValidatorEnable(two,
    true);
     }
    </script>
    </body>
    </
    HTML>

    **************

    The code is very self explanatory! We could do it in many ways, but this is one way, if you have a time, you could write your own generalized module for simulating this and to make more generalized for using in a big project. Take it away and use it as you like J

  • Take advantage of Pre-compile option in ASP.NET 2.0

    In ASP.NET 1.X, we need to manually batch compile each folder to avoid initially delay of the page load!!

     

    But in ASP.NET 2.0, the compilation process has been automated using special handler called “Precompile.axd” file.

     

    For example if the website name is “ASPNETWeb”, then we can pre-compile it simply by typing http://<servername>/ASPNETWeb/Precompile.axd on the web browser!

     

    That’s it, by doing that step the site is going to be compiled for us! From next request onwards pages will come without any initial delay J
    Haa... Life is good with 2.0!!

     

    Also please be sure to check aspnet_compiler.exe compile options! It has some really cool new features!