⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 home.aspx

📁 ASP.NET Web Forms Techniques
💻 ASPX
📖 第 1 页 / 共 2 页
字号:
<%@ Page Language="C#" EnableViewState="False" EnableSessionState="True" SmartNavigation="False"%>
<%@Import Namespace="System.Data"%>
<%@Import Namespace="System.Data.SqlClient"%>
<%@Register TagPrefix="wcc" TagName="pagebanner" Src="ascx/pagebanner.ascx"%>
<%@Register TagPrefix="wcc" TagName="footerlinks" Src="ascx/footerlinks.ascx"%>
<%@Register TagPrefix="wcc" TagName="sendmail" Src="sendmail.ascx"%>

<script runat="server">
// -------------------------------------------------------------------
// page-level variable to hold database connection string
String sConnect;

// page-level variable to client browser type
String sStyleSize = "Standard";

// page-level variable for client-side script support
Boolean bCanScript = false;

// -------------------------------------------------------------------

void Page_Load() {

  // check UserID is in Session (put there by default.aspx)
  // if not there, browser does not support sessions
  // or did not load default.aspx first
  if ((String)Session["WccUserID"] == "" || (String)Session["WccUserID"] == null) {
    Response.Clear();
    Response.Redirect("no-sessions.htm");
    Response.End();
  }

  // get value of "script" from query string *or* Session to see
  // if browser supports client-side scripting and it is enabled
  if (Request.QueryString["script"] == "yes" || Session["WccCanScript"] == "True") {
    bCanScript = true;
    Session["WccCanScript"] = "True";    // store in Session
  }

  // get the browser type and version
  String sUAType = Request.Browser["Browser"];
  String sUAVer = Request.Browser["MajorVersion"];

  // specify appropriate stylesheet for <link> element
  // cannot server-side <link> control element as the
  // presence of an ID attribute stops some browsers
  // from loading the stylesheet! Netscape and Amaya
  // require larger font sizes to be specified
  if (sUAType == "Netscape" || sUAType == "Unknown" || sUAType == "") {
    sStyleSize = "Large";
  }
  Session["WccStyleSize"] = sStyleSize;   // store in Session

  // set properties of "pagebanner" user control
  // if client supports scripting and it is enabled
  if (bCanScript) {
    ctlBanner.UAType = sUAType;
    ctlBanner.UAVer = sUAVer;

    // check if user has seen animation in this session
    // if not, show and set session value to indicate this
    if (Session["WccSeenAnimation"] != "True") {
      ctlBanner.Animate = true;
      Session["WccSeenAnimation"] = "True";
    }
  }

  // get database connection string from web.config
  sConnect = ConfigurationSettings.AppSettings["XroxCarsConnectString"];

  // set DataSource for "news" list and data-bind it
  repNews.DataSource = GetNewsListDR();
  repNews.DataBind();

  // set DataSource for "car models" list and data-bind it
  dgrModels.DataSource = GetModelListDR();
  dgrModels.DataBind();
}

// -------------------------------------------------------------------

// subroutine executed when the user submits their email
// address to add to the mailing list
void AddToMailingList(object sender, EventArgs args) {
  if (Page.IsValid) {
    // update database and display message
    String sMsg;
    String sEmailAddr = txtMailList.Text.Trim();
    switch(UpdateMailingList(sEmailAddr)) {
      case 0:
        sMsg = "You are already a member of our mailing list.";
        break;
      case 1:
        sMsg = "Thank you for joining our mailing list.";
        // call routine to send email confirmation
        SendEmailConfirmation(sEmailAddr);
        break;
      default:
        sMsg = "* Sorry, you could not be added to the "
          + "mailing list.<br />Please "
          + "<a href='contact.aspx// title='contact us'>contact us</a>"
          + " for more information";
        break;
    }
    lblMessage.Text = "<p><b>" + sMsg + "</b></p>";
  }
}

// -------------------------------------------------------------------

// function to add user's email address to mailing list
int UpdateMailingList(String sEmailAddr) {
  SqlConnection sqlConnect = new SqlConnection(sConnect);
  try {
    String sProcName = "AddMailingListAddress";
    SqlCommand sqlComm = new SqlCommand(sProcName, sqlConnect);
    sqlComm.CommandType = CommandType.StoredProcedure;
    sqlComm.Parameters.Add("@Email", sEmailAddr);
    sqlComm.Parameters.Add("@Result", -1);
    sqlComm.Parameters["@Result"].Direction = ParameterDirection.Output;
    sqlConnect.Open();
    sqlComm.ExecuteNonQuery();
    return (int)sqlComm.Parameters["@Result"].Value;
  }
  catch (Exception e) {
    return -1;
  }
  finally {
    sqlConnect.Close();
  }
}

// -------------------------------------------------------------------

// routine to send email confirmation of addition to mailing list
void SendEmailConfirmation(String sEmailAddr) {
  String sMessage;
  sMessage = "Thank you for joining the Xrox Car Company Mailing List."
           + (char)13 + (char)10 + (char)13 + (char)10
           + "We will keep you informed about our exciting range of "
           + "cars, and any other new developments. You are registered "
           + "on the list as: " + (char)13 + (char)10 + sEmailAddr + (char)13 + (char)10 + (char)13 + (char)10
           + "To be removed from the list, just reply to this message "
           + "with the word REMOVE in the subject line."
           + (char)13 + (char)10 + (char)13 + (char)10 + "Visit us often at http://www.daveandal.net/";

  // use separate ASCX control to send the message
  ctlMail.FromAddress = ConfigurationSettings.AppSettings["XroxCarsWebmasterEmail"];
  ctlMail.ToAddress = sEmailAddr;
  ctlMail.MessageSubject = "Mailing List Confirmation";
  ctlMail.MessageBody = sMessage;
  ctlMail.SendEmail();
}

// -------------------------------------------------------------------

// function to get details of car models for DataGrid control
SqlDataReader GetModelListDR() {
  try {
    SqlConnection sqlConnect = new SqlConnection(sConnect);
    String sProcName = "GetModelList";
    SqlCommand sqlComm = new SqlCommand(sProcName, sqlConnect);
    sqlComm.CommandType = CommandType.StoredProcedure;
    sqlConnect.Open();
    return sqlComm.ExecuteReader(CommandBehavior.CloseConnection);
  }
  catch (Exception e) {
    return null;
  }
}

// -------------------------------------------------------------------

// function to get list of new items for Repeater control
SqlDataReader GetNewsListDR() {
  try {
    SqlConnection sqlConnect = new SqlConnection(sConnect);
    String sProcName = "GetNewsList";
    SqlCommand sqlComm = new SqlCommand(sProcName, sqlConnect);
    sqlComm.CommandType = CommandType.StoredProcedure;
    sqlConnect.Open();
    return sqlComm.ExecuteReader(CommandBehavior.CloseConnection);
  }
  catch (Exception e) {
    return null;
  }
}

// -------------------------------------------------------------------

// function to set NavigateUrl of DataGrid Hyperlink controls
// to manage new windows better if client supports scripting
void SetNavigateUrl(object sender, DataGridItemEventArgs args) {
  if (bCanScript && (args.Item.ItemType == ListItemType.Item
  || args.Item.ItemType == ListItemType.AlternatingItem)) {

    // it's a data row that is being bound so create appropriate
    // URLs for hyperlinks and remove Target attributes
    // use separate function to ensure session ID is munged into URL
    // in case the page is running in "cookieless sessions" mode
    System.Data.Common.DbDataRecord oRow = (System.Data.Common.DbDataRecord)args.Item.DataItem;
    String sCarID = oRow["CarID"].ToString();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -