By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,913 Members | 1,305 Online
Bytes IT Community
Submit an Article
Got Smarts?
Share your bits of IT knowledge by writing an article on Bytes.

How to share code between .aspx pages

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

mypage.aspx:
Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="C#" AutoEventWireup="true" %>
  2. <%@ Import Namespace="System" %>
  3. <%@ Import Namespace="System.Web" %>
  4. <%@ Import Namespace="System.Data.OracleClient" %>
  5. <script runat="server" src="oralib.cs"></script>
  6. <html>
  7. <head><title>Reusable Code sample</title>
  8. </head>
  9. <body>
  10. <%
  11. OraLib ora = new OraLib(Request);
  12. if ( ora.HasOneRole("'IMAGE_UPLOAD'") ) {
  13.     %><H3>Welcome</H3>
  14. <%
  15. } else {
  16.     %><em><b><font color="red">UNAUTHORIZED</font></b></em>
  17. <%
  18. }
  19. ora.Close();
  20. %></body>
  21. </html>
  22.  
oralib.cs:
Expand|Select|Wrap|Line Numbers
  1. public class OraLib {
  2.     OracleConnection conn;
  3.     HttpRequest req;
  4.     bool isopen;
  5.  
  6.     public OraLib(HttpRequest hrq) {
  7.         req = hrq;
  8.         conn = new OracleConnection("user id=/;data source=myoradb;Integrated Security=true");
  9.         conn.Open();
  10.         isopen = true;
  11.     }
  12.  
  13.     public void Close() {
  14.         if ( isopen ) {
  15.             conn.Close();
  16.         }
  17.         isopen = false;
  18.     }
  19.  
  20.     public bool HasOneRole(String rol) {
  21.         String uid = req.ServerVariables["AUTH_USER"].ToUpper();
  22.         OracleCommand ocmd = conn.CreateCommand();
  23.         ocmd.CommandText = "select count(*) from dba_role_privs where grantee='"+uid+"' and granted_role in ("+rol+")";
  24.         OracleDataReader rows = ocmd.ExecuteReader();
  25.         int rval = 0;
  26.  
  27.         if ( rows.Read() ) {
  28.             rval = rows.GetInt32(0);
  29.         }
  30.         rows.Close();
  31.  
  32.         return (rval!=0);
  33.     }
  34. }
  35.  
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...
Sep 1 '13 #1
Share this Article
Share on Google+
1 Comment


P: 5
Use QUERYSTRING to send data from 1 page to another
Feb 7 '14 #2