来源: http://cn.codeof.com/articles/.net/asp.net-development/564.htm
无刷新的聊天室的制作兼谈组件制作和ClientSide Script(二)
作者: 未知
日期:
好了,至此,我们的webservice就完成了,大家可能不满了,还是没实现无刷新嘛,别急,这是客户端的事。下面我们就来做这项工作。
一般来说我们完全可以做一个html页面,而不用server page,但为了顺便说明怎样做组件,我决定作一个server control,先来看一下代码
1
using System;
2
using System.Web.UI;
3
using System.Web.UI.WebControls;
4
using System.Web.UI.HtmlControls;
5
using System.ComponentModel;
6
7
namespace Michael.Web.UI.Controls
8
{
9
/// <summary>
10
/// Summary description for chat.
11
/// </summary>
12
[DefaultProperty("Text"),
13
ToolboxData("<{0}:chat runat=server></{0}:chat>")]
14
public class chat : System.Web.UI.WebControls.Table
15
{
16
private string doc;
17
private string text;
18
[Bindable(true),
19
Category("Appearance"),
20
DefaultValue("")]
21
public string Text
22
{
23
get
24
{
25
return text;
26
}
27
28
set
29
{
30
text = value;
31
}
32
}
33
34
/// <summary>
35
/// Render this control to the output parameter specified.
36
/// </summary>
37
/// <param name="output"> The HTML writer to write out to </param>
38
protected override void Render(HtmlTextWriter output)
39
{
40
// The script block is written to the client
41
output.Write(doc);
42
43
base.Render(output);
44
}
45
46
private string Serviceurl = "http://localhost/chat/ChatWebService.asmx?WSDL";
47
[Bindable(true),
48
Category("WebServiceProperty"),
49
DefaultValue("http://localhost/chat/ChatWebService.asmx?WSDL")]
50
public string ServiceURL
51
{
52
get
53
{
54
return Serviceurl;
55
}
56
set
57
{
58
Serviceurl = value;
59
}
60
}
61
private string Behaviorurl = "http://localhost/chat/webservice.htc";
62
[Bindable(true),
63
Category("WebServiceProperty"),
64
DefaultValue("")]
65
public string BehaviorURL
66
{
67
get
68
{
69
return Behaviorurl;
70
}
71
set
72
{
73
Behaviorurl = value;
74
}
75
}
76
77
private string tablecssclass;
78
[Bindable(true),
79
Category("LayoutProperty"),
80
DefaultValue("")]
81
public string TableCssClass
82
{
83
get
84
{
85
return tablecssclass;
86
}
87
set
88
{
89
tablecssclass = value;
90
}
91
}
92
93
private string titlecssclass;
94
[Bindable(true),
95
Category("LayoutProperty"),
96
DefaultValue("")]
97
public string TitleCssClass
98
{
99
get
100
{
101
return titlecssclass;
102
}
103
set
104
{
105
titlecssclass = value;
106
}
107
}
108
109
private string onlinecssclass;
110
[Bindable(true),
111
Category("LayoutProperty"),
112
DefaultValue("")]
113
public string OnlineCssClass
114
{
115
get
116
{
117
return onlinecssclass;
118
}
119
set
120
{
121
onlinecssclass = value;
122
}
123
}
124
125
private string msgcssclass;
126
[Bindable(true),
127
Category("LayoutProperty"),
128
DefaultValue("")]
129
public string MSGCssClass
130
{
131
get
132
{
133
return msgcssclass;
134
}
135
set
136
{
137
msgcssclass = value;
138
}
139
}
140
141
private string selusercssclass;
142
[Bindable(true),
143
Category("LayoutProperty"),
144
DefaultValue("")]
145
public string SelUserCssClass
146
{
147
get
148
{
149
return selusercssclass;
150
}
151
set
152
{
153
selusercssclass = value;
154
}
155
}
156
protected override void OnInit(EventArgs e)
157
{
158
this.ID = "service";
159
160
this.Style["Behavior"] = "url('" + Behaviorurl + "')";
161
162
this.Style["Table-Layout"] = "Fixed";
163
164
if( this.Attributes["class"] == null) this.Attributes["class"] = tablecssclass;
165
166
this.Attributes["onresult"] = UniqueID + "_onmyresult();";
167
168
TableRow tr;
169
// And also create 7 Table Cell elements one by one
170
TableCell cell = new TableCell();
171
172
cell.Attributes["class"] = titlecssclass;
173
cell.Attributes["Align"] = "Left";
174
175
// Set the caption of the control
176
cell.Text = "Portal 聊天室";
177
// Instantiate a Table Roa and attach the First Cell to it
178
tr = new TableRow();
179
tr.Cells.Add(cell);
180
// Add the Table Row to our Control
181
this.Rows.Add(tr);
182
183
// Row No 2 starts here
184
185
cell = new TableCell();
186
187
cell.Attributes["class"] = onlinecssclass;
188
cell.Text = "在线人员";
189
tr = new TableRow();
190
tr.Cells.Add(cell);
191
this.Rows.Add(tr);
192
193
// Row No 3 Starts here
194
195
cell = new TableCell();
196
cell.Style["Height"] = "25%";
197
// We create a DIV element using HtmlGenericControl object
198
// We can also do this using the Panel object
199
HtmlGenericControl d = new HtmlGenericControl("Div");
200
d.ID = UniqueID + "_ChatMsgs";
201
d.Style["Height"] = "100%";
202
d.Style["Width"] = "100%";
203
d.Style["Overflow"] = "Auto";
204
d.Style["Padding-Left"] = "15%";
205
d.ID = UniqueID + "_ChatList";
206
// Adding the DIV element to the Table Cell
207
cell.Controls.Add(d);
208
tr = new TableRow();
209
tr.Cells.Add(cell);
210
this.Rows.Add(tr);
211
212
// Row No 4 Starts here
213
214
cell = new TableCell();
215
216
cell.Attributes["class"] = msgcssclass;
217
cell.Text = "消息:";
218
tr = new TableRow();
219
tr.Cells.Add(cell);
220
this.Rows.Add(tr);
221
222
// Row No 5 starts here
223
224
cell = new TableCell();
225
cell.Style["Height"] = "35%";
226
d = new HtmlGenericControl("Div");
227
d.ID = UniqueID + "_ChatMsgs";
228
d.Style["Height"] = "100%";
229
d.Style["Width"] = "100%";
230
d.Style["Overflow"] = "Auto";
231
cell.Controls.Add(d);
232
tr = new TableRow();
233
tr.Cells.Add(cell);
234
this.Rows.Add(tr);
235
236
// Row No 6 Starts here
237
238
cell = new TableCell();
239
240
cell.Attributes["class"] = selusercssclass;
241
cell.ID = UniqueID + "_Prompt";
242
cell.Text = "选择一个用户:";
243
tr = new TableRow();
244
tr.Cells.Add(cell);
245
this.Rows.Add(tr);
246
247
// Row No 7 starts here
248
249
cell = new TableCell();
250
cell.Text = "<INPUT Type=\"Text\" id= '" + UniqueID + "_UserInput'> \r\n";
251
cell.Text += "<BR>\r\n";
252
cell.Text += "<Button id = '" + UniqueID + "_bnSendMsg' onclick = \"return SendMsg();\" class = " + UniqueID + "_TitleLabel style = \"display:none\"> 发送 </Button>\r\n";
253
cell.Text += "<Button id = '" + UniqueID + "_bnSelectName' onclick = \"return " + UniqueID + "_SelectName();\" class = " + UniqueID + "_TitleLabel style = \"display:block\"> 登陆 </Button> \r\n";
254
cell.Style["Color"] = "Black";
255
cell.Style["Background-Color"] = "Gainsboro";
256
tr = new TableRow();
257
tr.Cells.Add(cell);
258
this.Rows.Add(tr);
259
260
// First script Block is written into 'doc' variable
261
262
doc = "\r\n<SCRIPT FOR = 'window' EVENT = 'onload()'>";
263
doc += "//alert(\"done\"); \r\n";
264
doc += "service.use(\"";
265
doc += Serviceurl + "\",\"ChatWebService\"); \r\n";
266
doc += "" + UniqueID + "_UserInput.focus();\r\n";
267
doc += "</SCRIPT> \r\n";
268
269
// Then the second script block follows
270
271
doc += "<script language=\"JavaScript\">\r\n";
272
doc += "var " + UniqueID + "_iCallID1, " + UniqueID + "_iCallID2, " + UniqueID + "_iCallID3; \r\n";
273
doc += "var " + UniqueID + "_NickName; \r\n";
274
doc += "var " + UniqueID + "_MsgXML = new ActiveXObject(\"MSXML.DOMDocument\");\r\n";
275
doc += "function " + UniqueID + "_SelectName() \r\n";
276
doc += "{ \r\n";
277
doc += "if (" + UniqueID + "_UserInput.value == \"\") return false;\r\n";
278
doc += "" + UniqueID + "_NickName = " + UniqueID + "_UserInput.value; \r\n";
279
doc += "" + UniqueID + "_bnSelectName.disabled = 'true'; \r\n";
280
doc += "" + UniqueID + "_UserInput.disabled = 'true';\r\n";
281
doc += "" + UniqueID + "_iCallID1 = service.ChatWebService.call(\"Login\"," + UniqueID + "_NickName); \r\n";
282
doc += "} \r\n";
283
doc += "function " + UniqueID + "_onmyresult() \r\n";
284
doc += "{ \r\n";
285
doc += "if((event.result.error)&&(" + UniqueID + "_iCallID1==event.result.id)) \r\n";
286
doc += "{ \r\n";
287
doc += "var xfaultcode = event.result.errorDetail.code; \r\n";
288
doc += "var xfaultstring = event.result.errorDetail.string; \r\n";
289
doc += "var xfaultsoap = event.result.errorDetail.raw;\r\n";
290
doc += "\r\n";
291
doc += "// Add code to output error information here\r\n";
292
doc += "alert(xfaultstring);\r\n";
293
doc += "" + UniqueID + "_bnSelectName.disabled = false;\r\n";
294
doc += "" + UniqueID + "_UserInput.disabled = false; \r\n";
295
doc += "" + UniqueID + "_UserInput.focus();\r\n";
296
doc += "\r\n";
297
doc += "} \r\n";
298
doc += "else if((!event.result.error)&&(" + UniqueID + "_iCallID1==event.result.id)) \r\n";
299
doc += "{ \r\n";
300
doc += "" + UniqueID + "_ChatList.innerText= event.result.value; \r\n";
301
doc += "" + UniqueID + "_ChatList.scrollTop = 2000; \r\n";
302
doc += "" + UniqueID + "_bnSelectName.style.display = 'none';\r\n";
303
doc += "" + UniqueID + "_bnSendMsg.style.display = 'block';\r\n";
304
doc += "" + UniqueID + "_UserInput.value = \"\"; \r\n";
305
doc += "" + UniqueID + "_UserInput.disabled = false; \r\n";
306
doc += "" + UniqueID + "_UserInput.focus();\r\n";
307
doc += "" + UniqueID + "_Prompt.innerText = " + UniqueID + "_NickName + \" 说:\"; \r\n";
308
doc += "window.setTimeout('" + UniqueID + "_iCallID2 = service.ChatWebService.call(\"GetMsgs\"," + UniqueID + "_NickName);',3000); \r\n";
309
doc += "} \r\n";
310
doc += "else if((event.result.error)&&(" + UniqueID + "_iCallID2==event.result.id))\r\n";
311
doc += " {\r\n";
312
doc += "var xfaultcode = event.result.errorDetail.code; \r\n";
313
doc += "var xfaultstring = event.result.errorDetail.string; \r\n";
314
doc += "var xfaultsoap = event.result.errorDetail.raw;\r\n";
315
doc += "// Add code to output error information here\r\n";
316
doc += "alert(\"xfaultstring\");\r\n";
317
doc += " }\r\n";
318
doc += " else if((!event.result.error)&&(" + UniqueID + "_iCallID2==event.result.id))\r\n";
319
doc += " {\r\n";
320
doc += "var xmlResult = event.result.raw.xml; \r\n";
321
doc += " if (xmlResult != \"\" && xmlResult != null)\r\n";
322
doc += " {\r\n";
323
doc += "\r\n";
324
doc += "" + UniqueID + "_MsgXML.loadXML(xmlResult);\r\n";
325
doc += " " + UniqueID + "_ChatList.innerText = " + UniqueID + "_MsgXML.selectSingleNode(\"//UserList\").text; \r\n";
326
doc += "" + UniqueID + "_ChatList.scrollTop = 2000; \r\n";
327
doc += " " + UniqueID + "_ChatMsgs.innerHTML += " + UniqueID + "_MsgXML.selectSingleNode(\"//Messages\").text;\r\n";
328
doc += "" + UniqueID + "_ChatMsgs.scrollTop = 2000; \r\n";
329
doc += " }\r\n";
330
doc += " window.setTimeout('" + UniqueID + "_iCallID2 = service.ChatWebService.call(\"GetMsgs\"," + UniqueID + "_NickName);',3000);\r\n";
331
doc += " }\r\n";
332
doc += "else if((event.result.error)&&(" + UniqueID + "_iCallID3==event.result.id))\r\n";
333
doc += " {\r\n";
334
doc += "var xfaultcode = event.result.errorDetail.code; \r\n";
335
doc += "var xfaultstring = event.result.errorDetail.string; \r\n";
336
doc += "var xfaultsoap = event.result.errorDetail.raw;\r\n";
337
doc += "// Add code to output error information here\r\n";
338
doc += "alert(\"xfaultstring\");\r\n";
339
doc += " }\r\n";
340
doc += " else if((!event.result.error)&&(" + UniqueID + "_iCallID3==event.result.id))\r\n";
341
doc += " {\r\n";
342
doc += "var xmlResult = event.result.raw.xml; \r\n";
343
doc += " if (xmlResult != \"\" && xmlResult != null)\r\n";
344
doc += " {\r\n";
345
doc += "\r\n";
346
doc += "" + UniqueID + "_MsgXML.loadXML(xmlResult);\r\n";
347
doc += " " + UniqueID + "_ChatList.innerText = " + UniqueID + "_MsgXML.selectSingleNode(\"//UserList\").text; \r\n";
348
doc += " " + UniqueID + "_ChatMsgs.innerHTML += " + UniqueID + "_MsgXML.selectSingleNode(\"//Messages\").text;\r\n";
349
doc += " " + UniqueID + "_ChatList.scrollTop = 2000; \r\n";
350
doc += " " + UniqueID + "_bnSendMsg.disabled = false;\r\n";
351
doc += " " + UniqueID + "_ChatMsgs.scrollTop = 2000; \r\n";
352
doc += " " + UniqueID + "_UserInput.value = \"\";\r\n";
353
doc += " " + UniqueID + "_UserInput.disabled = false;\r\n";
354
doc += " " + UniqueID + "_UserInput.focus();\r\n";
355
doc += " }\r\n";
356
doc += " window.setTimeout('" + UniqueID + "_iCallID2 = service.ChatWebService.call(\"GetMsgs\"," + UniqueID + "_NickName);',3000);\r\n";
357
doc += " }\r\n";
358
doc += "} \r\n";
359
doc += "function SendMsg()\r\n";
360
doc += "{ \r\n";
361
doc += "if (" + UniqueID + "_UserInput.value == \"\") return false;\r\n";
362
doc += "" + UniqueID + "_bnSendMsg.disabled = 'true';\r\n";
363
doc += "" + UniqueID + "_UserInput.disabled = 'true';\r\n";
364
doc += "" + UniqueID + "_iCallID3 = service.ChatWebService.call(\"XchangeMsgs\"," + UniqueID + "_NickName," + UniqueID + "_UserInput.value);\r\n";
365
doc += "} \r\n";
366
doc += "</script> \r\n";
367
368
}
369
}
370
}
371

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

这里有几个问题,
1。我们继承的是Table,记住table等server端控件本身就继承了control类,我们做控件不一定要直接继承control
2。[“。。。”]是metadata他是用来做可视化控件的具体含义看msdn
3。我们这里采用client script的方法,可以看出实现方式与asp中大体一致,即Server端“写”script
4。Dhtml Behavior的应用,Behavior是MS扩展的css元素,大家可去msdn查

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述