★文本赋值
Label1.Text = "字符串";
-----------------------------------------------------------------------------------
★定义变量
string username1 = "";
-----------------------------------------------------------------------------------
★跳转页面
this.Response.Redirect("main.aspx");
-----------------------------------------------------------------------------------
★弹出javascript对话框
this.Response.Write("<script languge='javascript'>alert('登录成功!')</script>"); 
-----------------------------------------------------------------------------------
★数据保存到 Session 中
this.Session["password1"] = password;
-----------------------------------------------------------------------------------
★捕获结果
try
{
//成功后执行
}
catch
{
//失败后执行
}
-----------------------------------------------------------------------------------
★连接ACCESS数据库 并查询输出
using System.Data;
using System.Data.OleDb;

OleDbConnection myConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath("data/db.mdb"));
OleDbCommand myCommand = new OleDbCommand("select * from user1 where username='" + username.Text + "'and password='" + password.Text + "'", myConnection);
myConnection.Open();
OleDbDataReader yanzheng = myCommand.ExecuteReader();
//----------------------
if (yanzheng.Read())
{
//如果查询成功则执行
username.Text = yanzheng["username"].ToString(); //从数据库中取出数据并赋值给变量
}
else
{
//如果查询不成功则执行
}
yanzheng.Close();
//----------------------
myConnection.Close();
-----------------------------------------------------------------------------------
★连接MS-SQL SERVER数据库
using System.Data;
using System.Data.SqlClient;

SqlConnection myConnection = new SqlConnection("server=127.0.0.1;database=test;uid=sa;pwd=sa");
SqlCommand myCommand = new SqlCommand("select * from user1 where username='" + username.Text + "'and password='" + password.Text + "'", myConnection);
myConnection.Open();
IDataReader yanzheng = myCommand.ExecuteReader();
//----------------------
//
//----------------------
myConnection.Close();
------------------------------------------------------------------------------------
★SQL语句
查询 select * from 表名 where username='" + username.Text + "'and password='" + password.Text + "'
插入 insert into 表名(字段1,字段2) values("+TextBox1.Text+",'"+TextBox2.Text+"')
更新 SqlCommand myCommand = new SqlCommand("UPDATE user1 SET username='" + username.Text + "',[password]='" + password.Text + "'", myConnection);
删除 delete * from 表名 where username='" + username.Text + "'and password='" + password.Text + "'
-----------------------------------------------------------------------------------
★提交变量到其他页面
(窗体载入)提交
HyperLink1.NavigateUrl = "default.aspx?TextBox1=" + System.Web.HttpUtility.UrlEncode(TextBox1.Text); 

(窗体载入)显示
if (!Page.IsPostBack)
{
Label2.Text = Request.Params["TextBox1"];
}
-----------------------------------------------------------------------------------
★GET方法从文本框空间中获得数据并提交到其他页面
if (TextBox2.Text != "")
{
Response.Redirect("default.aspx?TextBox2=" + System.Web.HttpUtility.UrlEncode(TextBox2.Text));
}
else
{
Label3.Text = "您好!请在文本框中输入您的姓名!";
}
-----------------------------------------------------------------------------------
★Response方法,获取GET方法提交的数据
Label3.Text = Request.Params["TextBox2"];
-----------------------------------------------------------------------------------
★单击窗体任意位置清空Label1内容。
<script language="javascript" type="text/javascript">
function nosource(e)
{
Label1.innerText="";
}
document.onmousedown=nosource;
</script>
-----------------------------------------------------------------------------------
★类的调用
类文件 .cs
static public string conn = "server=127.0.0.1;database=test;uid=sa;pwd=sa";
调用 .aspx
Class1.conn 类名称.变量名
示例:
string conn = Class1.conn;
SqlConnection myConnection = new SqlConnection(conn);

浙公网安备 33010602011771号