How to share code between .aspx pages

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • da808wiz
    New Member
    • Sep 2013
    • 1

    How to share code between .aspx pages

    I don't know if there is a better way, but here is how I do it:

    mypage.aspx:
    Code:
    <%@ Page Language="C#" AutoEventWireup="true" %>
    <%@ Import Namespace="System" %>
    <%@ Import Namespace="System.Web" %>
    <%@ Import Namespace="System.Data.OracleClient" %>
    <script runat="server" src="oralib.cs"></script>
    <html>
    <head><title>Reusable Code sample</title>
    </head>
    <body>
    <%
    OraLib ora = new OraLib(Request);
    if ( ora.HasOneRole("'IMAGE_UPLOAD'") ) {
    	%><H3>Welcome</H3>
    <%
    } else {
    	%><em><b><font color="red">UNAUTHORIZED</font></b></em>
    <%
    }
    ora.Close();
    %></body>
    </html>
    oralib.cs:
    Code:
    public class OraLib {
    	OracleConnection conn;
    	HttpRequest req;
    	bool isopen;
    	
    	public OraLib(HttpRequest hrq) {
    		req = hrq;
    		conn = new OracleConnection("user id=/;data source=myoradb;Integrated Security=true");
    		conn.Open();
    		isopen = true;
    	}
    	
    	public void Close() {
    		if ( isopen ) {
    			conn.Close();
    		}
    		isopen = false;
    	}
    	
    	public bool HasOneRole(String rol) {
    		String uid = req.ServerVariables["AUTH_USER"].ToUpper();
    		OracleCommand ocmd = conn.CreateCommand();
    		ocmd.CommandText = "select count(*) from dba_role_privs where grantee='"+uid+"' and granted_role in ("+rol+")";
    		OracleDataReader rows = ocmd.ExecuteReader();
    		int rval = 0;
    
    		if ( rows.Read() ) {
    			rval = rows.GetInt32(0);
    		}
    		rows.Close();
    
    		return (rval!=0);
    	}
    }
    Note:
    on the .aspx page, you need to @Import the namespaces which the .cs files you include use. The example above, oralib.cs uses the OracleClient namespace so the .aspx imports those BEFORE the script src tag brings in oralib.cs.

    I hope this helps someone, or if someone else has a better idea, feel free to comment...
  • sagarrupani
    New Member
    • Feb 2014
    • 5

    #2
    Use QUERYSTRING to send data from 1 page to another

    Comment

    Working...