温馨提示:代码在线浏览功能只能做为源码浏览参考,如果想更进一步了解该代码请下载:Asp.net2.0的电影在线点播系统片段源码
当前文件:
MovieShowSample/List.aspx.cs,打开代码结构图
MovieShowSample/List.aspx.cs,打开代码结构图1using System; 2
using System.Data; 3
using System.Configuration; 4
using System.Collections; 5
using System.Web; 6
using System.Web.Security; 7
using System.Web.UI; 8
using System.Web.UI.WebControls; 9
using System.Web.UI.WebControls.WebParts; 10
using System.Web.UI.HtmlControls; 11
using System.IO; 12
using System.Globalization; 13
using System.Reflection; 14
public partial class List : System.Web.UI.Page,ICallbackEventHandler 15
{ 16
protected void Page_Load(object sender, EventArgs e) 17
{ 18
//从cookie中获取身份验证票信息 19
string cookieName = FormsAuthentication.FormsCookieName; 20
HttpCookie authCookie = Context.Request.Cookies[cookieName]; 21
22
23
if (authCookie != null) 24
{ 25
div2.Visible = true; 26
//解密 27
FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value); 28
Link.Text = authTicket.Name.ToString(); 29
LinkRole.Text = authTicket.UserData.ToString(); 30
31
} 32
else 33
{ div2.Visible = false; } 34
35
//第一次加载时绑定datalist控件为第一页 36
if (!Page.IsPostBack) 37
bind(0); 38
39
40
} 41
42
43
protected void Linkout_Click(object sender, EventArgs e) 44
{ 45
System.Web.Security.FormsAuthentication.SignOut(); 46
47
string url = "List.aspx?category=" + Request.QueryString["category"]; 48
Context.Response.Redirect(url); 49
} 50
protected void login_Click(object sender, ImageClickEventArgs e) 51
{ 52
if (txtid.Text != "" && txtpwd.Text != "") 53
{ 54
string UserName = txtid.Text.Trim(); 55
string UserPwd = txtpwd.Text.Trim(); 56
57
DataVisit mydv = new DataVisit();//创建访问层的类 58
if (mydv.Confirm(UserName, UserPwd))//验证用户是否合法 59
{ 60
string UserRoles = mydv.GetRoles(UserName);//获取用户角色的字符串 61
//建立身份验证票据对象 62
FormsAuthenticationTicket Ticket = new FormsAuthenticationTicket(1, UserName, DateTime.Now, DateTime.Now.AddHours(1), false, UserRoles, "/"); 63
//加密序列化验证票为字符串 64
string HashTicket = FormsAuthentication.Encrypt(Ticket); 65
//创建cookie 66
HttpCookie UserCookie = new HttpCookie(FormsAuthentication.FormsCookieName, HashTicket); 67
//输出cookie 68
Context.Response.Cookies.Add(UserCookie); 69
// div2.Visible = true; 70
string url="List.aspx?category="+Request.QueryString["category"]; 71
Context.Response.Redirect(url); 72
73
74
75
76
} 77
else 78
{ 79
txtid.Text = "不正确!用户名或密码"; 80
81
} 82
83
} 84
} 85
protected void register_Click(object sender, ImageClickEventArgs e) 86
{ 87
Response.Redirect("Register.aspx"); 88
} 89
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e) 90
{ 91
92
93
if (e.Item.ItemType == ListItemType.Footer) 94
{ 95
ImageButton linkpre = (ImageButton)e.Item.FindControl("linkpre"); 96
ImageButton linknext = (ImageButton)e.Item.FindControl("linknext"); 97
DropDownList ddl = (DropDownList)e.Item.FindControl("DropDownList1"); 98
Label all = (Label)e.Item.FindControl("all"); 99
Label now = (Label)e.Item.FindControl("now"); 100
linknext.Enabled = false; 101
linkpre.Enabled = false; 102
103
int newpageIndex; 104
if (!Page.IsPostBack) 105
{ 106
newpageIndex = 0;//第一次加载时为第一页 107
108
} 109
else 110
{ 111
if (result == null)//如果回调参数为空 112
Session["page"] = 0;//session保存当前页面号 113
else 114
{ //由于回调结果在绑定事件之前 所以设置回调后的页面 保存在session中 115
string[] parts = result.Split('|'); 116
string n = parts[1]; 117
Session["page"] = n; 118
} 119
newpageIndex = Convert.ToInt32(Session["page"]); // 取出当前页面 120
} 121
122
if (newpageIndex > 0) 123
{ 124
linkpre.Enabled = true; 125
//设置回调事件 126
linkpre.Attributes.Add("onclick", "setpageto(" + (newpageIndex-1).ToString() + ");return false;"); 127
} 128
if (newpageIndex < pds().PageCount - 1) 129
{ 130
131
linknext.Enabled = true; 132
//设置回调事件 133
linknext.Attributes.Add("onclick", "setpageto(" + (newpageIndex+1).ToString() + "); return false;"); 134
} 135
136
137
138
for (int i = 1; i <= pds().PageCount;i++ ) 139
{ 140
ListItem item = new ListItem(); 141
item.Value = i.ToString(); 142
ddl.Items.Add(item); 143
} 144
ddl.Text = (newpageIndex + 1).ToString(); 145
ddl.Attributes.Add("onchange", "setpageto1(this); return false;"); 146
147
now.Text = pds(newpageIndex+1).CurrentPageIndex.ToString(); 148
all.Text = pds().PageCount.ToString(); 149
150
151
152
153
154
} 155
156
157
} 158
159
160
161
//返回一个可分页的数据源 162
public PagedDataSource pds() 163
{ 164
PagedDataSource pds = new PagedDataSource(); 165
166
DataVisit mydv = new DataVisit(); 167
string category=Request.QueryString["category"]; 168
switch (category) 169
{ 170
case "1": category = "动作"; 171
break; 172
case "2": category = "爱情"; 173
break; 174
case "3": category = "科幻"; 175
break; 176
case "4": category = "卡通"; 177
break; 178
case "5": category = "其他"; 179
break; 180
case "6": category = "剧情"; 181
break; 182
case "7": category = "喜剧"; 183
break; 184
case "8": category = "恐怖"; 185
break; 186
case "9": category = "大片"; 187
break; 188
case "10": category = "战争"; 189
break; 190
case "11": category = "美国"; 191
break; 192
case "12": category = "香港台湾"; 193
break; 194
case "13": category = "韩国"; 195
break; 196
case "14": category = "中国内地"; 197
break; 198
case "15": category = "其他地区"; 199
break; 200
201
} 202
DataTable mydt = mydv.getCategory(category) ; 203
pds.DataSource = mydt.DefaultView; 204
pds.AllowPaging = true; 205
int size =12; 206
pds.PageSize = size; 207
pds.CurrentPageIndex = pds.CurrentPageIndex; 208
return pds; 209
} 210
//根据一个传来的整数作为当前页码并返回分页后的数据员 211
public PagedDataSource pds(int n) 212
{ 213
PagedDataSource pds = new PagedDataSource(); 214
215
DataVisit mydv = new DataVisit(); 216
string category=Request.QueryString["category"]; 217
switch (category) 218
{ 219
case "1": category = "动作"; 220
break; 221
case "2": category = "爱情"; 222
break; 223
case "3": category = "科幻"; 224
break; 225
case "4": category = "卡通"; 226
break; 227
case "5": category = "其他"; 228
break; 229
case "6": category = "剧情"; 230
break; 231
case "7": category = "喜剧"; 232
break; 233
case "8": category = "恐怖"; 234
break; 235
case "9": category = "大片"; 236
break; 237
case "10": category = "战争"; 238
break; 239
case "11": category = "美国"; 240
break; 241
case "12": category = "香港台湾"; 242
break; 243
case "13": category = "韩国"; 244
break; 245
case "14": category = "中国内地"; 246
break; 247
case "15": category = "其他地区"; 248
break; 249
250
} 251
DataTable mydt = mydv.getCategory(category); 252
253
pds.DataSource = mydt.DefaultView; 254
pds.AllowPaging = true; 255
int size = 12; 256
pds.PageSize = size; 257
pds.CurrentPageIndex = n; 258
return pds; 259
} 260
261
//绑定DataList 262
public void bind(int n) 263
{ 264
DataList1.DataSource = pds(n); 265
DataList1.DataBind(); 266
267
} 268
269
270
271
272
273
//回调方法 274
public string result; 275
public string GetCallbackResult() 276
{ 277
string[] parts = result.Split('|'); 278
//搜索名为MethodName的方法 279
MethodInfo method = this.GetType().GetMethod(parts[0]); 280
281
//定义一个object数组用来保存参数 282
object[] args = new object[parts.Length - 1]; 283
Array.Copy(parts, 1, args, 0, args.Length); 284
285
return (string)method.Invoke(this, args); 286
287
288
} 289
public void RaiseCallbackEvent(string eventArgument) 290
{ 291
result = eventArgument; 292
} 293
public string pageChange(string newIndex) 294
{ 295
296
int n = Convert.ToInt32(newIndex); 297
bind(n); 298
return getRenderCode(); 299
} 300
301
public string getRenderCode() 302
{ 303
StringWriter write1 = new StringWriter(CultureInfo.InvariantCulture); 304
HtmlTextWriter write2 = new HtmlTextWriter(write1); 305
306
DataList1.RenderControl(write2); 307
write2.Flush(); 308
write2.Close(); 309
310
return write1.ToString(); 311
312
} 313
314
}





}
}