如何避免为每个控件实现更新面板控件? [英] How to avoid implementing update panel control for every control?

查看:69
本文介绍了如何避免为每个控件实现更新面板控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的表单中,我在单独的更新面板中设计了一些文本框,下拉菜单和提交和取消按钮,并且一些文本框和下拉列表都是在没有更新面板的情况下设计的,这些都是用jQuery设计的。



当我点击取消按钮时,它只清除updatepanel中的那些文本框和下拉列表。所以我在一个更新面板中接受了所有控件,但随后jQuery函数和代码停止工作。



现在我应该将更新面板带到每个控件吗?或者有任何其他程序?

解决方案

你的jQuery停止工作的原因是你的事件附加到的所有控件都被销毁,然后在updatepanel触发时重新创建。在附加活动时,您需要牢记这一点。



 <%@     Page    语言  =  C#     继承  =  WebTest.UpdatePanel    Codebehind   =  UpdatePanel.aspx.cs   %>  

< !DOCTYPE html PUBLIC - // W3C // DTD XHTML 1.0 Transitional // EN http://www.w3.org/TR /xhtml1/DTD/xhtml1-transitional.dtd\">

< ; html xmlns < span class =code-keyword> = http://www.w3.org/1999/xhtml >
< head runat = 服务器 >
< title > < / title >
< script src = // ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js\"> < / script > ;
< / head >
< body >
< script 类型 = text / C# runat = server >
protected void Page_Load( object sender,EventArgs e)
{
// ScriptManager.RegisterStartupScript(this,this.GetType(),resize,alert('x' );,true);
LabelTimeA.Text = LabelTimeB.Tex t = DateTime.Now.ToString();
}
< / script >

< 表格 id = form1 runat = server >
< asp:ScriptManager runat = server / >

< h2 > 使用jQuery < / h2 >
< p >
在这个例子中我们将鼠标悬停事件附加到时间标签。但是,updatepanel
会删除并替换UpdatePanel的内容。 jQuery事件附加到最初加载的
元素,但是它们已经被替换为没有附加jQuery事件的新的
元素,因此鼠标悬停不再是
在更新后工作。
< / p >
< asp:UpdatePanel ID = UpdateA runat = server UpdateMode = 条件 >
< ContentTemplate >
< asp:标签 ID = LabelTimeA runat = server / > < br / >
< asp:按钮 ID = ButtonUpdateA < span class =code-attribute> runat = serv呃 文字 = 更新 / >
< / ContentTemplate >
< / asp:UpdatePanel >

< script type = text / javascript >


document )。ready( function (){

' #<%= LabelTimeA.ClientID%>')。mouseover( function (){


In my form I designed some textboxes, dropdowns and submit & cancel buttons in separate update panels and some textboxes and dropdowns are designed without update panel,this all designed with jQuery.

When I am clicking cancel button it is clearing only those textboxes and dropdowns which are in updatepanel. So I took all controls in one update panel but then jQuery functions and codes stopped working.

Now should I take update panels to every single control? or have any other procedure?

解决方案

The reason your jQuery stops working is because all of the controls your events are attached to are destroyed then re-created when the updatepanel fires. You need to bear this in mind when attaching your events.

<%@ Page Language="C#"  Inherits="WebTest.UpdatePanel" Codebehind="UpdatePanel.aspx.cs" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
</head>
<body>
    <script type="text/C#" runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {
            //ScriptManager.RegisterStartupScript(this, this.GetType(), "resize", "alert('x');", true);
            LabelTimeA.Text = LabelTimeB.Text = DateTime.Now.ToString();
        }
    </script>

    <form id="form1" runat="server">
    <asp:ScriptManager runat="server" />

    <h2>Attach events using jQuery</h2>
    <p>
        In this example we attach mouseover events to the time label.  However the updatepanel
        removes and replaces the UpdatePanel's content.  The jQuery events were attached to the
        elements that were originally loaded, but they have gone and have been replaced with new
        elements that don't have the jQuery events attached, so the mouseover no longer
        works after the update.
    </p>
    <asp:UpdatePanel ID="UpdateA" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:Label ID="LabelTimeA" runat="server" /><br />
        <asp:Button ID="ButtonUpdateA" runat="server" Text="Update" />
    </ContentTemplate>
    </asp:UpdatePanel>

    <script type="text/javascript">


(document).ready(function () {


('#<%=LabelTimeA.ClientID %>').mouseover(function () {


这篇关于如何避免为每个控件实现更新面板控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