.NET复合控件开发随笔(一)

本文介绍了一种使用.NET自定义控件开发全站通用留言本的方法,通过继承Repeater控件,实现了灵活的模板加载和数据绑定,提高了网站活动留言本的复用性和效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用.NET一年多了,前不久开始迷上了.NET的控件开发,发觉这真是一个表现创造力的工作:)。

前不久应公司要求写一个在公司网站全站通用的留言本,因为公司旗下网站(www.163888.net)内容比较庞杂,而且时常会出现各种活动,留言本的使用频率非常高,而原先的方法是每次活动都从新开发一个留言本,实在是非常不科学的方式,而实际上每个留言本的逻辑处理基本上是一致的,因此才有了开发一个全站通用的留言本的想法。

任务到手之后,思考实现时,第一个想到了动态调用.ascx文件的方式来写一个复合控件,但是经过再三考虑之后,发觉这个方法存在不少问题。

虽然它达到了同一逻辑结构,灵活换肤的需求,但是正如我先前所说留言本在本站使用频率很高,这样每次使用都会产生1-2个.ascx文件,感觉上有点累赘。

经过再三考虑,无意中想到了Repeater这个控件,它使用ItemTemplate和AlternatingItemTemplate两个属性灵活包含模板而不产生.ascx文件,这不正是我所需要的吗?我何不写一个控件内部绑定数据的类似Repeater的控件?

有了这个想法之后,立刻使用Reflector打开了System.Web.dll找到了Repeater控件,参考着完成了下面的GuestBook控件。

 

  1 None.gif using  System;
  2 None.gif using  System.Web.UI;
  3 None.gif using  System.Web.UI.WebControls;
  4 None.gif using  System.ComponentModel;
  5 None.gif using  System.Collections;
  6 None.gif using  SunBird.Controls;
  7 None.gif using  SunBird.Components;
  8 None.gif
  9 None.gif namespace  SunBird.GuestBookControl
 10 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 11InBlock.gif    [ParseChildren(true), PersistChildren(false)]
 12InBlock.gif    public class GuestBook : Control , INamingContainer
 13ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 14InBlock.gif        ITemplate itemTemplate;
 15InBlock.gif        [DefaultValue((string)null), PersistenceMode(PersistenceMode.InnerProperty), TemplateContainer(typeof(GuestBookItem)), Browsable(false)]
 16InBlock.gif        public ITemplate ItemTemplate
 17ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 18ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn itemTemplate; }
 19ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ itemTemplate = value; }
 20ExpandedSubBlockEnd.gif        }

 21InBlock.gif
 22InBlock.gif        ITemplate alternatingItemTemplate;
 23InBlock.gif        [DefaultValue((string)null), PersistenceMode(PersistenceMode.InnerProperty), TemplateContainer(typeof(GuestBookItem)), Browsable(false)]
 24InBlock.gif        public ITemplate AlternatingItemTemplate
 25ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 26ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn alternatingItemTemplate; }
 27ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ alternatingItemTemplate = value; }
 28ExpandedSubBlockEnd.gif        }

 29InBlock.gif
 30InBlock.gif        ITemplate pagerTemplate;
 31InBlock.gif        [DefaultValue((string)null), PersistenceMode(PersistenceMode.InnerProperty), TemplateContainer(typeof(GuestBookItem)), Browsable(false)]
 32InBlock.gif        public ITemplate PagerTemplate
 33ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 34ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn pagerTemplate; }
 35ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ pagerTemplate = value; }
 36ExpandedSubBlockEnd.gif        }

 37InBlock.gif
 38InBlock.gif        object dataSource;
 39InBlock.gif        [Browsable(false)]
 40InBlock.gif        public object DataSource
 41ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 42ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn dataSource; }
 43ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ dataSource = value; }
 44ExpandedSubBlockEnd.gif        }

 45InBlock.gif
 46InBlock.gif        int type;
 47InBlock.gif        public int Type
 48ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 49ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn type; }
 50ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ type = value; }
 51ExpandedSubBlockEnd.gif        }

 52InBlock.gif
 53InBlock.gif        int markup;
 54InBlock.gif        public int Markup
 55ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 56ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn markup; }
 57ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ markup = value; }
 58ExpandedSubBlockEnd.gif        }

 59InBlock.gif
 60InBlock.gif        int top;
 61InBlock.gif        public int Top
 62ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 63ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn top; }
 64ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ top = value; }
 65ExpandedSubBlockEnd.gif        }

 66InBlock.gif
 67InBlock.gif        int pageSize;
 68InBlock.gif        public int PageSize
 69ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 70ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ pageSize = value; }
 71ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn pageSize <= 0 ? 10 : pageSize;}
 72ExpandedSubBlockEnd.gif        }

 73InBlock.gif
 74InBlock.gif        int superUser;
 75InBlock.gif        public int SuperUser
 76ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 77InBlock.gif            get 
 78ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif
 79InBlock.gif                if (superUser <= 0)
 80InBlock.gif                    return -1;
 81InBlock.gif                return superUser;
 82ExpandedSubBlockEnd.gif            }

 83ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ superUser = value; }
 84ExpandedSubBlockEnd.gif        }

 85InBlock.gif
 86InBlock.gif
 87InBlock.gif        protected override void OnLoad(EventArgs e)
 88ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 89InBlock.gif            base.OnLoad (e);
 90InBlock.gif            if (!Page.IsPostBack)
 91ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 92InBlock.gif                DataBind();
 93ExpandedSubBlockEnd.gif            }

 94ExpandedSubBlockEnd.gif        }

 95InBlock.gif
 96ContractedSubBlock.gifExpandedSubBlockStart.gif        DataBind#region DataBind
 97InBlock.gif        int totalRecords;
 98InBlock.gif        public override void DataBind()
 99ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
