ASP.NET调用UpdatePanel的Update()方法
创始人
2024-04-18 12:21:04
0

ASP.NET中用编程的方法控制UpdatePanel的更新

UpdatePanel可以用来创建丰富的局部更新Web应用程序,它是ASP.NET 2.0 AJAX Extensions中很重要的一个控件。对于UpdatePanel,我们也可以使用编程的方法来控制它的更新,可以通过ScriptManager的RegisterAsyncPostBackControl()方法注册一个异步提交的控件,并且调用UpdatePanel的Update()方法来让它更新。再次用我在前面的文章中用到的一个无聊的时间更新例子来看一下,有时候我觉得例子过于复杂更加不好说明白所要讲的内容,如下代码所示,注意Button1并不包含在UpdatePanel中:

 

  1. <%@PageLanguage="C#"AutoEventWireup="true"CodeFile="Default
    .aspx.cs"Inherits="_Default"%> 
  2. <scriptrunatscriptrunat="server"> 
  3. voidButton1_Click(objectsender,EventArgse)  
  4. {  
  5. this.Label2.Text=DateTime.Now.ToString();  
  6. }  
  7. script> 
  8. <htmlxmlnshtmlxmlns="http://www.w3.org/1999/xhtml"> 
  9. <headrunatheadrunat="server"> 
  10. </FONT></STRONG>RefreshinganUpdatePanelProgrammatically<STRONG><FONT color=#006699></< SPAN>title></FONT></STRONG> <LI class=alt><STRONG><FONT color=#006699></< SPAN>head></FONT></STRONG> <LI class=""><STRONG><FONT color=#006699><body></FONT></STRONG> <LI class=alt><STRONG><FONT color=#006699><</FONT></STRONG><FONT color=#ff0000>formid</FONT><STRONG><FONT color=#006699>formid</FONT></STRONG>="form1"<FONT color=#0000ff>runat</FONT>="server"<STRONG><FONT color=#006699>></FONT></STRONG> <LI class=""><STRONG><FONT color=#006699><</FONT></STRONG><FONT color=#ff0000>asp:ScriptManagerID</FONT><STRONG><FONT color=#006699>asp:ScriptManagerID</FONT></STRONG>="ScriptManager1<BR>"<FONT color=#0000ff>runat</FONT>="server"<STRONG><FONT color=#006699>/></FONT></STRONG> <LI class=alt><STRONG><FONT color=#006699><div></FONT></STRONG> <LI class=""><STRONG><FONT color=#006699><</FONT></STRONG><FONT color=#ff0000>asp:UpdatePanelID</FONT><STRONG><FONT color=#006699>asp:UpdatePanelID</FONT></STRONG>="UpdatePanel1"<FONT color=#0000ff>runat</FONT>=<BR>"server"<FONT color=#ff0000>UpdateMode</FONT>=<FONT color=#0000ff>"Conditional"</FONT><STRONG><FONT color=#006699>></FONT></STRONG> <LI class=alt><STRONG><FONT color=#006699><ContentTemplate></FONT></STRONG> <LI class=""><STRONG><FONT color=#006699><</FONT></STRONG><FONT color=#ff0000>asp:LabelID</FONT><STRONG><FONT color=#006699>asp:LabelID</FONT></STRONG>="Label1"<FONT color=#0000ff>runat</FONT>="server"<FONT color=#ff0000>Text</FONT>=<BR><FONT color=#0000ff>"更新时间:"</FONT><STRONG><FONT color=#006699>></< SPAN>asp:Label></FONT></STRONG> <LI class=alt><STRONG><FONT color=#006699><</FONT></STRONG><FONT color=#ff0000>asp:LabelID</FONT><STRONG><FONT color=#006699>asp:LabelID</FONT></STRONG>="Label2"<FONT color=#0000ff>runat</FONT>="server"<FONT color=#ff0000>Text</FONT>=<BR>"Label"<FONT color=#0000ff>ForeColor</FONT>="Red"<STRONG><FONT color=#006699>></< SPAN>asp:Label><br/><br/></FONT></STRONG> <LI class=""><STRONG><FONT color=#006699></< SPAN>ContentTemplate></FONT></STRONG> <LI class=alt><STRONG><FONT color=#006699></< SPAN>asp:UpdatePanel></FONT></STRONG> <LI class=""><STRONG><FONT color=#006699><</FONT></STRONG><FONT color=#ff0000>asp:ButtonID</FONT><STRONG><FONT color=#006699>asp:ButtonID</FONT></STRONG>="Button1"<FONT color=#0000ff>runat</FONT>="server"<FONT color=#ff0000>Text</FONT>=<BR>"Button"<FONT color=#0000ff>OnClick</FONT>="Button1_Click"<STRONG><FONT color=#006699>/></FONT></STRONG> <LI class=alt><STRONG><FONT color=#006699></< SPAN>div></FONT></STRONG> <LI class=""><STRONG><FONT color=#006699></< SPAN>form></FONT></STRONG> <LI class=alt><STRONG><FONT color=#006699></< SPAN>body></FONT></STRONG> <LI class=""><STRONG><FONT color=#006699></< SPAN>html></FONT></STRONG> </LI></OL></PRE> <P>再次修改上面的例子,使用ScriptManager的RegisterAsyncPostBackControl()注册Button1为一个异步提交控件,并且调用UpdatePanel的Update()方法:</P> <P> </P><PRE><OL class=dp-xml><LI class=alt><STRONG><FONT color=#006699><</FONT></STRONG>%@<FONT color=#ff0000>PageLanguage</FONT>="C#"<FONT color=#0000ff>AutoEventWireup</FONT>="true"<FONT color=#ff0000>CodeFile</FONT>="Default.aspx.cs"<BR><FONT color=#0000ff>Inherits</FONT>="_Default"%<STRONG><FONT color=#006699>></FONT></STRONG> <LI class=""><STRONG><FONT color=#006699><</FONT></STRONG><FONT color=#ff0000>scriptrunat</FONT><STRONG><FONT color=#006699>scriptrunat</FONT></STRONG>=<FONT color=#0000ff>"server"</FONT><STRONG><FONT color=#006699>></FONT></STRONG> <LI class=alt>voidPage_Load(objectsender,EventArgse)  <LI class="">{  <LI class=alt>ScriptManager1.RegisterAsyncPostBackControl(Button1);  <LI class="">}  <LI class=alt>voidButton1_Click(objectsender,EventArgse)  <LI class="">{  <LI class=alt><FONT color=#ff0000>this.Label2.Text</FONT>=<FONT color=#0000ff>DateTime</FONT>.Now.ToString();  <LI class="">this.UpdatePanel1.Update();  <LI class=alt>}  <LI class=""><STRONG><FONT color=#006699></< SPAN>script></FONT></STRONG> <LI class=alt><STRONG><FONT color=#006699><</FONT></STRONG><FONT color=#ff0000>htmlxmlns</FONT><STRONG><FONT color=#006699>htmlxmlns</FONT></STRONG>=<FONT color=#0000ff>"http://www.w3.org/1999/xhtml"</FONT><STRONG><FONT color=#006699>></FONT></STRONG> <LI class=""><STRONG><FONT color=#006699><</FONT></STRONG><FONT color=#ff0000>headrunat</FONT><STRONG><FONT color=#006699>headrunat</FONT></STRONG>=<FONT color=#0000ff>"server"</FONT><STRONG><FONT color=#006699>></FONT></STRONG> <LI class=alt><STRONG><FONT color=#006699><title></FONT></STRONG>RefreshinganUpdatePanelProgrammatically<STRONG><FONT color=#006699></< SPAN>title></FONT></STRONG> <LI class=""><STRONG><FONT color=#006699></< SPAN>head></FONT></STRONG> <LI class=alt><STRONG><FONT color=#006699><body></FONT></STRONG> <LI class=""><STRONG><FONT color=#006699><</FONT></STRONG><FONT color=#ff0000>formid</FONT><STRONG><FONT color=#006699>formid</FONT></STRONG>="form1"<FONT color=#0000ff>runat</FONT>="server"<STRONG><FONT color=#006699>></FONT></STRONG> <LI class=alt><STRONG><FONT color=#006699><</FONT></STRONG><FONT color=#ff0000>asp:ScriptManagerID</FONT><STRONG><FONT color=#006699>asp:ScriptManagerID</FONT></STRONG>="ScriptManager1"<FONT color=#0000ff>runat</FONT>="server"<STRONG><FONT color=#006699>/></FONT></STRONG> <LI class=""><STRONG><FONT color=#006699><div></FONT></STRONG> <LI class=alt><STRONG><FONT color=#006699><</FONT></STRONG><FONT color=#ff0000>asp:UpdatePanelID</FONT><STRONG><FONT color=#006699>asp:UpdatePanelID</FONT></STRONG>="UpdatePanel1"<FONT color=#0000ff>runat</FONT>=<BR>"server"<FONT color=#ff0000>UpdateMode</FONT>=<FONT color=#0000ff>"Conditional"</FONT><STRONG><FONT color=#006699>></FONT></STRONG> <LI class=""><STRONG><FONT color=#006699><ContentTemplate></FONT></STRONG> <LI class=alt><STRONG><FONT color=#006699><</FONT></STRONG><FONT color=#ff0000>asp:LabelID</FONT><STRONG><FONT color=#006699>asp:LabelID</FONT></STRONG>="Label1"<FONT color=#0000ff>runat</FONT>="server"<FONT color=#ff0000>Text</FONT>=<FONT color=#0000ff>"更新时间:"</FONT><STRONG><FONT color=#006699>></< SPAN>asp:Label></FONT></STRONG> <LI class=""><STRONG><FONT color=#006699><</FONT></STRONG><FONT color=#ff0000>asp:LabelID</FONT><STRONG><FONT color=#006699>asp:LabelID</FONT></STRONG>="Label2"<FONT color=#0000ff>runat</FONT>="server"<FONT color=#ff0000>Text</FONT>="Label"<FONT color=#0000ff>ForeColor<BR></FONT>="Red"<STRONG><FONT color=#006699>></< SPAN>asp:Label><br/><br/></FONT></STRONG> <LI class=alt> <LI class=""><STRONG><FONT color=#006699></< SPAN>ContentTemplate></FONT></STRONG> <LI class=alt><STRONG><FONT color=#006699></< SPAN>asp:UpdatePanel></FONT></STRONG> <LI class=""><STRONG><FONT color=#006699><</FONT></STRONG><FONT color=#ff0000>asp:ButtonID</FONT><STRONG><FONT color=#006699>asp:ButtonID</FONT></STRONG>="Button1"<FONT color=#0000ff>runat</FONT>="server"<FONT color=#ff0000>Text</FONT>=<BR>"Button"<FONT color=#0000ff>OnClick</FONT>="Button1_Click"<STRONG><FONT color=#006699>/></FONT></STRONG> <LI class=alt><STRONG><FONT color=#006699></< SPAN>div></FONT></STRONG> <LI class=""><STRONG><FONT color=#006699></< SPAN>form></FONT></STRONG> <LI class=alt><STRONG><FONT color=#006699></< SPAN>body></FONT></STRONG> <LI class=""><STRONG><FONT color=#006699></< SPAN>html></FONT></STRONG> </LI></OL></PRE> <P>以上是ASP.NET调用UpdatePanel的Update()方法</P> <P>【编辑推荐】</P> <OL> <LI><FONT color=#0000ff>浅谈ASP.NET访问数据</FONT></LI> <LI><FONT color=#0000ff>ASP.NET运行时的处理流程</FONT></LI> <LI><FONT color=#0000ff>概述ASP.NET MVC与FubuMVC核心框架</FONT></LI> <LI><FONT color=#0000ff>ASP.NET framework的处理方法</FONT></LI> <LI><FONT color=#0000ff>浅析ASP.NET安全架构</FONT></LI></OL> <!--end::Text--> </div> <!--end::Description--> <div class="mt-5"> <!--关键词搜索--> </div> <div class="mt-5"> <p class="fc-show-prev-next"> <strong>上一篇:</strong><a href="/chengxu/15657.html">快速防护能否应对零日攻击?微软力推Fix-it技术</a><br> </p> <p class="fc-show-prev-next"> <strong>下一篇:</strong><a href="/chengxu/15659.html">iBATIS使用$和 ibatis面试</a> </p> </div> <!--begin::Block--> <div class="d-flex flex-stack mb-2 mt-10"> <!--begin::Title--> <h3 class="text-dark fs-5 fw-bold text-gray-800">相关内容</h3> <!--end::Title--> </div> <div class="separator separator-dashed mb-9"></div> <!--end::Block--> <div class="row g-10"> </div> </div> <!--end::Table widget 14--> </div> <!--end::Col--> <!--begin::Col--> <div class="col-xl-4 mt-0"> <!--begin::Chart Widget 35--> <div class="card card-flush h-md-100"> <!--begin::Header--> <div class="card-header pt-5 "> <!--begin::Title--> <h3 class="card-title align-items-start flex-column"> <!--begin::Statistics--> <div class="d-flex align-items-center mb-2"> <!--begin::Currency--> <span class="fs-5 fw-bold text-gray-800 ">热门资讯</span> <!--end::Currency--> </div> <!--end::Statistics--> </h3> <!--end::Title--> </div> <!--end::Header--> <!--begin::Body--> <div class="card-body pt-3"> <!--begin::Item--> <div class="d-flex flex-stack mb-7"> <!--begin::Symbol--> <div class="symbol symbol-60px symbol-2by3 me-4"> <div class="symbol-label" style="background-image: url('/static/assets/images/nopic.gif')"></div> </div> <!--end::Symbol--> <!--begin::Title--> <div class="m-0"> <a href="/chengxu/241137.html" class="text-dark fw-bold text-hover-primary fs-6">如何允许远程连接到MySQL数...</a> <span class="text-gray-600 fw-semibold d-block pt-1 fs-7">[[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...</span> </div> <!--end::Title--> </div> <!--begin::Item--> <div class="d-flex flex-stack mb-7"> <!--begin::Symbol--> <div class="symbol symbol-60px symbol-2by3 me-4"> <div class="symbol-label" style="background-image: url('/static/assets/images/nopic.gif')"></div> </div> <!--end::Symbol--> <!--begin::Title--> <div class="m-0"> <a href="/chengxu/61017.html" class="text-dark fw-bold text-hover-primary fs-6">如何利用交换机和端口设置来管理...</a> <span class="text-gray-600 fw-semibold d-block pt-1 fs-7">在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...</span> </div> <!--end::Title--> </div> <!--begin::Item--> <div class="d-flex flex-stack mb-7"> <!--begin::Symbol--> <div class="symbol symbol-60px symbol-2by3 me-4"> <div class="symbol-label" style="background-image: url('/static/assets/images/nopic.gif')"></div> </div> <!--end::Symbol--> <!--begin::Title--> <div class="m-0"> <a href="/chengxu/119031.html" class="text-dark fw-bold text-hover-primary fs-6">施耐德电气数据中心整体解决方案...</a> <span class="text-gray-600 fw-semibold d-block pt-1 fs-7">近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...</span> </div> <!--end::Title--> </div> <!--begin::Item--> <div class="d-flex flex-stack mb-7"> <!--begin::Symbol--> <div class="symbol symbol-60px symbol-2by3 me-4"> <div class="symbol-label" style="background-image: url('https://files.pic99.top/shayuweb/202409/eb2e03366045d9f.jpg')"></div> </div> <!--end::Symbol--> <!--begin::Title--> <div class="m-0"> <a href="/chengxu/139440.html" class="text-dark fw-bold text-hover-primary fs-6">20个非常棒的扁平设计免费资源</a> <span class="text-gray-600 fw-semibold d-block pt-1 fs-7">Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...</span> </div> <!--end::Title--> </div> <!--begin::Item--> <div class="d-flex flex-stack mb-7"> <!--begin::Symbol--> <div class="symbol symbol-60px symbol-2by3 me-4"> <div class="symbol-label" style="background-image: url('/static/assets/images/nopic.gif')"></div> </div> <!--end::Symbol--> <!--begin::Title--> <div class="m-0"> <a href="/chengxu/27164.html" class="text-dark fw-bold text-hover-primary fs-6">2009 IBM动态架构新动力...</a> <span class="text-gray-600 fw-semibold d-block pt-1 fs-7"></span> </div> <!--end::Title--> </div> <!--begin::Item--> <div class="d-flex flex-stack mb-7"> <!--begin::Symbol--> <div class="symbol symbol-60px symbol-2by3 me-4"> <div class="symbol-label" style="background-image: url('/static/assets/images/nopic.gif')"></div> </div> <!--end::Symbol--> <!--begin::Title--> <div class="m-0"> <a href="/chengxu/225.html" class="text-dark fw-bold text-hover-primary fs-6">通过 XML 进行内容发布</a> <span class="text-gray-600 fw-semibold d-block pt-1 fs-7"></span> </div> <!--end::Title--> </div> <!--begin::Item--> <div class="d-flex flex-stack mb-7"> <!--begin::Symbol--> <div class="symbol symbol-60px symbol-2by3 me-4"> <div class="symbol-label" style="background-image: url('/static/assets/images/nopic.gif')"></div> </div> <!--end::Symbol--> <!--begin::Title--> <div class="m-0"> <a href="/chengxu/130901.html" class="text-dark fw-bold text-hover-primary fs-6">德国电信门户网站可实时显示全球...</a> <span class="text-gray-600 fw-semibold d-block pt-1 fs-7">德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...</span> </div> <!--end::Title--> </div> <!--begin::Item--> <div class="d-flex flex-stack mb-7"> <!--begin::Symbol--> <div class="symbol symbol-60px symbol-2by3 me-4"> <div class="symbol-label" style="background-image: url('https://files.pic99.top/shayuweb/202503/5fe82cca6c5ff78.jpg')"></div> </div> <!--end::Symbol--> <!--begin::Title--> <div class="m-0"> <a href="/chengxu/246646.html" class="text-dark fw-bold text-hover-primary fs-6">为啥国人偏爱 Mybatis,...</a> <span class="text-gray-600 fw-semibold d-block pt-1 fs-7">关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...</span> </div> <!--end::Title--> </div> <!--begin::Item--> <div class="d-flex flex-stack mb-7"> <!--begin::Symbol--> <div class="symbol symbol-60px symbol-2by3 me-4"> <div class="symbol-label" style="background-image: url('https://files.pic99.top/shayuweb/202407/404021d01b8347f.jpg')"></div> </div> <!--end::Symbol--> <!--begin::Title--> <div class="m-0"> <a href="/chengxu/48727.html" class="text-dark fw-bold text-hover-primary fs-6">《非诚勿扰》红人闫凤娇被曝厕所...</a> <span class="text-gray-600 fw-semibold d-block pt-1 fs-7">【51CTO.com 综合消息360安全专家提醒说,“闫凤娇”、“非诚勿扰”已经被黑客盯上成为了“木...</span> </div> <!--end::Title--> </div> <!--begin::Item--> <div class="d-flex flex-stack mb-7"> <!--begin::Symbol--> <div class="symbol symbol-60px symbol-2by3 me-4"> <div class="symbol-label" style="background-image: url('/static/assets/images/nopic.gif')"></div> </div> <!--end::Symbol--> <!--begin::Title--> <div class="m-0"> <a href="/chengxu/133934.html" class="text-dark fw-bold text-hover-primary fs-6">2012年第四季度互联网状况报...</a> <span class="text-gray-600 fw-semibold d-block pt-1 fs-7">[[71653]]  北京时间4月25日消息,据国外媒体报道,全球知名的云平台公司Akamai Te...</span> </div> <!--end::Title--> </div> </div> <!--end::Body--> </div> <!--end::Chart Widget 35--> </div> <!--end::Col--> </div> </div> <!--end::Content container--> </div> <!--end::Content--> </div> <!--end::Content wrapper--> <!--begin::Footer--> <div id="kt_app_footer" class="app-footer"> <!--begin::Footer container--> <div class="app-container container-xxl d-flex flex-column flex-md-row flex-center flex-md-stack py-3"> <!--begin::Copyright--> <div class="text-dark order-2 order-md-1"> <span class="text-muted fw-semibold me-1">2025 ©</span> <a href="/" target="_blank" class="text-gray-800 text-hover-primary">鲨鱼网</a> <a href="https://beian.miit.gov.cn/" target="_blank" class="text-gray-800 text-hover-primary"></a> <a href="http://spbjmm.com.shayuweb.com">上品网</a><a href="http://www.zzszq.net/">深知网</a><a href="http://www.taiyangwa.net/">太阳生活网</a><a href="http://baike.taiyangwa.net/">太阳百科网</a><a href="http://ypkjmy.com.shayuweb.com/">一品科技</a><a href="http://www.yuansudz.com/news/">元素网</a><a href="http://www.xn--i6qw12a.com/">帛典网</a><a href="http://xldmm.com.shayuweb.com/">星链岛</a> </div> <!--end::Copyright--> <!--begin::Menu--> <ul class="menu menu-gray-600 menu-hover-primary fw-semibold order-1"> <li class="menu-item"> <a href="/news/" target="_blank" class="menu-link px-2">科技资讯</a> </li> <li class="menu-item"> <a href="/chengxu/" target="_blank" class="menu-link px-2">程序开发</a> </li> <li class="menu-item"> <a href="/sitemap.xml" target="_blank" class="menu-link px-2">sitemap</a> </li> </ul> <!--end::Menu--> </div> <!--end::Footer container--> </div> <!--end::Footer--> </div> <!--end:::Main--> </div> <!--end::Wrapper--> </div> <!--end::Page--> </div> <!--end::App--> <div id="kt_scrolltop" class="scrolltop" data-kt-scrolltop="true"> <!--begin::Svg Icon | path: icons/duotune/arrows/arr066.svg--> <span class="svg-icon"> <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"> <rect opacity="0.5" x="13" y="6" width="13" height="2" rx="1" transform="rotate(90 13 6)" fill="currentColor"></rect> <path d="M12.5657 8.56569L16.75 12.75C17.1642 13.1642 17.8358 13.1642 18.25 12.75C18.6642 12.3358 18.6642 11.6642 18.25 11.25L12.7071 5.70711C12.3166 5.31658 11.6834 5.31658 11.2929 5.70711L5.75 11.25C5.33579 11.6642 5.33579 12.3358 5.75 12.75C6.16421 13.1642 6.83579 13.1642 7.25 12.75L11.4343 8.56569C11.7467 8.25327 12.2533 8.25327 12.5657 8.56569Z" fill="currentColor"></path> </svg> </span> <!--end::Svg Icon--> </div> <!--begin::Javascript--> <script>var hostUrl = "/static/default/pc/";</script> <!--begin::Global Javascript Bundle(mandatory for all pages)--> <script src="/static/default/pc/plugins/global/plugins.bundle.js"></script> <script src="/static/default/pc/js/scripts.bundle.js"></script> <!--end::Global Javascript Bundle--> <!--end::Javascript--> </body> <!--end::Body--> </html>