编程语言
首页 > 编程语言> > asp.net – UpdatePanel启动脚本未执行

asp.net – UpdatePanel启动脚本未执行

作者:互联网

我正在编写一个ASP.NET webpart用于SharePoint站点并尝试使用UpdatePanel来呈现查询结果.我想使用JQuery插件来修改从异步回发返回的表,但是我无法在异步udpate上执行启动脚本.

我发现这篇文章有indicates that the UpdatePanel won’t do an eval() on startup scripts; instead, you must register the startup script block with the ScriptManager.这一切都有意义,直到它不起作用. MSDN reference documentation seems to concur采取了这种方法.

我的控制时间太长,无法完全发布,但这里是一个精简的表示,我认为它涵盖了任何相关内容.原谅我,如果下面的粘贴中缺少控件 – 我不得不删除一些部件,可能会有一些悬垂的触手,可以这么说.以下是webpart的代码,与加载用户控件(.ascx)的SmartPart不同.

如您所见,我正在使用ScriptManager.RegisterStartupScript方法.我尝试了两种重载;一次用于Page,一次用于更新面板中的ListView(重命名为“AspListView”).在这两种情况下,启动脚本都不会在异步更新上执行,而我却为此感到茫然.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;

namespace MyNamespace
{
    using AspListView = System.Web.UI.WebControls.ListView;

    [Guid("601b3bdb-ed2a-4ec8-8a40-c37de8ab048d")]
    public class ListSearch : StaticTemplateWebPart
    {
        private AspListView resultsList;

        public ListSearch()
        {
        }

        protected override void CreateChildControls()
        {            
            base.CreateChildControls();

            ScriptLink.Register(Page, "jquery-1.3.2.js", false);
            ScriptLink.Register(Page, "jquery-ui-1.7.2.custom.min.js", false);
            ScriptLink.Register(Page, "jquery.timepickr.js", false);
            ScriptLink.Register(Page, "jquery.quicksearch.js", false);

            string scriptBlock =
@"
if ($('table#discrepancy-results').length)
{
    $('table#discrepancy-results tr').quicksearch({
        position: 'before',
        attached: 'table.results',
        stripeRowClass: ['odd', 'even'],
        labelText: 'Keyword Search'
    });
}";
            ScriptManager.RegisterStartupScript(Page, typeof(Page), UniqueID, scriptBlock, true);

            /* adding other controls, getting references, databinding, etc. */

        }             

        void searchButt_Click(object sender, EventArgs e)
        {           
            if (Page.IsPostBack)
            {
                var beginDT = DateTime.Parse((beginDateText.Text ?? "") + " " + (beginTimeText.Text ?? ""));
                var endDT = DateTime.Parse((endDateText.Text ?? "") + " " + (endTimeText.Text ?? ""));
                var dataList = SPContext.Current.Web.Lists["MyDataList"].Items;

                var results = SearchListItems(dataList, beginDT, endDT, keywordText.Text ?? "");
                if (results.Count > 0)
                {
                    resultsList.DataSource = results;
                    resultsList.DataBind();
                }
            }
        }
    }
}

并且加载的用户控件:

<%@ Control Language="C#" ClassName="ListSearchControl" %>

<% if (false)
   { %>
    <script src="../../LAYOUTS/jquery-1.3.2-vsdoc2.js" type="text/javascript"></script>    
<% } %>

<script type="text/javascript">
    $(function() {
        $('id').trigger('click');
        $('#<%= BeginTime.ClientID %>').timepickr({
            handle: '#<%= BeginTimeTrigger.ClientID %>',
            convention: 12,
            trigger: 'nothing' 
        });
        $('#<%= EndTime.ClientID %>').timepickr({
            handle: '#<%= EndTimeTrigger.ClientID %>',
            convention: 12,
            trigger: 'nothing'
        });        
    });
</script>

