Asp.Net的控件如何与Server交互
点击次数:32 次 发布日期:2008-11-26 12:26:29 作者:源代码网
|
源代码网推荐 如:Asp.Net中的Button就是等于<input type="submit">. 源代码网推荐 但是现在Asp.Net的好多控件都可以任意的和服务器端交互,如:LinkButton. 源代码网推荐 这是怎么实现的呢? 源代码网推荐 难道是一种全新的方式吗? 源代码网推荐 其实,这只是微软的一种变通的方式。 源代码网推荐 我们先来看看客户端的代码是如何的. 源代码网推荐 我这里是一个带有LinkButton的页面, 源代码网推荐 其实LinkButton在客户端就等于html里的A. 源代码网推荐 我们来看看LinkButton为什么也能和服务器端交互? 源代码网推荐 当我们点击右键查看该页面的源代码时,看到: 源代码网推荐 源代码网推荐 <script type="text/javascript"> 源代码网推荐 <!-- 源代码网推荐 var theForm = document.forms["ctl00"]; 源代码网推荐 if (!theForm) { 源代码网推荐 theForm = document.ctl00; 源代码网推荐 } 源代码网推荐 function __doPostBack(eventTarget, eventArgument) { 源代码网推荐 if (!theForm.onsubmit || (theForm.onsubmit() != false)) { 源代码网推荐 theForm.__EVENTTARGET.value = eventTarget; 源代码网推荐 theForm.__EVENTARGUMENT.value = eventArgument; 源代码网推荐 theForm.submit(); 源代码网推荐 } 源代码网推荐 } 源代码网推荐 // --> 源代码网推荐 </script> 源代码网推荐 源代码网推荐 <a id="simpleLinkButton1" href="javascript:__doPostBack("simpleLinkButton1","")">Click Me</a> 源代码网推荐 <input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" /> 源代码网推荐 <input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" /> 源代码网推荐 从上面的代码中就可以看出simpleLinkButton1是一个链接,通过客户端代码来提交所在的表单。 源代码网推荐 而其中两个隐藏域,则用来交换数据的,就是把__doPostBack的两个参数值赋给这两个隐藏域。 源代码网推荐 这就是为什么LinkButton也具有提交数据的最直观的原因。 源代码网推荐 源代码网推荐 那么这上面的代码是怎么生成的呢? 源代码网推荐 我们看LinkButton的源代码就可以清楚地知道: 源代码网推荐 protected internal override void OnPreRender(EventArgs e) 源代码网推荐 { 源代码网推荐 base.OnPreRender(e); 源代码网推荐 if ((this.Page != null) && this.Enabled) 源代码网推荐 { 源代码网推荐 this.Page.RegisterPostBackScript(); 源代码网推荐 if ((this.CausesValidation && (this.Page.GetValidators(this.ValidationGroup).Count > 0)) || !string.IsNullOrEmpty(this.PostBackUrl)) 源代码网推荐 { 源代码网推荐 this.Page.RegisterWebFormsScript(); 源代码网推荐 } 源代码网推荐 } 源代码网推荐 } 源代码网推荐 源代码网推荐 以上只是把自己在学习工作过程中的一些体会记录如此,防止自己忘记了。 源代码网推荐 也望和大家多多交流! 源代码网推荐 源代码网推荐 http://www.cnblogs.com/maplye/archive/2006/08/29/489338.html 源代码网推荐 源代码网推荐 源代码网推荐 源代码网推荐 做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。 源代码网推荐 源代码网供稿. |
