❶ asp.net c#语言怎么实现登陆、注册和数据库连接
登入的时候,在后台代码中提取用户输入的用户名和密码,然后和数据库中的数据进行比较(或者也可以在web.config中配置),如果正确,则转到相应页面,否则提示用户名或密码错误。
下面是利用web.config文件实现的,直接用数据库很简单(主要就是连接,下面有相应代码),代码如下:
web.config里:
<appSettings>
<add key="ConnectionString" value="server=(local);User id=sa;Pwd=sa;database=yourDB"/>
<add key="Admin.Username" value="username"/>
<add key="Admin.Password" value="password"/>
</appSettings>
页面后台代码里:
protected void btnLogin_Click(object sender, System.EventArgs e)
//点击登入的按钮
{
AdminDB admin = new AdminDB();//AdminDB为自己写的一个类,里面可以定义Login()方法
string username = admin.Login(txtUserName.Text,txtPassword.Text);
if (username.Length==0)
{
//登入失败相应操作
}
else
{
Response.Redirect("~/Edit.aspx");//登入成功操作
}
}
链接数据库:
SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["connectionString"]);
❷ ASP用户登录
index.htm:
<center><form action="login.asp">
请输入用户名:<input type="text" name="userID"><br>
请输入密码:<input type="password" name="password"><br>
<input type="submit" value="提交"><input type="reset" value="重写">
</form>
login.asp:
<%
dim connstr,conn,rs,sql
connstr="Driver={SQL Server};Server=(local);UID=sa;PWD=;DATABASE=compact"
set conn=server.CreateObject("adodb.connection")
conn.Open connstr
set rs=server.CreateObject("adodb.recordset")
on error resume next
dim i,founderr
founderr=false
dim userID,password
userID=ltrim(Request.Form("userID"))
password=ltrim(Request.Form("password"))
if userID="" or password="" then
founderr=true
%>
<script language="javascript">
alert("非法用户,或者密码不能为空")
history.back();
</script>
<% else
sql="select * from userlist where userID='" & userID & "'"
sql=sql+" and password='"& password & "'"
rs.Open sql,conn,1,1
if rs.RecordCount<>0 then
Session("userID")=userid
if rs.fields("Ifadmission")=true then
Session("Ifadmission")="1"
session("usertype")=rs.fields("usertype")
getfilestate
if userID<>"admin" then
Response.Redirect "../aspin.asp?usertype="
else
Response.Redirect "register1.asp"
end if
else
Session("Ifadmission")="0"
Session("userID")=""
%>
<script language="javascript">
alert("你的登录权限还未经过管理员批准!")
history.back();
</script>
<% end if
else
Session("Ifadmission")="0"
Session("userID")=""
%>
<script language="javascript">
alert("你的密码输入不正确")
history.back();
</script>
<%
end if
end if
%>
❸ 在Asp.net中用客户端控件做登录界面,点击提交按钮登录的后台代码怎么写呢请教各位!
可用Session来实现。我现在在做的一个物流系统就是这样来增加车辆的发车时间的。
首先在Page_Load里初始化 Session["str"] = "";
在该按钮的单击事件里写
Session["str"] = String.Concat(Session["str"].ToString(),"<tr><td><input type=\"text\" name=\"(这儿自己取个名,方便到时取里面的值。用Request["取的名字"].ToString 会自动取得以逗号格式隔开的textbox里的值。可自己试着做。)\" style=\"width: 156px\"></td></tr>");
(String.Concat(string1,string2)方法用来连接string1和string2两个字符串,即每次都在现有的一行后面新加一行。)
然后在页面的前台aspx文件里用<%=Session["str"]%>来显示这些行,注意将它放在你的<table>和</table>中间,就可以往该表格中加入行了。
要有命名规则也可以的。它们也可以都用一样的名字,比如说都为aa ,则name=\"aa\",这样可以用Request["aa"]将一批值取过来。或者你想命名为aa0,aa1,aa2……,可以定义一个Session["i"]=0,然后在前面那段代码的name=\"aa\"+Session["i"].ToString() Session["i"]=Session["i"]+1; 这样就可以了。