用UpDataPanel实现同步提交显示数据
这是我来博客园发的第一篇文章
做这个的灵感来自TerryLee的博客园的一篇文章,我在这儿只是把他文章里的东西,我照着做了一次,并引入了我自己的一些东西而已,希望各位老鸟不要笑话~呵呵
首先新建一个ASP.net AJAX网站,名字随便取了
在页面中放入一个ScriptManage控件,这是不能少的,在ScriptManage下面,放入两个UpDataPanel控件,一个用于做提交数据的窗口,一个用来显示GridView里面的是数据用,至于怎么插入表格之类的,这儿就不多说了。
下面我把有关提交数据到数据库的方法贴出来一下,希望和大叫讨论一下我的方法:
1.在目录下建立App_Code文件夹,建立TiJiao.cs
:)
做这个的灵感来自TerryLee的博客园的一篇文章,我在这儿只是把他文章里的东西,我照着做了一次,并引入了我自己的一些东西而已,希望各位老鸟不要笑话~呵呵
首先新建一个ASP.net AJAX网站,名字随便取了
在页面中放入一个ScriptManage控件,这是不能少的,在ScriptManage下面,放入两个UpDataPanel控件,一个用于做提交数据的窗口,一个用来显示GridView里面的是数据用,至于怎么插入表格之类的,这儿就不多说了。
下面我把有关提交数据到数据库的方法贴出来一下,希望和大叫讨论一下我的方法:
1.在目录下建立App_Code文件夹,建立TiJiao.cs
1 using System;
2 using System.Data;
3 using System.Configuration;
4 using System.Web;
5 using System.Web.Security;
6 using System.Web.UI;
7 using System.Web.UI.WebControls;
8 using System.Web.UI.WebControls.WebParts;
9 using System.Web.UI.HtmlControls;
10 using System.Data.SqlClient;
11 using SqlHelper;
12
13 /// <summary>
14 /// TiJiao 的摘要说明
15 /// </summary>
16 public class TiJiao
17 {
18 public TiJiao()
19 {
20 //
21 // TODO: 在此处添加构造函数逻辑
22 //
23 }
24
25 private string id = string.Empty;
26
27 private string userName = string.Empty;
28
29 private string password = string.Empty;
30
31 public string Id
32 {
33 get { return this.id; }
34 set { this.id = value; }
35 }
36
37 public string UserName
38 {
39 get { return this.userName; }
40 set { this.userName = value; }
41 }
42
43 public string Password
44 {
45 get { return this.password; }
46 set { this.password = value; }
47 }
48 }
2.在App_Code目录下建立一个Entity实体文件夹,用于对数据库的操作,具体代码为:2 using System.Data;
3 using System.Configuration;
4 using System.Web;
5 using System.Web.Security;
6 using System.Web.UI;
7 using System.Web.UI.WebControls;
8 using System.Web.UI.WebControls.WebParts;
9 using System.Web.UI.HtmlControls;
10 using System.Data.SqlClient;
11 using SqlHelper;
12
13 /// <summary>
14 /// TiJiao 的摘要说明
15 /// </summary>
16 public class TiJiao
17 {
18 public TiJiao()
19 {
20 //
21 // TODO: 在此处添加构造函数逻辑
22 //
23 }
24
25 private string id = string.Empty;
26
27 private string userName = string.Empty;
28
29 private string password = string.Empty;
30
31 public string Id
32 {
33 get { return this.id; }
34 set { this.id = value; }
35 }
36
37 public string UserName
38 {
39 get { return this.userName; }
40 set { this.userName = value; }
41 }
42
43 public string Password
44 {
45 get { return this.password; }
46 set { this.password = value; }
47 }
48 }
1 using System;
2 using System.Data;
3 using System.Configuration;
4 using System.Web;
5 using System.Web.Security;
6 using System.Web.UI;
7 using System.Web.UI.WebControls;
8 using System.Web.UI.WebControls.WebParts;
9 using System.Web.UI.HtmlControls;
10 using System.Data.SqlClient;
11 using SqlHelper;
12
13 /// <summary>
14 /// TiJiaoDao 的摘要说明
15 /// </summary>
16 public class TiJiaoDao
17 {
18 public TiJiaoDao()
19 {
20 //
21 // TODO: 在此处添加构造函数逻辑
22 //
23 }
24 private static string UserName = "@UserName";
25
26 private static string Password = "@Password";
27
28 private static string INSERTINTO_LoginTable = "INSERT INTO LoginTable VALUES(@UserName,@Password) ";
29
30 public static bool LoginTable(TiJiao TiJiao9)
31 {
32 SqlParameter[] parms = new SqlParameter[]{
33 new SqlParameter(UserName,SqlDbType.NVarChar,50),
34 new SqlParameter(Password,SqlDbType.NVarChar,50)};
35
36 parms[0].Value = TiJiao9.UserName;
37 parms[1].Value = TiJiao9.Password;
38
39
40
41
42 //获取连接字符串,插入数据到SQL数据库
43 int tag = SqlHelper.SqlHelpDao.ExecuteNonQuery(GetConnectionString.ConnectionString, CommandType.Text, INSERTINTO_LoginTable, parms);
44 if (tag > 0)
45 {
46 return true;
47 }
48 return false;
49 }
50 }
51
3.在刚才建好的UpDataPanel中,插入两个TextBox,一个Button,在对这个页面进行代码的编写,如下:2 using System.Data;
3 using System.Configuration;
4 using System.Web;
5 using System.Web.Security;
6 using System.Web.UI;
7 using System.Web.UI.WebControls;
8 using System.Web.UI.WebControls.WebParts;
9 using System.Web.UI.HtmlControls;
10 using System.Data.SqlClient;
11 using SqlHelper;
12
13 /// <summary>
14 /// TiJiaoDao 的摘要说明
15 /// </summary>
16 public class TiJiaoDao
17 {
18 public TiJiaoDao()
19 {
20 //
21 // TODO: 在此处添加构造函数逻辑
22 //
23 }
24 private static string UserName = "@UserName";
25
26 private static string Password = "@Password";
27
28 private static string INSERTINTO_LoginTable = "INSERT INTO LoginTable VALUES(@UserName,@Password) ";
29
30 public static bool LoginTable(TiJiao TiJiao9)
31 {
32 SqlParameter[] parms = new SqlParameter[]{
33 new SqlParameter(UserName,SqlDbType.NVarChar,50),
34 new SqlParameter(Password,SqlDbType.NVarChar,50)};
35
36 parms[0].Value = TiJiao9.UserName;
37 parms[1].Value = TiJiao9.Password;
38
39
40
41
42 //获取连接字符串,插入数据到SQL数据库
43 int tag = SqlHelper.SqlHelpDao.ExecuteNonQuery(GetConnectionString.ConnectionString, CommandType.Text, INSERTINTO_LoginTable, parms);
44 if (tag > 0)
45 {
46 return true;
47 }
48 return false;
49 }
50 }
51
1 protected void Page_Load(object sender, EventArgs e)
2 {
3 SqlConnection con = new SqlConnection("server=(local);database=AtlasSMDB;uid=sa;password=sa");
4 SqlDataAdapter da = new SqlDataAdapter("select * from LoginTable", con);
5 DataSet ds = new DataSet();
6 da.Fill(ds, "LoginTable");
7 DataTable dt = ds.Tables["LoginTable"];
8 this.GridView1.DataSource = dt.DefaultView;
9 this.GridView1.DataBind();
10
11 }
12 protected void UpdateBtn_Click(object sender, EventArgs e)
13 {
14 TiJiao TiJiao9 = new TiJiao();
15
16 TiJiao9.UserName = this.txtUserName.Text.Trim();
17 TiJiao9.Password = this.txtPassword.Text.Trim();
18
19 if (TiJiaoDao.LoginTable(TiJiao9) == true)
20 {
21 Response.Redirect("~/Default.aspx");
22 }
23 }
至此,一个无状态的提交显示的页面就做好了,第一次写技术性的博客,写的不好之处,请大家包涵哈!~2 {
3 SqlConnection con = new SqlConnection("server=(local);database=AtlasSMDB;uid=sa;password=sa");
4 SqlDataAdapter da = new SqlDataAdapter("select * from LoginTable", con);
5 DataSet ds = new DataSet();
6 da.Fill(ds, "LoginTable");
7 DataTable dt = ds.Tables["LoginTable"];
8 this.GridView1.DataSource = dt.DefaultView;
9 this.GridView1.DataBind();
10
11 }
12 protected void UpdateBtn_Click(object sender, EventArgs e)
13 {
14 TiJiao TiJiao9 = new TiJiao();
15
16 TiJiao9.UserName = this.txtUserName.Text.Trim();
17 TiJiao9.Password = this.txtPassword.Text.Trim();
18
19 if (TiJiaoDao.LoginTable(TiJiao9) == true)
20 {
21 Response.Redirect("~/Default.aspx");
22 }
23 }
:)