当前位置:编程学习 > C#/ASP.NET >>

ASP.NET中下载代码怎么写

追问:你好,我是要将超链接的内容从数据库中读出来,然后单击即可下载,不知道您知道怎么写吗
答案:Asp.Net登陆代码
DAL层

using System;
using System.Collections.Generic;
using System.Text;
using MODEL;
using System.Data.SqlClient;

namespace DAL
{
    public static class LoginService
    {
        public static UserMessage GetUserByLoginId(string LoginName)
        {
            string sql = "select * from My_UserMessage where uname=@uname";
            using (SqlDataReader reader = DBHelper.GetReader(sql, new SqlParameter("@uname", LoginName)))
            {
                if (reader.Read())
                {
                    UserMessage user = new UserMessage();
                    user.Id = (int)reader["Id"];
                    user.Uname = (string)reader["uname"];
                    user.Password = (string)reader["password"];
                    return user;
                }
                else
                {
                    reader.Close();
                    return null;
                }
            }
        }
    }
}

BLL层

using System;
using System.Collections.Generic;
using System.Text;
using DAL;
using MODEL;
namespace BLL
{
    public static class LoginManager
    {
        /// <summary>
        /// 登陆
        /// </summary>
        /// <param name="loginId"></param>
        public static bool Login(string loginName, string Pwd, out UserMessage validUser)
        {
            UserMessage user = LoginService.GetUserByLoginId(loginName);
            if (user == null)
            {
                validUser = null;
                return false;
            }
            if (user.Password == Pwd)
            {
                validUser = user;
                return true;
            }
            else
            {
                validUser = null;//密码错误
                return false;
            }
        }
    }
}

MODEL层

using System;
using System.Collections.Generic;
using System.Text;

namespace MODEL
{
    public class UserMessage
    {
        private int id;

        public int Id
        {
            get { return id; }
            set { id = value; }
        }
        private string uname;

        public string Uname
        {
            get { return uname; }
            set { uname = value; }
        }


        private string password;

        public string Password
        {
            get { return password; }
            set { password = value; }
        }
    }
}


Web层

Login.aspx代码

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Login" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
    <script>
    function checkSubmit()
    {
         if(document.admininfo.TextBox1.value==""){
          alert("用户名不能为空!");
          document.admininfo.TextBox1.focus();
          return false;
         }
         if(document.admininfo.TextBox2.value==""){
          alert("密码不能为空!");
          document.admininfo.TextBox2.focus();
          return false;
         }
     }
    </script>
</head>
<body>
    <form id="admininfo" runat="server" onSubmit="return checkSubmit();">
    <div>
    用户名:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />
    密    码:
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="登录" /></div>
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </form>
</body>
</html>

Login.aspx.cs代码

using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 BLL;
using MODEL;

public partial class Login : System.Web.UI.Page
{
    UserMessage user;
    protected void Page_Load(object sender, EventArgs e)
    {}
    //登录事件
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (LoginManager.Login(this.TextBox1.Text, TextBox2.Text, out user))
        {
            Session["user"] = user;
            Response.Redirect("Default.aspx");
        }
        else
        {
            if (!LoginManager.Login(this.TextBox1.Text, TextBox2.Text, out user))
            {
                ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('用户名或密码错误!');</script>");
            }
            else
            {
                ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('用户名或密码错误!');</script>");
            }
        }
    }
}
其他:使用JS实现单击连接保存图片
2种形式都可以

第一种:

<script> 
function   SaveAs5(imgURL) 
...{ 
  var   oPop   =   window.open(imgURL,"","width=1,   height=1,   top=5000,   left=5000");   
  for(;   oPop.document.readyState   !=   "complete";   )   
  ...{ 
    if   (oPop.document.readyState   ==   "complete")break; 
  } 
  oPop.document.execCommand("SaveAs"); 
  oPop.close();   
} 

</script> 
<img   src="t_screenshot_17616.jpg"   id="DemoImg"   border="0"   onclick="SaveAs5(this.src)"> 

第二种:

<script> 
function   SaveAs5(imgURL) 
...{ 
  var   oPop   =   window.open(imgURL,"","width=1,   height=1,   top=5000,   left=5000");   
  for(;   oPop.document.readyState   !=   "complete";   )   
  ...{ 
    if   (oPop.document.readyState   ==   "complete")break; 
  } 
  oPop.document.execCommand("SaveAs"); 
  oPop.close();   
} 

</script> 
<img   src="../t_screenshot_17616.jpg" style="border:0px;point:hand;" onclick="SaveAs5(this.src)" title="点击保存"> 你可以去研究一下asp.net自己内置的身份验证模块 你要先理解什么是登录 楼主应该是初学者,给你个简单的登录
Login.cs
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data .SqlClient ;

public partial class _Default : System.Web.UI.Page 
{
    SqlConnection connect=new SqlConnection (@"Data Source=DEEPINXP\SQLEXPRESS;Initial Catalog=Address List;Integrated Security=True");//连接数据库
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {

        ////从主界面到注册界面
        //Response.Redirect("Default2.aspx");
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        try
        {


            scomm = new SqlCommand("select*from OwnerUser where username=@un and userpassw=@up", sconn);
            //查询login中的namess和Password
            scomm.Parameters.Add("@un", SqlDbType.NVarChar, 20).Value = TextBox1.Text;
            scomm.Parameters.Add("@up", SqlDbType.NVarChar, 20).Value = TextBox2.Text;
            sr = scomm.ExecuteReader();
            if (sr.Read())
            {


                //MessageBox.Show("成功登录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);


            }
            else
            {
                label7.Visible = true; x++;

            }

            sr.Close();


        }
        catch { }
    }
} <a href="文件地址">文件名</a> 

自动下载

上一个:asp 记录登陆次数
下一个:ASP中,我要查询或取出数据库的一个字段里的值,然后根据这个字段的值来判断用哪种方式显示。请问要怎么写

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,