<asp:Panel ID="ControlPanel" runat="server">
    <asp:Panel ID="Inputs" runat="server">
        <asp:Panel CssClass="DateInputWrapper" runat="server">
            <asp:Panel CssClass="BeginDateInput" runat="server">
                <asp:Label Text="Begin Date: "  runat="server" />   
                <asp:TextBox ID="BeginDate" Columns="14" runat="server"></asp:TextBox>     
                <asp:Image ID="BeginDateImg" ImageUrl="/_layouts/Images/calendar.gif" runat="server" />
                <ajax:CalendarExtender ID="BeginDateExtender" TargetControlID="BeginDate" PopupButtonID="BeginDateImg" 
                                       Format="MMMM d, yyyy" runat="server">
                </ajax:CalendarExtender>
                <asp:Label Text="Begin Time: " runat="server" />
                <asp:TextBox ID="BeginTime" Columns="6" Text="04:00 am" runat="server"></asp:TextBox>
                <asp:Image ID="BeginTimeTrigger" runat="server" ImageUrl="/_layouts/1033/Images/clock.png" />                        
            </asp:Panel>                    
            <asp:Panel CssClass="EndDateInput" runat="server">
                <asp:Label Text="End Date: " runat="server" />                        
                <asp:TextBox ID="EndDate" Columns="14" runat="server"></asp:TextBox>     
                <asp:Image ID="EndDateImg" ImageUrl="/_layouts/Images/calendar.gif" runat="server" />
                <ajax:CalendarExtender ID="EndDateExtender" TargetControlID="EndDate" PopupButtonID="EndDateImg" 
                                       Format="MMMM d, yyyy" runat="server">
                </ajax:CalendarExtender>
                <asp:Label Text="End Time: " runat="server" />
                <asp:TextBox ID="EndTime" Columns="6" Text="03:59 am" runat="server"></asp:TextBox>
                <asp:Image ID="EndTimeTrigger" runat="server" ImageUrl="/_layouts/1033/Images/clock.png" />
            </asp:Panel>   
        </asp:Panel>         
        <asp:Panel CssClass="Submit" runat="server">
            <asp:Button ID="SearchButton" Text="Search" runat="server" />
            <asp:Label CssClass="SearchStatusText" runat="server" />
        </asp:Panel>       
    </asp:Panel>
</asp:Panel>

<asp:Panel ID="ResultsPanel" runat="server">
    <asp:ListView ID="ResultsList" runat="server">
        <LayoutTemplate>            
            <table id="discrepancy-results">
                <tr class="header-row">                    
                    <th>Scheduled Date/Time</th>                   
                    <th>Code</th>     
                    <th>Description</th>               
                </tr>                
                <asp:PlaceHolder runat="server" ID="itemPlaceHolder" />
            </table>            
        </LayoutTemplate>
        <ItemTemplate>
            <tr class="result-row">                
                <td><%# Eval("ScheduledDate") %></td>                
                <td><%# Eval("Code") %></td>         
                <td><%# Eval("Description") %></td>
            </tr>
        </ItemTemplate>
    </asp:ListView>
</asp:Panel>

<asp:Panel ID="DetailsPanel" runat="server">
</asp:Panel>

解决方法:

我想补充一点,除非你使用UpdatePanel的ID和typeof(UpdatePanel),否则updatepanel startupscripts对我们不起作用.在updatepanel之外的其他地方使用startupscripts并不是那么挑剔.我们得到这样的工作:

ScriptManager.RegisterStartupScript(UpdatePanelId, typeof(UpdatePanel), "myScript",
                    / *
                      * Register a startup script to run 
                      * on the client after running this method on the server
                      * /
                    @" alert('Add your function to replace this.');", true);

Darin提供的解决方案也可以正常工作,但在我们的案例中,updatepanel具有更多功能,并且需要额外的逻辑来“跟踪”正在执行的操作,因此脚本不会在更新面板的每次更新时执行,而只是在某个事件之后.这样,脚本仅在此事件上执行.

标签:javascript,ajax,asp-net,updatepanel
来源: https://codeday.me/bug/20190717/1485078.html