ASP.net自己常用的一些代码[新同学请进]
1.如何在Reapter控件里显示数据库数据?
页面部分代码:
2 <HeaderTemplate>
3 <table class="STYLE2" border="0" cellpadding="1" cellspacing="1" style="border-right:#CC6600 thin dotted; border-top:#CC6600 thin dotted;
4 border-left:#CC6600 thin dotted; width: 480px; border-bottom:#CC6600 thin dotted;
5 background-color: #333333; color: #ffffff; text-decoration: none;">
6 <tr>
7 <th bgcolor="#CC6600" style="width:40px ;">编号</th><th bgcolor="#CC66OO" style="width:150px">歌名</th><th bgcolor="#CC6600" style="width:30px">试听</th><th bgcolor="#CC6600" style="width:60px">点击率</th>
8 </tr>
9 </HeaderTemplate>
10 <ItemTemplate>
11 <tr>
12 <td bgcolor="#666666">
13 <asp:Label runat="server" ID="LblSongID" Text='<%# Eval("SongID") %>' />
14 </td>
15 <td bgcolor="#666666">
16 <asp:Label runat="server" ID="LblSongName" Text='<%# Eval("SongName") %>' />
17 </td>
18 <td bgcolor="#666666" style="width:30px;">
19 <a href="sound.html" target="_blank" ><img src="images/play.gif" border="0" alt="点击试听"/></a> </td>
20 <td bgcolor="#666666">
21 <asp:Label runat="server" ID="LblSongStat" Text='<%# Eval("SongStat") %>' />
22 </td>
23 </tr>
24 </ItemTemplate>
25 <FooterTemplate>
26 </table>
27 </FooterTemplate>
28 </asp:Repeater>
29
C#代码
2 {
3 SqlDataSource SongDataSource = new SqlDataSource();
4
5 SongDataSource.ConnectionString = SqlHelper.ConnectionStringLocalTransaction;
6 SongDataSource.DataSourceMode = SqlDataSourceMode.DataSet;
7 SongDataSource.SelectCommandType = SqlDataSourceCommandType.Text;
8 SongDataSource.SelectCommand = "SELECT * FROM [SongInfo] where ID =下拉列表选择的文本 ORDER BY [ID]";
9
10 this.Repeater1.DataSource = SongDataSource;
11 this.Repeater1.DataBind();
12 }
13
2.Session不能查看别人信息:
2 {
3 string userName = Session["UserName"].ToString();
4 if (this.TextBox1.Text.ToString().Trim() == userName)
5 {
6 this.GridView1.DataBind();
7 }
8 else
9 {
10 Response.Write("<script>alert('你没有权限查看别人的成绩');</script>");
11 }
12 }
3.使用Session变量:
使用Session变量
使用Session变量是可以在页面间传递值的的另一种方式,在本例中我们把控件中的值存在Session变量中,然后在另一个页面中使用它,以不同页面间实现值传递的目的。但是,需要注意的是在Session变量存储过多的数据会消耗比较多的服务器资源,在使用session时应该慎重,当然了,我们也应该使用一些清理动作来去除一些不需要的session来降低资源的无谓消耗。使用Session变量传递值的一般步骤如下:
1,在页面里添加必要的控件
2,创建可以返回表单的按钮和链接按钮
3,在按钮或链接按钮的单击事件里,把控件的值添加到session变量里
4,使用Response.Redirect方法重定向到另一个页面
5,在另一个页面提取session的值,在确定不需要使用该session时,要显式清除它
下面的代码片断演示了如何实现这个方法:
源页面代码:
2 (object sender, System.EventArgs e)
3 {
4 //textbox1 and textbox2 are webform
5 //controls
6 Session["name"]=TextBox1.Text;
7 Session["email"]=TextBox2.Text;
8 Server.Transfer("anotherwebform.aspx");
9 }
10
2 (object sender, System.EventArgs e)
3 {
4 Label1.Text=Session["name"].ToString();
5 Label2.Text=Session["email"].ToString();
6 Session.Remove("name");
7 Session.Remove("email");
8 }
4.FileUpLoad确定按钮事件:
2 {
3 try
4 {
5 string path = Server.MapPath("~/Image/");
6
7 FileUpload1.PostedFile.SaveAs(path + FileUpload1.FileName);
8
9 Response.Write("<Script>alert('上传成功');</Script>");
10 }
11 catch (Exception ex)
12 {
13 Response.Write("上传失败,原因" + ex.Message);
14 }
15 }
5.GridView填充数据:
2 {
3 SqlConnection con = new SqlConnection("server=(local);database=StudentManageDB;uid=sa;password=sa");
4 SqlDataAdapter da = new SqlDataAdapter("select * from Student_course" , con);
5 DataSet ds = new DataSet();
6 da.Fill(ds, "Student_course");
7 DataTable dt = ds.Tables["Student_course"];
8 this.GridView1.DataSource = dt.DefaultView;
9 this.GridView1.DataBind();
10
11
12 }
6.提交数据到数据库:
一,需要App_Code目录下建立一个实体Entity文件夹(App_Code/Entity)
二,在App_Code目录下建立Reg.cs类(App_Code/Reg.cs)
三,在Entity文件夹下建立RegDao.cs类(App_Code/Entity/RegDao.cs)
四,在App_Code目录下建立GetConnectionString类,此类是获取数据库连接字符串(App_Code/GetConnectionString)
五,记得using SqlHelper;
六,一定要从vs.net里面添加引用来导入Sqlhelper.dll文件,否则无用。
**************************************************************************
2 public class Reg
3 {
4 public Reg()
5 {
6 //
7 // TODO: 在此处添加构造函数逻辑
8 //
9 }
10 private string userId = string.Empty;//此处一地要注意,要小写userId
11 private string userPassword = string.Empty;
12 private string email = string.Empty;
13 private string city = string.Empty;
14 private string zipCode = string.Empty;
15 private string userDate = string.Empty;
16
17 public string UserId
18 {
19 get { return this.userId; }
20 set { this.userId = value; }
21 }
22
23 public string UserPassword
24 {
25 get { return this.userPassword; }
26 set { this.userPassword = value; }
27 }
28
29 public string Email
30 {
31 get { return this.email; }
32 set { this.email = value; }
33 }
34
35 public string City
36 {
37 get { return this.city; }
38 set { this.city = value; }
39 }
40
41 public string ZipCode
42 {
43 get { return this.zipCode; }
44 set { this.zipCode = value; }
45 }
46
47 public string UserDate
48 {
49 get { return this.userDate; }
50 set { this.userDate = value; }
51 }
52 }
53
54
*******************************************************************
2 public class RegDao
3 {
4 public RegDao()
5 {
6 //
7 // TODO: 在此处添加构造函数逻辑
8 //
9 }
10 private static string UserId = "@UserId";
11
12 private static string UserPassword= "@UserPassword";
13
14 private static string Email = "@Email";
15
16 private static string City = "@City";
17
18 private static string ZipCode = "@ZipCode";
19
20 private static string UserDate = "@UserDate";
21
22 //执行SQL语句
23 private static string INSERTINTO_Userinfo="insert into Userinfo Values(@UserId,@UserPassword,@Email,@City,@ZipCode,@UserDate)";
24
25 private static bool AddUserinfo(Reg user)//这儿的括号里面的表示什么?(对象名 类名)传到AddUserinfo这个私有静态方法。对象名要写对,类名可以任意取。
26 {
27 SqlParameter[] parms = new SqlParameter[]{
28 new SqlParameter(UserId,SqlDbType.Char,50),//以后数据库最好都写成varchar类型,长度给长一些。
29 new SqlParameter(UserPassword,SqlDbType.Char,16),
30 new SqlParameter(Email,SqlDbType.NVarChar,40),
31 new SqlParameter(City,SqlDbType.Char,15),
32 new SqlParameter(ZipCode,SqlDbType.Char,10),
33 new SqlParameter(UserDate,SqlDbType.DateTime,8)};
34
35 parms[0].Value=user.UserId;
36 parms[1].Value=user.UserPassword;
37 parms[2].Value=user.Email;
38 parms[3].Value=user.City;
39 parms[4].Value=user.ZipCode;
40 parms[5].Value=user.UserDate;
41
42 //获取连接字符串,插入数据到SQL数据库
43 int tag = SqlHelper.SqlHelpDao.ExecuteNonQuery(GetConnectString.ConnectionString, CommandType.Text, INSERTINTO_Userinfo, parms);
44 if (tag > 0)
45 {
46 return true;
47 }
48 return false;
49 }
50 }
51
52
***********************************************************************************
2 protected void regBtn_Click(object sender, EventArgs e)
3 {
4 Reg user = new Reg();
5
6 user.UserId = this.UserId.Text.Trim();
7 user.UserPassword = this.UserPassword.Text.Trim();
8 user.Email = this.Email.Text.Trim();
9 user.City = this.City.Text.Trim();
10 user.ZipCode = this.Zipcode.Text.Trim();
11
12 //if (RegDao.AddUserinfo(User) == true)
13 //{
14 // Response.Write("<script>alert('注册成功')</script>");
15 //}
16
17 }
18
19
*************************************************************************************
2 public abstract class GetConnectString
3 {
4 public GetConnectString()
5 {
6 //
7 // TODO: 在此处添加构造函数逻辑
8 //
9 }
10 public static readonly string ConnectionString=ConfigurationManager.ConnectionStrings["WebShareDBConnectionString"].ConnectionString;
11
12 }
13
14
**************************************************************************************
Reg.cs与RegDao.cs的关系?
Reg是用户的属性,RegDao是对用户注册操作的业务。