1、登陆测试
A:
namespace test
{
?///
?/// WebForm1 的摘要说明。
?///
?public class login : System.Web.UI.Page
?{
??protected System.Web.UI.WebControls.TextBox txtUserId;
??protected System.Web.UI.WebControls.TextBox txtPassword;
??protected System.Web.UI.WebControls.Button Button1;
??protected System.Web.UI.WebControls.Button Button2;
??string userid;
??string password;
?
??private void Page_Load(object sender, System.EventArgs e)
??{
???// 在此处放置用户代码以初始化页面
??}
??#region Web Form Designer generated code
??override protected void OnInit(EventArgs e)
??{
???//
???// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
???//
???InitializeComponent();
???base.OnInit(e);
??}
??
??///
??/// 设计器支持所需的方法 - 不要使用代码编辑器修改
??/// 此方法的内容。
??///
??private void InitializeComponent()
??{???
???this.Button1.Click += new System.EventHandler(this.Button1_Click);
???this.Load += new System.EventHandler(this.Page_Load);
??}
??#endregion
??private void Button1_Click(object sender, System.EventArgs e)
??{
???SqlConnection myConnection = new SqlConnection("data source = (local);user id = sa;password = qazwsx1;initial catalog = test;");
???SqlCommand myCommand = new SqlCommand("select userid,password from [User] where userid = @userid",myConnection);
???
???userid = txtUserId.Text;
???password = txtPassword.Text;
???myCommand.Parameters.Add("@userid",SqlDbType.NVarChar,50).Value = userid;
???myConnection.Open();
???SqlDataReader dr = myCommand.ExecuteReader();
//???bool exist = false;
//???while(dr.Read())
//???{
//????if (!exist)
//????{
//?????userid == dr[0].ToString();
//?????password == dr[1].ToString();
//?????exist = true;
//?????Response.Redirect("success.aspx");
//?????
//????}
//????break;
//???}
???if (!dr.Read())
????Response.Redirect("fail.aspx");
???if (dr[1].ToString()==password)
????Response.Redirect("success.aspx");
???else
????Response.Redirect("fail.aspx");
??}
?}
}
?
B:
namespace test
{
?///
?/// denglu 的摘要说明。
?///
?public class denglu : System.Web.UI.Page
?{
??protected System.Web.UI.WebControls.TextBox txtusername;
??protected System.Web.UI.WebControls.TextBox txtpassword;
??protected System.Web.UI.WebControls.Button Button1;
??protected string userid;
??protected string password;
?
??private void Page_Load(object sender, System.EventArgs e)
??{
???// 在此处放置用户代码以初始化页面
??}
??#region Web Form Designer generated code
??override protected void OnInit(EventArgs e)
??{
???//
???// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
???//
???InitializeComponent();
???base.OnInit(e);
??}
??
??///
??/// 设计器支持所需的方法 - 不要使用代码编辑器修改
??/// 此方法的内容。
??///
??private void InitializeComponent()
??{???
???this.Button1.Click += new System.EventHandler(this.Button1_Click);
???this.Load += new System.EventHandler(this.Page_Load);
??}
??#endregion
??private void Button1_Click(object sender, System.EventArgs e)
??{
???SqlConnection myConnection=new SqlConnection("data source=(local);user id=sa;password=qazwsx1;initial catalog=test;");
???SqlCommand myCommand=new SqlCommand("select * from [user] where userid=@userid",myConnection);
???userid=txtusername.Text;
???password=txtpassword.Text;
???myCommand.Parameters.Add("@userid",SqlDbType.NVarChar,50).Value=userid;
???myConnection.Open();
???SqlDataReader dr=myCommand.ExecuteReader();
???if(dr.Read())
????if (password==dr[1].ToString())
?????Response.Redirect("success.aspx");
?????else
??????Response.Redirect("fail.aspx");
???else
????Response.Redirect("fail.aspx");
??}
?}
}
?
2、新闻发布
添加:
namespace test.news
{
?///
?/// news_add 的摘要说明。
?///
?public class news_add : System.Web.UI.Page
?{
??protected System.Web.UI.WebControls.TextBox txtTitle;
??protected System.Web.UI.WebControls.TextBox txtContent;
??protected System.Web.UI.WebControls.Button btnOk;
??protected string title;
??protected string content;
?
??private void Page_Load(object sender, System.EventArgs e)
??{
???// 在此处放置用户代码以初始化页面
??}
??#region Web Form Designer generated code
??override protected void OnInit(EventArgs e)
??{
???//
???// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
???//
???InitializeComponent();
???base.OnInit(e);
??}
??
??///
??/// 设计器支持所需的方法 - 不要使用代码编辑器修改
??/// 此方法的内容。
??///
??private void InitializeComponent()
??{???
???this.btnOk.Click += new System.EventHandler(this.btnOk_Click);
???this.Load += new System.EventHandler(this.Page_Load);
??}
??#endregion
??private void btnOk_Click(object sender, System.EventArgs e)
??{
???SqlConnection myConnection=new SqlConnection("data source=(local);user id=sa;password=qazwsx1;initial catalog=test;");
???SqlCommand myCommand=new SqlCommand("insert into news (title,content) values (@title,@content)",myConnection);
???title=txtTitle.Text;
???content=txtContent.Text;
???myCommand.Parameters.Add("@title",SqlDbType.NVarChar,50).Value=title;
???myCommand.Parameters.Add("@content",SqlDbType.NVarChar,500).Value=content;
???myConnection.Open();
???myCommand.ExecuteNonQuery();
??}
?}
}
列表:
namespace test.news
{
?///
?/// news_list 的摘要说明。
?///
?public class news_list : System.Web.UI.Page
?{
??protected System.Web.UI.WebControls.DataGrid dgTitle;
?
??private void Page_Load(object sender, System.EventArgs e)
??{
???// 在此处放置用户代码以初始化页面
???SqlConnection myConnection=new SqlConnection("data source=(local);user id=sa;password=qazwsx1;initial catalog=test;");
???string selCom="select id,title from news";
???SqlDataAdapter myDataAdapter=new SqlDataAdapter(selCom,myConnection);
//???string id=Request.QueryString["id"];
//???myDataAdapter.SelectCommand.Parameters.Add(new SqlParameter("@id",SqlDbType.NVarChar,500)).Value=id;
???DataSet ds=new DataSet();
???myDataAdapter.Fill(ds,"Tblcontent");
???DataView dv;
???dv=ds.Tables["Tblcontent"].DefaultView;
???dgTitle.DataSource=dv;
???dgTitle.DataBind();
??}
??#region Web Form Designer generated code
??override protected void OnInit(EventArgs e)
??{
???//
???// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
???//
???InitializeComponent();
???base.OnInit(e);
??}
??
??///
??/// 设计器支持所需的方法 - 不要使用代码编辑器修改
??/// 此方法的内容。
??///
??private void InitializeComponent()
??{???
???this.dgTitle.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.dgTitle_PageIndexChanged);
???this.Load += new System.EventHandler(this.Page_Load);
??}
??#endregion
??private void dgTitle_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
??{
???dgTitle.CurrentPageIndex=e.NewPageIndex;
???dgTitle.DataBind();
??}
?}
}
详细:
namespace test.news
{
?///
?/// news_detial 的摘要说明。
?///
?public class news_detial : System.Web.UI.Page
?{
??protected System.Web.UI.WebControls.Label labContent;
?
??private void Page_Load(object sender, System.EventArgs e)
??{
???// 在此处放置用户代码以初始化页面
???SqlConnection myConnection=new SqlConnection("data source=(local);user id=sa;password=qazwsx1;initial catalog=test;");
???SqlCommand myCommand=new SqlCommand("select content from news where id=@id",myConnection);
???string id=Request.QueryString["id"];
???myCommand.Parameters.Add("@id",SqlDbType.NVarChar,500).Value=id;
???myConnection.Open();
???SqlDataReader dr=myCommand.ExecuteReader(CommandBehavior.SingleRow);
???if (dr.Read())
???{
????labContent.Visible=true;
????labContent.Text=dr[0].ToString();
???}
//???else
??}
??#region Web Form Designer generated code
??override protected void OnInit(EventArgs e)
??{
???//
???// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
???//
???InitializeComponent();
???base.OnInit(e);
??}
??
??///
??/// 设计器支持所需的方法 - 不要使用代码编辑器修改
??/// 此方法的内容。
??///
??private void InitializeComponent()
??{???
???this.Load += new System.EventHandler(this.Page_Load);
??}
??#endregion
?}
}