好了,至此,我们的webservice就完成了,大家可能不满了,还是没实现无刷新嘛,别急,这是客户端的事。下面我们就来做这项工作。
一般来说我们完全可以做一个HTML页面,而不用server page,但为了顺便说明怎样做组件,我决定作一个server control,先来看一下代码
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HTMLControls;
using System.ComponentModel;
namespace Michael.Web.UI.Controls
{
///
/// Summary description for chat.
///
[DefaultProperty("Text"),
ToolboxData("<{0}:chat runat=server>{0}:chat>")]
public class chat : System.Web.UI.WebControls.Table
{
private string doc;
private string text;
[Bindable(true),
Category("Appearance"),
DefaultValue("")]
public string Text
{
get
{
return text;
}
set
{
text = value;
}
}
///
/// Render this control to the output parameter specified.
///
/// The HTML writer to write out to
protected override void Render(HTMLTextWriter output)
{
// The script block is written to the client
output.Write(doc);
base.Render(output);
}
private string Serviceurl = "http://localhost/chat/ChatWebService.asmx?WSDL";
[Bindable(true),
Category("WebServiceProperty"),
DefaultValue("http://localhost/chat/ChatWebService.asmx?WSDL")]
public string ServiceURL
{
get
{
return Serviceurl;
}
set
{
Serviceurl = value;
}
}
private string Behaviorurl = "http://localhost/chat/webservice.htc";
[Bindable(true),
Category("WebServiceProperty"),
DefaultValue("")]
public string BehaviorURL
{
get
{
return Behaviorurl;
}
set
{
Behaviorurl = value;
}
}
private string tableCSSclass;
[Bindable(true),
Category("LayoutProperty"),
DefaultValue("")]
public string TableCSSClass
{
get
{
return tableCSSclass;
}
set
{
tableCSSclass = value;
}
}
private string titleCSSclass;
[Bindable(true),
Category("LayoutProperty"),
DefaultValue("")]
public string TitleCSSClass
{
get
{
return titleCSSclass;
}
set
{
titleCSSclass = value;
}
}
private string onlineCSSclass;
[Bindable(true),
Category("LayoutProperty"),
DefaultValue("")]
public string OnlineCSSClass
{
get
{
return onlineCSSclass;
}
set
{
onlineCSSclass = value;
}
}
private string msgCSSclass;
[Bindable(true),
Category("LayoutProperty"),
DefaultValue("")]
public string MSGCSSClass
{
get
{
return msgCSSclass;
}
set
{
msgCSSclass = value;
}
}
private string seluserCSSclass;
[Bindable(true),
Category("LayoutProperty"),
DefaultValue("")]
public string SelUserCSSClass
{
get
{
return seluserCSSclass;
}
set
{
seluserCSSclass = value;
}
}
protected override void OnInit(EventArgs e)
{
this.ID = "service";
this.Style["Behavior"] = "url('" + Behaviorurl + "')";
this.Style["Table-Layout"] = "Fixed";
if( this.Attributes["class"] == null) this.Attributes["class"] = tableCSSclass;
this.Attributes["onresult"] = UniqueID + "_onmyresult();";
TableRow tr;
// And also create 7 Table Cell elements one by one
TableCell cell = new TableCell();
cell.Attributes["class"] = titleCSSclass;
cell.Attributes["Align"] = "Left";
// Set the caption of the control
cell.Text = "Portal 聊天室";
// Instantiate a Table Roa and attach the First Cell to it
tr = new TableRow();
tr.Cells.Add(cell);
// Add the Table Row to our Control
this.Rows.Add(tr);
// Row No 2 starts here
cell = new TableCell();
cell.Attributes["class"] = onlineCSSclass;
cell.Text = "在线人员";
tr = new TableRow();
tr.Cells.Add(cell);
this.Rows.Add(tr);
// Row No 3 Starts here
cell = new TableCell();
cell.Style["Height"] = "25%";
// We create a DIV element using HTMLGenericControl object
// We can also do this using the Panel object
HTMLGenericControl d = new HTMLGenericControl("Div");
d.ID = UniqueID + "_ChatMsgs";
d.Style["Height"] = "100%";
d.Style["Width"] = "100%";
d.Style["Overflow"] = "Auto";
d.Style["Padding-Left"] = "15%";
d.ID = UniqueID + "_ChatList";
// Adding the DIV element to the Table Cell
cell.Controls.Add(d);
tr = new TableRow();
tr.Cells.Add(cell);
this.Rows.Add(tr);
// Row No 4 Starts here
cell = new TableCell();
cell.Attributes["class"] = msgCSSclass;
cell.Text = "消息:";
tr = new TableRow();
tr.Cells.Add(cell);
this.Rows.Add(tr);
// Row No 5 starts here
cell = new TableCell();
cell.Style["Height"] = "35%";
d = new HTMLGenericControl("Div");
d.ID = UniqueID + "_ChatMsgs";
d.Style["Height"] = "100%";
d.Style["Width"] = "100%";
d.Style["Overflow"] = "Auto";
cell.Controls.Add(d);
tr = new TableRow();
tr.Cells.Add(cell);
this.Rows.Add(tr);
// Row No 6 Starts here
cell = new TableCell();
cell.Attributes["class"] = seluserCSSclass;
cell.ID = UniqueID + "_Prompt";
cell.Text = "选择一个用户:";
tr = new TableRow();
tr.Cells.Add(cell);
this.Rows.Add(tr);
// Row No 7 starts here
cell = new TableCell();
cell.Text = " \r\n";
cell.Text += "
\r\n";
cell.Text += "\r\n";
cell.Text += " \r\n";
cell.Style["Color"] = "Black";
cell.Style["Background-Color"] = "Gainsboro";
tr = new TableRow();
tr.Cells.Add(cell);
this.Rows.Add(tr);
// First script Block is written into 'doc' variable
doc = "\r\n \r\n";
// Then the second script block follows
doc += " \r\n";
}
}
}
这里有几个问题,
1。我们继承的是Table,记住table等server端控件本身就继承了control类,我们做控件不一定要直接继承control
2。[“。。。”]是metadata他是用来做可视化控件的具体含义看msdn
3。我们这里采用client script的方法,可以看出实现方式与ASP中大体一致,即Server端“写”script
4。DHTML Behavior的应用,Behavior是MS扩展的CSS元素,大家可去msdn查