100ExpandedSubBlockStart.gifContractedSubBlock.gif            if (type == 0dot.gif{
101InBlock.gif                throw new ArgumentException("为指定读取类型(Type)。");
102ExpandedSubBlockEnd.gif            }

103ExpandedSubBlockStart.gifContractedSubBlock.gif            if (markup == 0dot.gif{
104InBlock.gif                throw new ArgumentException("未指定读取标识(Markup)。");
105ExpandedSubBlockEnd.gif            }

106InBlock.gif
107InBlock.gif            int pageIndex = Globals.SafeInt(UContext.Current.QueryString["PageIndex"], 1);
108InBlock.gif
109InBlock.gif            ArrayList posts = GuestBookDataProvider.Instance().GetPosts(type, markup, top, pageIndex, PageSize, out totalRecords);
110InBlock.gif            DataSource = posts;
111InBlock.gif
112InBlock.gif            base.DataBind ();
113ExpandedSubBlockEnd.gif        }

114ExpandedSubBlockEnd.gif        #endregion

115InBlock.gif
116ContractedSubBlock.gifExpandedSubBlockStart.gif        OnDataBinding#region OnDataBinding
117InBlock.gif        protected override void OnDataBinding(EventArgs e)
118ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
119InBlock.gif            CreateDataItemTemplate();
120InBlock.gif            CreatePagerTemplate();
121InBlock.gif            base.OnDataBinding(e);
122ExpandedSubBlockEnd.gif        }

123ExpandedSubBlockEnd.gif        #endregion

124InBlock.gif
125ContractedSubBlock.gifExpandedSubBlockStart.gif        CreatePagerTemplate#region CreatePagerTemplate
126InBlock.gif        private void CreatePagerTemplate()
127ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
128InBlock.gif            if (pagerTemplate != null && dataSource != null && top == 0)
129ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
130InBlock.gif                GuestBookItem item = new GuestBookItem(-1, GuestBookItemType.Pager);
131InBlock.gif                pagerTemplate.InstantiateIn(item);
132InBlock.gif                Pager p = new Pager();
133InBlock.gif                p.PageSize = PageSize;
134InBlock.gif                p.TotalRecords = totalRecords;
135InBlock.gif
136InBlock.gif                item.Controls.Add(p);
137InBlock.gif
138InBlock.gif                Controls.Add(item);
139ExpandedSubBlockEnd.gif            }

140ExpandedSubBlockEnd.gif        }

141ExpandedSubBlockEnd.gif        #endregion

