开发者

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. Almost everything I want done is working except one little issue. I need to use Javascript to Format Date and Currency fields. The Client wants DateTime fields to be mm/dd/yyyy and currency have $ and commas where appropriate.

This is easy enough in javascript on a regular aspx page. 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.

Well, the WebPArt loads and the Javascript doesn't fire... so I click refresh and it does fire. Can anyone help me get the code working on the inital WebPart Load? Has anyone been able to get server side script to work this way in SharePoint?

Thanks, Mike V


For this, you can take advantage of _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:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