Sharepoint Custom WebPart Startup Script - javascript

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>

Related

Enabling Javascript Scripts in my C# code on IIS

I have some Javascript scripts in my ASP code that isn't working when I host it on Information. I tried enabling static content and setting Authentication to Anonymous Authentication. I don't have any separate JS files that I need to run, but .js is also enabled on MIME.
protected void GetDateCal_SelectionChanged(object sender, EventArgs e)
{
if (Request.QueryString["textbox"] != "")
{
string strScript = "<script>window.opener.document.forms[0].ctl00_ContentPlaceHolder1_" + Request.QueryString["textbox"].ToString() + ".value = '";
strScript += GetDateCal.SelectedDate.ToString();
strScript += "';window.opener.document.forms[0].submit();self.close();";
strScript += "</" + "script>";
Page.ClientScript.RegisterClientScriptBlock( this.GetType(), "Calendar_ChangeDate", strScript);
}
}
I would either use the "true" tag to automatic surround your js code with script tags.
And, any "" character in a c# string will be escaped, so use # for your strings.
eg:
string strScript = "<script>window.opener.document.forms[0].ctl00_ContentPlaceHolder1_" + Request.QueryString["textbox"].ToString() + ".value = '";
strScript += GetDateCal.SelectedDate.ToString();
strScript += "';window.opener.document.forms[0].submit();self.close();";
strScript += #"</" + "script>";
Page.ClientScript.RegisterClientScriptBlock( this.GetType(), "Calendar_ChangeDate", strScript);
or, consider dropping the script tags, and use this:
string strScript = "window.opener.document.forms[0].ctl00_ContentPlaceHolder1_" + Request.QueryString["textbox"].ToString() + ".value = '";
strScript += GetDateCal.SelectedDate.ToString();
strScript += "';window.opener.document.forms[0].submit();self.close();";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Calendar_ChangeDate", strScript,true);
So, "" in any string tends to be ignored, or escaped.
Use # for such strings, but probably better is to simple include the true tage in the Register script block, and it will surround the js code with script tags for you.

window.onload = function() is not showing complete msg

windows.onload() popup is not showing entire message in chrome. In explorer popup is showing full message. How to resolve this issue in Chrome?
tried in explorer popup is showing full msg . how to resole this issue in chrome
string script = "window.onload = function() { alert('" + message + "'); };";
ClientScript.RegisterStartupScript(this.GetType(), "alert", script, true);
protected void Page_load(object sender,EventArgs e)
{
string message="hii";
Response.Write("<script type='text/javascript'>alert('"+message+"');</script>");
}
Try this one...

bootbox.alert not working from code behind

I am importing data to DB from excel using bootstrap file uploader. After data import I need to show a message to user. So I have below code
string strScript = "bootbox.alert('" + Resources.Resource.Success_SaveUserInfo + "');";
Page.ClientScript.RegisterStartupScript(this.GetType(), "UserSave", strScript, true);
But it is not working. Page get freeze instead of alert. But if I use below code it work.
string strScript = "alert('" + Resources.Resource.Success_SaveUserInfo + "');";
Page.ClientScript.RegisterStartupScript(this.GetType(), "UserSave", strScript, true);
I have already added bootbox.min.js. So it is not issue.
You can do like this
string strScript = "<script type = 'text/javascript'>bootbox.alert('" + Resources.Resource.Success_SaveUserInfo + "');</script>";
Page.ClientScript.RegisterStartupScript(this.GetType(), "UserSave", strScript, true);
OR
Call javascript function it's highly recommended and reusable as well
Page.ClientScript.RegisterStartupScript(this.GetType(), "UserSave", CustomalertFunction('" + Resources.Resource.Success_SaveUserInfo + "'), true);
and write a javascript function
function CustomalertFunction(message)
{
bootbox.alert(message);
}

I want to run a new page on my website after the registration is finish

Wen I finish (successfully) the new user registration I want to change the current page in a new notification page from my code behind.
For this purpose I use a javascript:
<script type="text/javascript">
var Tag;
function notifyscript(TagName) {
tag = TagName;
var myTag = document.getElementById(tag);
myTag.click();
}
in order to call:
<a id="notify" href="http://localhost/Pages/Account/Notification.aspx" target="_parent"/>
But it trow me an error when I run this locally.
500 (HTTP Error 500.0 - Internal Server Error
The call fails LoadLibraryEx στο φίλτρο ISAPI "C:\Windows\Microsoft.NET\Framework\v4.0.30319\\aspnet_filter.dll"
When I run this in the web doing nothing.
Is there someone who knows how to do this action?
I mean to open a new page when the code behind ends the registration.
Please try this code on Code Behind:
string url="www.google.com";
string msg = "<script type='text/javascript'>" +
" $(document).ready(function(){ " +
" window.location.href='" + url + "';" +
" });" +
"</script>";
page.ClientScript.RegisterStartupScript(page.GetType(), "myURL", msg);

Retrieving Values from Form Using ASP and Javascript

I have an html form on a page that sends information to an .asp file. In the .ASP file, I am trying to write in the javascript version of asp to retrieve the information sent by the form. What is the code for doing this? I have tried Request.Querystring, but this does not work. I want to use the javascript version as that is what I am familiar with. Please do not answer this with the VBSCRIPT version.
Regardless of what language you use, the asp collection for form values is going to be querystring for GET and form for POST.
<%# language="Javascript" %>
<script language="Javascript" runat="server">
Response.Write("Script Engine: " + ScriptEngine() + " " +
ScriptEngineMajorVersion() + "." +
ScriptEngineMinorVersion() + " build " +
ScriptEngineBuildVersion());
var p1 = Request.QueryString('p1') + '';
if (p1 != 'undefined') {
Response.Write ("<br/><br/>you said: " + p1);
}
else {
Response.Write ("<br/><br/>If you append a query string param called 'p1', "+
"it will be echoed here.");
}
</script>

Categories