142InBlock.gif
143ContractedSubBlock.gifExpandedSubBlockStart.gif        CreateDataItemTemplate#region CreateDataItemTemplate
144InBlock.gif        private void CreateDataItemTemplate()
145ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
146InBlock.gif            if (itemTemplate != null)
147ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
148InBlock.gif                if (dataSource != null)
149ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
150InBlock.gif                    int itemCount = 1;
151InBlock.gif                    
152InBlock.gif                    foreach(object data in (ArrayList)dataSource)
153ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
154InBlock.gif                        ITemplate template = itemTemplate;
155InBlock.gif                        GuestBookItemType itemType = GuestBookItemType.Item;
156InBlock.gif                        if(alternatingItemTemplate != null)
157ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
158InBlock.gif                            if (itemCount % 2 > 0)
159ExpandedSubBlockStart.gifContractedSubBlock.gif                            dot.gif{
160InBlock.gif                                template = alternatingItemTemplate;
161InBlock.gif                                itemType = GuestBookItemType.AlternatingItem;
162ExpandedSubBlockEnd.gif                            }

163ExpandedSubBlockEnd.gif                        }

164InBlock.gif                        GuestBookItem item = new GuestBookItem(itemCount, itemType);                
165InBlock.gif                        
166InBlock.gif                        item.DataItem = data;
167InBlock.gif
168InBlock.gif                        template.InstantiateIn(item);
169InBlock.gif
170InBlock.gif                        GuestBook_ItemDataBound(item);
171InBlock.gif
172InBlock.gif                        Controls.Add(item);
173InBlock.gif
174InBlock.gif                        itemCount ++;
175ExpandedSubBlockEnd.gif                    }

176InBlock.gif                    
177InBlock.gif                    ViewState["ItemCount"= itemCount;
178ExpandedSubBlockEnd.gif                }

179InBlock.gif
180InBlock.gif                ChildControlsCreated = true;
181ExpandedSubBlockEnd.gif            }

182ExpandedSubBlockEnd.gif        }

183ExpandedSubBlockEnd.gif        #endregion

184InBlock.gif
185ContractedSubBlock.gifExpandedSubBlockStart.gif        GuestBook_ItemDataBound#region GuestBook_ItemDataBound
186InBlock.gif        private void GuestBook_ItemDataBound(GuestBookItem item)
187ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
188InBlock.gif            GBPost post = item.DataItem as GBPost;
189InBlock.gif            if (post != null)
190ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
191InBlock.gif                Image GBUserHead = item.FindControl("GBUserHead"as Image;
192InBlock.gif                if (GBUserHead != null)
193ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
194InBlock.gif                    if (!Globals.IsNullorEmpty(post.UserHead))
195InBlock.gif                        GBUserHead.ImageUrl = post.UserHead;
196InBlock.gif                    else
197InBlock.gif                        GBUserHead.Visible = false;
198ExpandedSubBlockEnd.gif                }

199InBlock.gif
200InBlock.gif                HyperLink GBUser = item.FindControl("GBUser"as HyperLink;
201InBlock.gif                if (GBUser != null)
202ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
203InBlock.gif                    GBUser.Text = post.UserName;
204InBlock.gif                    GBUser.NavigateUrl = "http://" + post.UserID + ".163888.net/";
205InBlock.gif                    GBUser.Target = "_blank";
206ExpandedSubBlockEnd.gif                }

207InBlock.gif
208InBlock.gif                Literal GBBody = item.FindControl("GBBody"as Literal;
209InBlock.gif                if (GBBody != null)
210ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
211InBlock.gif                    GBBody.Text = post.Body;
212InBlock.gif                    Literal GBReplayBody = item.FindControl("GBReplayBody"as Literal;
213InBlock.gif                    if (GBReplayBody != null)
214ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
215InBlock.gif                        if (!Globals.IsNullorEmpty(post.ReplyBody))
216ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
217InBlock.gif                            GBReplayBody.Visible = true;
218InBlock.gif                            GBReplayBody.Text = "<font color=\"#808080\">回复</font>:" + post.ReplyBody;
219ExpandedSubBlockEnd.gif                        }

220InBlock.gif                        else
221ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
222InBlock.gif                            GBReplayBody.Visible = false;
223ExpandedSubBlockEnd.gif                        }

224InBlock.gif
225ExpandedSubBlockEnd.gif                    }

226ExpandedSubBlockEnd.gif                }

