Results 1 to 2 of 2

Thread: how to use codes in c# classes?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2007
    Posts
    208

    Thumbs up how to use codes in c# classes?

    i am developing web application using C# and asp.net. i want to put some codes in classes so that i can call these when necessary. example let's i have to files, called register.aspx and register.cs
    now i want to store the register.cs codes in classes, so that i can call it when i use it? how to do that?
    please redirect me to good sources and give sample code of this.
    regards

  2. #2
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: how to use codes in c# classes?

    You need this line at the top of the web page

    asp.net Code:
    1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ColorChangeOnError.aspx.cs" Inherits="ColorChangeOnError" %>

    CodeFile points to the *.cs you wish to use with the page and Inherits points to the method, see the following example

    webpage
    asp.net Code:
    1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ColorChangeOnError.aspx.cs" Inherits="ColorChangeOnError" %>
    2.  
    3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    4.  
    5. <html xmlns="http://www.w3.org/1999/xhtml" >
    6. <head runat="server">
    7.     <title>Untitled Page</title>
    8. </head>
    9. <body>
    10.     <form id="form1" runat="server">
    11.     <div>
    12.        
    13.     <asp:Label
    14.         id="lblFirstName"
    15.         Text="First Name"
    16.         AssociatedControlID="txtFirstName"
    17.         Runat="server" />
    18.     <br />    
    19.     <asp:TextBox
    20.         id="txtFirstName"
    21.         Runat="server" />
    22.     <asp:RequiredFieldValidator
    23.         id="reqFirstName"
    24.         ControlToValidate="txtFirstName"
    25.         Text="(Required)"
    26.         EnableClientScript="false"
    27.         Runat="server" />  
    28.      
    29.     <br /><br />
    30.        
    31.     <asp:Label
    32.         id="lblLastName"
    33.         Text="Last Name"
    34.         AssociatedControlID="txtLastName"
    35.         Runat="server" />
    36.     <br />    
    37.     <asp:TextBox
    38.         id="txtLastname"
    39.         Runat="server" />
    40.     <asp:RequiredFieldValidator
    41.         id="reqLastName"
    42.         ControlToValidate="txtLastName"
    43.         Text="(Required)"
    44.         EnableClientScript="false"
    45.         Runat="server" />  
    46.      
    47.      <br /><br />
    48.      
    49.      <asp:Button
    50.         id="btnSubmit"
    51.         Text="Submit"
    52.         Runat="server" OnClick="btnSubmit_Click" />  
    53.  
    54.     </div>
    55.     </form>
    56. </body>
    57. </html>

    cs file

    C# Code:
    1. using System;
    2. using System.Data;
    3. using System.Configuration;
    4. using System.Collections;
    5. using System.Web;
    6. using System.Web.Security;
    7. using System.Web.UI;
    8. using System.Web.UI.WebControls;
    9. using System.Web.UI.WebControls.WebParts;
    10. using System.Web.UI.HtmlControls;
    11.  
    12. public partial class ColorChangeOnError : System.Web.UI.Page
    13. {
    14.     protected void Page_PreRender()
    15.     {
    16.         foreach (BaseValidator valControl in Page.Validators)
    17.         {
    18.             WebControl ctrl = (WebControl)Page.FindControl(valControl.ControlToValidate);
    19.             if (!valControl.IsValid)
    20.                 ctrl.BackColor = System.Drawing.Color.Yellow;
    21.             else
    22.                 ctrl.BackColor = System.Drawing.Color.White;
    23.         }
    24.     }
    25.     protected void btnSubmit_Click(object sender, EventArgs e)
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

Posting Permissions

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



Click Here to Expand Forum to Full Width