views:

39

answers:

1

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

+1  A: 

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>
Kit Menke
Kit, Thanks for the input - I tried is - but seems not to work... but that may be because I am adding it at the wrong time/place. Would I add this code to the WebPart OnLoad or PreRender. I am using the ScriptManager to reg this script - but nothing happens.
MDV2000
OnLoad or PreRender should both be OK (since it is just getting added to the control collection). However, it will matter where the script ends up on the page. _spBodyOnLoadFunctionNames is in /_layouts/1033/init.js so your call will need to come after init.js is loaded. I added some code you can try in a content editor on your page. Try it out and see if you get a Hello World alert.
Kit Menke