227InBlock.gif                
228InBlock.gif                User user = UContext.Current.User;
229InBlock.gif
230InBlock.gif                LinkButton GBDelPost = item.FindControl("GBDelPost"as LinkButton;
231InBlock.gif                if (GBDelPost != null)
232ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
233InBlock.gif                    if (user.UserID == SuperUser)
234ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
235InBlock.gif                        GBDelPost.Visible = true;
236InBlock.gif                        GBDelPost.CausesValidation = false;
237InBlock.gif                        GBDelPost.Command += new CommandEventHandler(GBDelPost_Command);
238InBlock.gif                        GBDelPost.CommandName = "DelPost";
239InBlock.gif                        GBDelPost.CommandArgument = post.ID.ToString();
240ExpandedSubBlockEnd.gif                    }

241InBlock.gif                    else
242ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
243InBlock.gif                        GBDelPost.Visible = false;
244ExpandedSubBlockEnd.gif                    }

245ExpandedSubBlockEnd.gif                }

246InBlock.gif
247InBlock.gif                HyperLink GBReplayPost = item.FindControl("GBReplayPost"as HyperLink;
248InBlock.gif                if (GBReplayPost != null)
249ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
250InBlock.gif                    if (user.UserID == SuperUser)
251ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
252InBlock.gif                        string script = "<script language=\"javascript\">" + Environment.NewLine
253InBlock.gif                                        + "function openWindow(pagePath, args, width, height) {" + Environment.NewLine
254InBlock.gif                                        + "        var s = '';" + Environment.NewLine
255InBlock.gif                                        + "        s = 'width=' + width + ',height=' + height + ',';" + Environment.NewLine
256InBlock.gif                                        + "        var win = window.open(pagePath, 'guestbook', s + 'resizable=no,toolbar=no,scrollbars=no,status=no,alwaysLowered=yes,');" + Environment.NewLine
257InBlock.gif                                        + "        win.focus();" + Environment.NewLine
258InBlock.gif                                        + "        win.moveTo( (screen.width - width)/2, (screen.height - height)/2 );}</script>" + Environment.NewLine;
259InBlock.gif                        GBReplayPost.Visible = true;
260InBlock.gif                        Page.RegisterClientScriptBlock("guestBook_openWindow", script);
261InBlock.gif                        GBReplayPost.NavigateUrl = "javascript:openWindow('/GuestBook/GuestBook_Replay.aspx?rid=" + post.ID + "', '', 350, 150);";
262ExpandedSubBlockEnd.gif                    }

263InBlock.gif                    else
264ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
265InBlock.gif                        GBReplayPost.Visible = false;
266ExpandedSubBlockEnd.gif                    }

267ExpandedSubBlockEnd.gif                }

268InBlock.gif
269InBlock.gif                Literal GBDate = item.FindControl("GBDate"as Literal;
270InBlock.gif                if (GBDate != null)
271ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
272InBlock.gif                    GBDate.Text = post.DateCreated.ToShortDateString();
273ExpandedSubBlockEnd.gif                }

274InBlock.gif
275ExpandedSubBlockEnd.gif            }

276ExpandedSubBlockEnd.gif        }

277ExpandedSubBlockEnd.gif        #endregion

278InBlock.gif
279ContractedSubBlock.gifExpandedSubBlockStart.gif        GBDelPost_Command#region GBDelPost_Command
280InBlock.gif        private void GBDelPost_Command(object sender, CommandEventArgs e)
281ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
282InBlock.gif            string cmd = e.CommandName;
283InBlock.gif            int postID = int.Parse(e.CommandArgument as string);
284InBlock.gif            switch(cmd)
285ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
286InBlock.gif                case "DelPost":
287InBlock.gif                    GuestBookDataProvider.Instance().DeletePost(postID);
288InBlock.gif                    break;
289ExpandedSubBlockEnd.gif            }

290ExpandedSubBlockEnd.gif        }

291ExpandedSubBlockEnd.gif        #endregion

292ExpandedSubBlockEnd.gif    }

293ExpandedBlockEnd.gif}

 

如果使用这样一个控件,在页面注册之后只需要使用它的ItemTemplate和AlternatingItemTemplate属性就可以加载模板了,比原先设想的动态加载.ascx文件的方式要好得多,如:

 

