我正在编写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>
答案 0 :(得分:6)
我想补充一点,除非您使用UpdatePanel's 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具有更多功能,并且需要额外的逻辑来“跟踪”正在执行的操作,因此在更新面板的每次更新时脚本都不会执行,但仅在某一事件发生后。这样,脚本只在此事件上执行。
答案 1 :(得分:4)
您可以尝试使用其他内容而不是使用ScriptManager.RegisterStartupScript
。注册UpdatePanel的end_request事件并执行您的jQuery脚本:
// This could also be done in jQuery's $(document).ready
function pageLoad() {
Sys.WebForms
.PageRequestManager
.getInstance()
.add_endRequest(endRequestHandler);
}
function endRequestHandler(sender, args) {
// Shouldn't the next test be:
// $('table#discrepancy-results').length > 0 ???
// and is it necessary at all? I suppose the quicksearch plugin
// won't apply on empty array
if ($('table#discrepancy-results').length) {
$('table#discrepancy-results tr').quicksearch({
position: 'before',
attached: 'table.results',
stripeRowClass: ['odd', 'even'],
labelText: 'Keyword Search'
});
}
}
答案 2 :(得分:1)
ScriptManager.RegisterStartupScript
使用参数 key ,您将其设置为控件的 UniqueID 。在执行页面脚本期间,此密钥必须是唯一的。所以,你应该改变这一行:
ScriptManager.RegisterStartupScript(Page, typeof(Page), UniqueID, scriptBlock, true);
与
ScriptManager.RegisterStartupScript(Page, typeof(Page), Guid.NewGuid(), scriptBlock, true);
或您希望生成唯一键的其他方式。
答案 3 :(得分:0)