0
好吧,我得到了一个我一直试图解决的独特问题两天。Sharepoint自定义WebPart启动脚本
我有System.Web.UI.WebControls.WebParts.WebPart控件我正在构建一个自定义Sharepoint视图。我想要做的几乎所有事情都在工作,除了一个小问题。我需要使用Javascript来格式化日期和货币字段。客户希望日期时间字段为mm/dd/yyyy,货币在适当的地方有$和逗号。
在一个普通的aspx页面上,这在javascript中很容易。我刚写的功能,在页面加载
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridFieldDAO dao = new GridFieldDAO();
myGrid.DataSource = dao.getItems();
myGrid.DataBind();
}
GetBuildFormattingScript();
}
private void GetBuildFormattingScript()
{
string script = "<script type=\"text/javascript\">";
script += " FormatByRows(\"" + myGrid.ClientID + "\",2);";
script += " FormatByRowsDate(\"" + myGrid.ClientID + "\",1);";
script += "</script>";
if(!ClientScript.IsClientScriptBlockRegistered("DoFormatting"))
ClientScript.RegisterStartupScript(typeof(string), "DoFormatting", script);
string script2 = " <script type=\"text/javascript\">"+
"var prm = Sys.WebForms.PageRequestManager.getInstance(); "+
"prm.add_beginRequest(BeginRequestHandler); "+
"prm.add_endRequest(EndRequestHandler); "+
"function BeginRequestHandler(sender, args) "+
"{ }"+
"function EndRequestHandler(sender, args) "+
"{ FormatByRows(\"" + myGrid.ClientID + "\",2); "+
" FormatByRowsDate(\""+myGrid.ClientID+"\",1);}</script> ";
if (!ClientScript.IsClientScriptBlockRegistered("DoUpdateFormatting"))
ClientScript.RegisterStartupScript(typeof(string), "DoUpdateFormatting", script2);
}
我的问题上,我想更新电网不存在的WebPart的onload ......所以我必须将代码添加到的OnPreRender。
好吧,WebPArt加载和JavaScript不会触发...所以我点击刷新,它会触发。任何人都可以帮助我获得有关初始WebPart Load的代码吗?有没有人能够获得服务器端脚本在SharePoint中以这种方式工作?
谢谢, 迈克V
套件, 感谢您的输入 - 我试过了 - 但似乎不工作......但这可能是因为我在错误的时间/地点添加它。我会将此代码添加到WebPart OnLoad或PreRender。我正在使用ScriptManager来注册这个脚本 - 但没有任何反应。 – MDV2000 2010-07-28 19:30:32
OnLoad或PreRender应该都可以(因为它只是被添加到控件集合中)。但是,脚本最终会在页面上出现的位置很重要。 _spBodyOnLoadFunctionNames在/_layouts/1033/init.js中,所以你的调用需要在加载init.js之后才能完成。我添加了一些代码,您可以在网页上的内容编辑器中尝试。试试看看你是否得到了Hello World警报。 – 2010-07-29 20:55:45
这篇Technet文章可能会给你一些见解。 http://social.technet.microsoft.com/wiki/contents/articles/15790.all-about-sharepoint-javascript-function-spbodyonloadfunctionnames.aspx您可以使用setTimeout在脚本的开头添加一个小延迟。 http://sharepointsolutions.blogspot.com/2007/07/adding-javascript-to-content-editor-web.html – MonkeyWrench 2013-05-03 23:47:15