ExpandedBlockStart.gif ContractedBlock.gif <% dot.gif   @ Register TagPrefix="GB" Namespace="SunBird.GuestBookControl" Assembly="SunBird.GuestBook"  %>
None.gif
None.gif
< GB:GuestBook  id ="guestBook"  runat ="server"  Type ="0"  Markup ="0" >
None.gif
None.gif    
< ItemTemplate >
None.gif        模板代码
None.gif    
</ ItemTemplate >
None.gif    
< AlternatingItemTemplate >
None.gif        奇数行模板代码
None.gif    
</ AlternatingItemTemplate >
None.gif
None.gif    
< PagerTemplate  />   <!--  分页  -->
None.gif
</ GB:GuestBook >
None.gif

 

 

 

这样就完成了该控件的定义,PagerTemplate属性是分页显示。

下面是留言本信息的发布,发不分两种:1、需要登录,2、不需要登录。

于是分别写了三个控件类:

1、GuestBookCreateBase (基类) 它定义了大部分的功能。

 

 1 None.gif using  System;
 2 None.gif using  System.Web.UI.WebControls;
 3 None.gif using  SunBird.Components;
 4 None.gif
 5 None.gif namespace  SunBird.GuestBookControl
 6 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 7InBlock.gif    public class GuestBookCreateBase : GuestBookSkinBase
 8ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 9InBlock.gif        public override void DataBind()
10ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
11InBlock.gif            base.DataBind();
12ExpandedSubBlockEnd.gif        }

13InBlock.gif
14InBlock.gif        protected override void OnLoad(EventArgs e)
15ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
16InBlock.gif            base.OnLoad(e);
17InBlock.gif            if (!Page.IsPostBack)
18ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
19InBlock.gif                DataBind();
20ExpandedSubBlockEnd.gif            }

21ExpandedSubBlockEnd.gif        }

22InBlock.gif
23ContractedSubBlock.gifExpandedSubBlockStart.gif        AttachChildCotrols#region AttachChildCotrols
24InBlock.gif        protected TextBox Content;
25InBlock.gif        protected Button CreateButton;
26InBlock.gif        protected ImageButton CreateImageButton;
27InBlock.gif        protected override void AttachChildControls()
28ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
29InBlock.gif            Content = (TextBox) FindControl("Content");
30InBlock.gif            CreateButton = FindControl("CreateButton"as Button;
31InBlock.gif            if (CreateButton == null)
32ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
33InBlock.gif                throw new Exception("缺少必要的请求控件(Button)。");
34ExpandedSubBlockEnd.gif            }

35InBlock.gif            CreateButton.Click += new EventHandler(CreateButton_Click);
36ExpandedSubBlockEnd.gif        }

37ExpandedSubBlockEnd.gif        #endregion

38InBlock.gif
39ContractedSubBlock.gifExpandedSubBlockStart.gif        CreateButton_Click#region CreateButton_Click
40InBlock.gif        protected virtual void CreateButton_Click(object sender, EventArgs e)
41ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
42InBlock.gif            if (Globals.IsNullorEmpty(Content.Text))
43ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
44InBlock.gif                Globals.OutMessage("请输入评论内容。");
45InBlock.gif                return;
46ExpandedSubBlockEnd.gif            }

47InBlock.gif
48InBlock.gif            User user = UContext.Current.User;
49InBlock.gif
50InBlock.gif            GBPost post = new GBPost();
51InBlock.gif            post.UserID = user.UserID;
52InBlock.gif            post.UserName = user.UserName;
53InBlock.gif            post.Type = Type;
54InBlock.gif            post.Markup = Markup;
55InBlock.gif            post.Body = HtmlScrubber.Clean(Content.Text, falsetrue);
56InBlock.gif            post.DateCreated = DateTime.Now;
57InBlock.gif
58InBlock.gif            GuestBookDataProvider.Instance().CreatePost(post);
59InBlock.gif
60InBlock.gif            Uri uri = UContext.Current.CurrentUri;
61InBlock.gif            if (uri != null)
62ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
63InBlock.gif                Page.Response.Redirect(uri.ToString());
64ExpandedSubBlockEnd.gif            }

65ExpandedSubBlockEnd.gif        }

66ExpandedSubBlockEnd.gif        #endregion

67InBlock.gif
68InBlock.gif        int type;
69InBlock.gif        public virtual int Type
70ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
71ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn type; }
72ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ type = value; }
73ExpandedSubBlockEnd.gif        }

