简体   繁体   English

Sharepoint自定义WebPart启动脚本

[英]Sharepoint Custom WebPart Startup Script

Okay, I've gotten a unique issue I've been trying to solve for two days. 好的,我遇到了一个已经解决了两天的独特问题。

I have System.Web.UI.WebControls.WebParts.WebPart control I am building a custom Sharepoint View with. 我有System.Web.UI.WebControls.WebParts.WebPart控件,正在使用它构建自定义的Sharepoint视图。 Almost everything I want done is working except one little issue. 除了一个小问题,我几乎想做的所有事情都在工作。 I need to use Javascript to Format Date and Currency fields. 我需要使用Javascript来格式化日期和货币字段。 The Client wants DateTime fields to be mm/dd/yyyy and currency have $ and commas where appropriate. 客户希望DateTime字段为mm / dd / yyyy,并且货币在适当的地方带有$和逗号。

This is easy enough in javascript on a regular aspx page. 在常规aspx页面上的javascript中,这非常容易。 I just wrote the functions and on page load 我只是写了函数和页面加载

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);  
}

My issue in that on the OnLoad of the WebPart the grid I am wanting to update doesn't exists... so I have to add code to the OnPreRender. 我的问题在于WebPart的OnLoad上我要更新的网格不存在...所以我必须将代码添加到OnPreRender。

Well, the WebPArt loads and the Javascript doesn't fire... so I click refresh and it does fire. 好吧,WebPArt加载并且Javascript不启动...所以我单击刷新它就启动了。 Can anyone help me get the code working on the inital WebPart Load? 谁能帮助我让代码在初始WebPart Load上运行? Has anyone been able to get server side script to work this way in SharePoint? 有没有人能够让服务器端脚本在SharePoint中以这种方式工作?

Thanks, Mike V 谢谢,迈克五世

For this, you can take advantage of _spBodyOnLoadFunctionNames : 为此,您可以利用_spBodyOnLoadFunctionNames

string script = "<script type=\"text/javascript\">";   
script += " function FormatDataGridRows() {";
script += "    FormatByRows(\"" + myGrid.ClientID + "\",2);";   
script += "    FormatByRowsDate(\"" + myGrid.ClientID + "\",1);";   
script += " }";
script += " _spBodyOnLoadFunctionNames.push('FormatDataGridRows');";
script += "</script>";   

Edit To test, put the following code in a Content Editor web part on your page: 编辑要测试,请将以下代码放在页面上的Content Editor Web部件中:

<script type="text/javascript">
function SayHello() {
   alert('hello world!');
}
_spBodyOnLoadFunctionNames.push("SayHello");
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM