Import this code in all my aspx pages

Discussion in 'C#' started by realbg, Nov 10, 2008.

  1. #1
    Hello,
    please can someone tell me how to import the following code(tnx*net) into all my pages?
    there are 2 files that I downloaded from their site, the first one is called banner.aspx and the code in it is:
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="xap.aspx.cs" Inherits="_Default" Debug="false" %>
    <%Banners(); %>
    The second file is called xap.aspx.cs and the code in it is:
    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Security.Cryptography;
    using System.Net;
    using System.Net.Sockets;

    public partial class _Default : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public void Banners()
    {
    string login = "your_login", ru, file, path_code, user_pref, path, domain, site = Request.ServerVariables["HTTP_HOST"], abuff = "";
    byte[] buffer=new byte[4096];
    MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();

    ru = Request.Path.ToString();
    ru = ru.Substring(0, ru.LastIndexOf("/")+1);
    if (ru.Length == 0)ru = "/";
    if (ru.Length <= 180)
    {
    file = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(ru));
    if (site.IndexOf("www.") == 0) site = site.Substring(4);
    path_code = GetHexString(md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(file)));
    path_code = path_code.ToLower();
    user_pref = login.Substring(0, 2);
    path = path_code.Substring(0, 1) + "/" + path_code.Substring(1, 2)+"/";
    domain = login + ".tnx.net";
    path = "/users/" + user_pref + "/" + login + "/" + site + "/" + path + file + ".txt";
    Socket sock1 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    sock1.ReceiveBufferSize = 4096;
    sock1.SendTimeout = 10000; //500 - recommended
    sock1.ReceiveTimeout = 10000; //500 - recommended
    try
    {
    sock1.Connect(Dns.GetHostAddresses(domain), 80);
    if (sock1.Connected)
    {
    sock1.Send(System.Text.Encoding.ASCII.GetBytes("GET " + path + " HTTP/1.0\r\nUser-Agent: XAPBannerDownloader v0.1a\r\n\r\n"));
    while (true)
    {
    int i = sock1.Receive(buffer);
    if (i <= 0) break;
    abuff += System.Text.Encoding.Default.GetString(buffer, 0, i);
    }
    }
    sock1.Shutdown(SocketShutdown.Both);
    int j = abuff.IndexOf("\r\n\r\n");
    int k = abuff.IndexOf("\r\n");
    if (abuff.Substring(0, k) == "HTTP/1.1 200 OK")
    {
    Response.Write(abuff.Substring(j + 4));
    }
    else
    {
    Response.Write("Error!<br>" + abuff.Substring(0, k));
    }
    }
    catch (Exception e)
    {
    Response.Write("Error: <b><font color=red>"+e.Message+"</font></b>");
    }
    }
    }
    private string GetHexString(byte[] b)
    {
    string tret="",str="";
    for (int i = 0; i < b.Length; ++i)
    {
    str = b.ToString("X");
    if (str.Length < 2)tret+="0";
    tret+=str;
    }
    return tret;
    }
    }


    Will be very greatful if you can help me!
     
    realbg, Nov 10, 2008 IP
  2. ranabra

    ranabra Peon

    Messages:
    125
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I read your post a few times. it is not clear to me what you are requesting!
    Please be more specific, invest a couple more minutes in your request.

    If I understood you (maybe), would using a userControl be helpful!?
     
    ranabra, Nov 11, 2008 IP
  3. web-bod

    web-bod Guest

    Messages:
    17
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    easiest way would be to use a master page.
     
    web-bod, Nov 15, 2008 IP
  4. theprototype

    theprototype Peon

    Messages:
    41
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Create a user control.. paste the code in the user control and then add the control in all your pages...
    If you had used master pages, you could have just put the code in the master page
     
    theprototype, Nov 17, 2008 IP