74InBlock.gif
75InBlock.gif        int markup;
76InBlock.gif        public virtual int Markup
77ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
78ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn markup; }
79ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ markup = value; }
80ExpandedSubBlockEnd.gif        }

81ExpandedSubBlockEnd.gif    }

82ExpandedBlockEnd.gif}

 

2、CreatePostNeedLogin : GuestBookCreateBase 需要登录,他实际上只是override了基类的CreateButton_Click方法,为该方法加入了验证登录的操作。

 

 1 None.gif using  System;
 2 None.gif using  System.Web.UI.WebControls;
 3 None.gif using  SunBird.Components;
 4 None.gif
 5 None.gif namespace  SunBird.GuestBookControl
 6 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 7InBlock.gif    public class CreatePostNeedLogin : GuestBookCreateBase
 8ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 9InBlock.gif        protected TextBox UserName;
10InBlock.gif        protected TextBox UserPassword;
11InBlock.gif        protected override void AttachChildControls()
12ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
13InBlock.gif            base.AttachChildControls();
14InBlock.gif
15InBlock.gif            UserName = (TextBox)FindControl("UserName");
16InBlock.gif            UserPassword = (TextBox)FindControl("UserPassword");
17ExpandedSubBlockEnd.gif        }

18InBlock.gif
19InBlock.gif        protected override void CreateButton_Click(object sender, EventArgs e)
20ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
21InBlock.gif            User user = UContext.Current.User;
22InBlock.gif            if (user.IsAnonymous)
23ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
24InBlock.gif                if (Globals.IsNullorEmpty(UserName.Text))
25ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
26InBlock.gif                    Globals.OutMessage("请输入用户名。");
27InBlock.gif                    return;
28ExpandedSubBlockEnd.gif                }

29InBlock.gif                if (Globals.IsNullorEmpty(UserPassword.Text))
30ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
31InBlock.gif                    Globals.OutMessage("请输入密码。");
32InBlock.gif                    return;
33ExpandedSubBlockEnd.gif                }

34InBlock.gif
35InBlock.gif                int userId;
36InBlock.gif                string name = UserName.Text;
37InBlock.gif                string password = NewBase.String.GetMd5(UserPassword.Text);
38InBlock.gif                bool result = GuestBookDataProvider.Instance().ValidateUser(name, password, out userId);
39InBlock.gif                if (result)
40ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
41InBlock.gif                    user.UserID = userId;
42InBlock.gif                    user.UserName = name;
43InBlock.gif                    Users.SaveUserCookie(user);
44ExpandedSubBlockEnd.gif                }

45InBlock.gif                else
46ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
47InBlock.gif                    Globals.OutMessage("用户名或密码错误。");
48InBlock.gif                    return;
49ExpandedSubBlockEnd.gif                }

50ExpandedSubBlockEnd.gif            }

51InBlock.gif
52InBlock.gif
53InBlock.gif            base.CreateButton_Click (sender, e);    
54ExpandedSubBlockEnd.gif        }

55InBlock.gif
56ExpandedSubBlockEnd.gif    }

57ExpandedBlockEnd.gif}

 

3、CreatePostNotNeedLogin : GuestBookCreateBase 不需要登录,实际上该类没有做任何操作仅仅只是为了方便其他人使用。

 

 1 None.gif using  System;
 2 None.gif using  System.Web.UI.WebControls;
 3 None.gif
 4 None.gif namespace  SunBird.GuestBookControl
 5 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 6InBlock.gif    public class CreatePostNotNeedLogin : GuestBookCreateBase
 7ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 8InBlock.gif        protected override void AttachChildControls()
 9ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
10InBlock.gif            base.AttachChildControls();
11InBlock.gif
12InBlock.gif
13ExpandedSubBlockEnd.gif        }

14ExpandedSubBlockEnd.gif    }

15ExpandedBlockEnd.gif}

 

控件的核心的类差不多就这几个,完整的代码如果有兴趣,请下载附件查看。

终于找到上传附件的地方了,下面是源代码,有兴趣的朋友可以下载看看,如果你有更好的方法请一定告诉我。

GuestBookControl源代码

转载于:https://www.cnblogs.com/uimeet/archive/2006/04/06/368048.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值