UpdatePanel控件使用注意事项

简介: 使用UpdatePanel控件应注意以下两个方面: 一、Rsponse.Write()是程序调试中的常见语句,但使用了UpdatePanel后,由于UpdatePanel中的控件触发的是异步调用过程,使用它就会报JavaScript错误。

使用UpdatePanel控件应注意以下两个方面:

 

一、Rsponse.Write()是程序调试中的常见语句,但使用了UpdatePanel后,由于UpdatePanel中的控件触发的是异步调用过程,使用它就会报JavaScript错误。(不能使用)

    解决办法:

         System.Web.UI.ScriptManager.RegisterStartupScript(btnQuery,this.GetType(),"msg1","alert('js脚本')",true);

         通过System.Web.UI.ScriptManager类的静态方法RegisterStartupScript向页面注册了页面加载完成后便执行的JS代码。

         public static void RegisterStartupScript(

                Control control,//正在注册该客户端脚本块的控件

                Type type, //通常使用typeof运算符或GetType()获取控件的类型

                string key, //该脚本块的惟一标识符

                string script,  //JavaScript内容

                bool addScriptTags //如果用<script></script>标记括起该脚本块,则为true,否则为false

        )

 

二、UpdatePanel的Visible属性要慎用

       一个页面中如果有多个UpdatePanel,但是在程序中可能会通过其他控件来控制UpdatePanel控件的显示与否,这时第一考虑到的就是使用UpdatePanel控件的Visible属性,如果此属性设置为false的话,UpdatePanle在Render成HTML代码时,并不会生成相应的HTML代码,也就是说,页面HTML的DOM中根本就没有这个UpdatePanel对应的对象。

        解决办法,通过设置UpdatePanel控件的Style属性,通过css中的display属性间接实现,由于UpdatePanel没有Style属性,所以在每个UpdatePanel控件的外面加上一个Panel控件。

        <style type="css/text">

             .hidden

             {

                  display:none;

             }

             .show         

            {

                  display:block;

             }

        </style>

 

       <asp:Panel ID="p1" runat="server">

             <asp:UpdatePanel ID="up1" runat="server">

                      <ContentTemplate>

                                 <asp:Label ID="Label1" runat="server" Text=""></asp:Label>

                      </ContentTemplate>

             </asp:UpdatePanel>

       </asp:Panel>

       <asp:Button ID="btnShow" runat="server" Text="显示"></asp:Button>

       <asp:Button ID="btnHide" runat="server" Text="隐藏"></asp:Button>

 

在Page_Load事件中

               this.btnShow.OnClientClick = " $get(' " + this.p1.ClientID + " ').className = 'show' ; return false " ;

               this.btnHide.OnClientClick = " $get(' " + this.p1.ClientID + " ').className = 'hide' ;  return false ";

 

说明:$get()是ScriptManager控件向客户端注册的一个JavaScript方法,相当于document.getElementById()方法。

相关文章
WPF项目中不支持 ResizingPanel,未在类型“ResizingPanel”中找到可附加的属性“ResizeWidth”
WPF项目中不支持 ResizingPanel,未在类型“ResizingPanel”中找到可附加的属性“ResizeWidth”
|
.NET
艾伟:Tip:自定义UpdatePanelTrigger
  大家在使用UpdatePanel的时候有没有遇到过这种情况呢?   页面上放置着3到4的UpdatePanel,它们互相独立,并且UpdateMode都是Condition,这意味着没有哪个UpdatePanel每次都会更新。
903 0
|
.NET 开发框架 前端开发