RegisterStartupScript is not working - javascript

i am new at ASP.NET and i got problem at using RegisterStartupScript.
i have one page with two UserControl. each UserControl have GridView that can display Detail Page, just like this.
here's the portion of my code:
SenderUserControl.ascx
<script type="text/javascript">
function ShowInsertFormSender() {
window.radopen("WebfrmManageMemo.aspx?RefType=S", "UserListDialog");
return false;
}
function refreshGridSender(arg) {
if (!arg) {
$find("<%= RadAjaxManager.GetCurrent(Page).ClientID %>").ajaxRequest("RebindSender");
}
else {
$find("<%= RadAjaxManager.GetCurrent(Page).ClientID %>").ajaxRequest("RebindAndNavigateSender");
}
}
function RowDblClickReceiver(sender, eventArgs) {
window.radopen("WebfrmManageMemo.aspx?RefType=S&MemoID=" + eventArgs.getDataKeyValue("MemoID"), "UserListDialog");
}
ReceiverUserControl.ascx
<script type="text/javascript">
function ShowInsertFormReceiver() {
window.radopen("WebfrmManageMemo.aspx?RefType=R", "UserListDialog");
return false;
}
function refreshGridReceiver(arg) {
if (!arg) {
$find("<%= RadAjaxManager.GetCurrent(Page).ClientID %>").ajaxRequest("RebindReceiverReferral");
}
else {
$find("<%= RadAjaxManager.GetCurrent(Page).ClientID %>").ajaxRequest("RebindAndNavigateReceiverReferral");
}
}
function RowDblClickReceiver(sender, eventArgs) {
window.radopen("WebfrmManageMemo.aspx?RefType=R&MemoID=" + eventArgs.getDataKeyValue("MemoID"), "UserListDialog");
}
DetailView.aspx
<script type="text/javascript">
function CloseAndRebindSender(args) {
GetRadWindow().BrowserWindow.refreshGridSender(args);
GetRadWindow().close();
}
function CloseAndRebindReceiver(args) {
GetRadWindow().BrowserWindow.refreshGridReceiver(args);
GetRadWindow().close();
}
function GetRadWindow() {
var oWindow = null;
if (window.radWindow) oWindow = window.radWindow; //Will work in Moz in all cases, including clasic dialog
else if (window.frameElement.radWindow) oWindow = window.frameElement.radWindow; //IE (and Moz as well)
return oWindow;
}
function CancelEdit() {
GetRadWindow().close();
}
</script>
DetailView.aspx.cs
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (Request["RefType"].ToString() == "S")
{
ScriptManager.RegisterStartupScript((sender as Control), GetType(), "closeSender", "CloseAndRebindSender('navigate');", true);
}
else if (Request["RefType"].ToString() == "R")
{
ScriptManager.RegisterStartupScript((sender as Control), GetType(), "closeReceiver", "CloseAndRebindReceiver('navigate');", true);
}
}
My problem is when i click button at DetailView.aspx, ScriptManager.RegisterStartupScript((sender as Control), GetType(), "closeSender", "CloseAndRebindSender('navigate');", true); is not working, but ScriptManager.RegisterStartupScript((sender as Control), GetType(), "closeReceiver", "CloseAndRebindReceiver('navigate');", true); is working perfectly.
i have search on stackoverflow about why RegisterStartupScript is not working and found this question, but i didn't see something is wrong with my code.
is there anything i miss?
Please help. Thank you

i've got the answer from this question.
the problem is with the javascript in Sender.ascx.
i don't know what's going on but i solved it.
Thank you

Look into using the Sys.Application.Load event when working with IScriptControls, as accessing them in earlier client-side events will give you null. Here is an article on the matter: http://www.telerik.com/help/aspnet-ajax/window-troubleshooting-javascript-from-server-side.html.

Related

Error: Unable to get property '_ScriptLoaderTask' of undefined or null reference

Why does Error: Unable to get property '_ScriptLoaderTask' of undefined or null referece get thrown when trying to close a RadWindow with ScriptManager and JavaScript in ASP? (Internet Explorer 11)
Our application has 'Save and Close' buttons that have the following C# code for the closing logic that is executed after the save has completed:
public void CloseWindow()
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "close",
"CloseModal()", true);
}
The .aspx page has the following JavaScript:
function CloseModal() {
var oWnd = GetRadWindow();
if (oWnd) {
oWnd.close();
}
}
function GetRadWindow() {
var oWindow = null;
if (window.radWindow) {
oWindow = window.radWindow;
} else if (window.frameElement &&
window.frameElement.radWindow) {
oWindow = window.frameElement.radWindow;
}
return oWindow;
}
Adding setTimeout() for one second before the RadWindow .close() function is called seems to fix the issue. I believe this allows the ScriptManager.RegisterStartupScript just enough time to complete execution.
The following JavaScript is a solution to the problem, and stops the error modal from displaying post 'Save and Close' button click:
function CloseModal() {
var oWnd = GetRadWindow();
if (oWnd) {
setTimeout(function () {
oWnd.close();
}, 1000);
}
}

Telerik RadMenu Click To Open and Close when clicked to next page

Hi im having problem with my radmenu. I want it to click to open and not open as you hover. I have set the clicktoopen = "true" already. Now the problem is when i click, it navigate to the respective requested page but the menu doesn't close.
<script type="text/javascript">
function OnClientItemClicking(sender, args) {
if (args.get_item().get_isOpen() == true) {
args.set_cancel(true);
args.get_item().close();
}
else {
}
}
</script>
<script type="text/javascript">
function OnClientMouseOverHandler(sender, eventArgs) {
if (eventArgs.get_item().get_parent() == sender) {
sender.set_clicked(false);
}
}
function onClientItemClicked(sender, args) {
}
</script>

Null reference from control after page loads

I am trying to run a piece of javascript after the page loads:
protected void Page_LoadComplete(object sender, EventArgs e)
{
if (IsPostBack)
{
string controlName = getPostBackControlName();
if (controlName == "btnSubmit" || controlName == "ddlSalary")
{
Page.ClientScript.RegisterStartupScript(GetType(), "myAlertScript", "onLoadDisplay(this)", true);
}
}
}
Here is the referenced javascript:
function onLoadDisplay(sender) {
PerDiemClicked(sender);
}
function PerDiemClicked(sender) {
if (document.getElementById('<%= chkbxPerDiem.ClientID %>').checked == true) {
document.getElementById("PerDiemDisplay").style.display = 'inline';
}
else {
document.getElementById("PerDiemDisplay").style.display = 'none';
}
}
I'm getting this error:
0x800a138f - JavaScript runtime error: Unable to get property
'checked' of undefined or null reference
I dont understand why the checked is coming back null because I am waiting for the page to complete postback before checking. How do I check this control and run the above code correctly?
edit: here is the checkbox:
<asp:CheckBox ID="chkbxPerDiem" runat="server" Checked="false" onclick="PerDiemClicked(this)" />
Your script likely executed before chkbxPerDiem was added to the DOM. Perform your script in the window load event:
window.addEventListener("load", function() {
onLoadDisplay(this);
});
Try this:
Page.ClientScript.RegisterStartupScript(GetType(), "myAlertScript", #"
window.addEventListener('load', function() {
onLoadDisplay(this);
});", true);
See this answer
Here is how I solved it.
This code does in fact post after page load:
protected void Page_LoadComplete(object sender, EventArgs e)
{
if (IsPostBack)
{
string controlName = getPostBackControlName();
if (controlName == "btnSubmit" || controlName == "ddlSalary")
{
Page.ClientScript.RegisterStartupScript(GetType(), "myAlertScript", "window.addEventListener('load', function() { onLoadDisplay(this); });", true);
}
}
}
I had to modify the javascript function to check if it exists before checking whether its checked:
function PerDiemClicked(sender) {
if (document.getElementById('<%= chkbxPerDiem.ClientID %>') != null) {
if (document.getElementById('<%= chkbxPerDiem.ClientID %>').checked == true) {
document.getElementById("PerDiemDisplay").style.display = 'inline';
}
else {
document.getElementById("PerDiemDisplay").style.display = 'none';
}
}
}
Thanks everyone.
Page_LoadComplete is a server-side event, and all server-side events will run before the page is even sent to the browser. Remember, these server-side events are related to constructing the page. You need to run your JavaScript after the page has been constructed, served, and fully loaded by the browser. This means the Page_LoadComplete event runs long before your JavaScript code can run.
Page.ClientScript.RegisterStartupScript(GetType(), "myAlertScript", "onLoadDisplay(this)", true);
The code above registers a call to your JavaScript load function. But it will insert the call to that function at the top of your page. So it will run before the rest of the page is loaded.
You need to review how ASP.NET events work. Again, all of the server-side events run before the page is even sent to the browser. The JavaScript shouldn't run until after the browser has received and loaded the page.

Javascript function and web service call synchronization

Hi i have this code in page_load event on aspx page:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
FillSomeDropDownLists();
}
EnableExportButton(false);
if (!string.IsNullOrEmpty(Page.Request["__EVENTTARGET"]) && !string.IsNullOrEmpty(Page.Request["__EVENTARGUMENT"]) && Page.Request["__EVENTARGUMENT"] == "true")
{
ScriptManager.RegisterStartupScript(Page, GetType(), "Show", "ShowPopup();", true);
if (CallToWebService(Convert.ToInt32(Page.Request["__EVENTTARGET"]), 1))
{
SomeOtherCallToWebService();
Global.ShowMessage(this.Page, StringConstants.SUCCESS_TRANSACTION_TITLE);
}
else
{
Global.ShowMessage(this.Page, StringConstants.FAIL_TRANSACTION_TITLE);
}
ScriptManager.RegisterStartupScript(Page, GetType(), "Hide", "HidePopup();", true);
}
}
I would like to show a javascript popup, then do webservice related stuff, and then hide the popup. That is how I coded it.
But in page execution I get call to webservices first, and then shows and hides the popup. Showing and hiding the popup is so fast that it seems nothing happens.
Is there solution to get what I want?

iscroll jquery breaks on partial postback

I have found a script called iScroll to use on iPad devices as a 1 figure scroll mechanism for a verticle list.
all it needs is:
<script type="text/javascript" src="http://cubiq.org/dropbox/iscroll4/src/iscroll.js"></script>
and the below:
var myScroll;
function loaded() {
myScroll = new iScroll('wrapper');
}
document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);
document.addEventListener('DOMContentLoaded', function () { setTimeout(loaded, 200); }, false);
which works fine when the page loads for the first time, however the list that uses this is within an asp.net upadate panel and the scroll breaks on postback.
I have tried registerstartupscript from my c# when the events that trigger the list refresh occur:
public string ipadScript()
{
StringBuilder sb = new StringBuilder();
sb.Append("var myScroll;" + Environment.NewLine);
sb.Append("function loaded() {" + Environment.NewLine);
sb.Append("alert('h');" + Environment.NewLine);
sb.Append("myScroll = new iScroll('wrapper');" + Environment.NewLine);
sb.Append("}" + Environment.NewLine);
sb.Append("document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);" + Environment.NewLine);
sb.Append("document.addEventListener('DOMContentLoaded', function () { setTimeout(loaded, 200); }, false);" + Environment.NewLine);
return sb.ToString();
}
ClientScriptManager cs = Page.ClientScript;
cs.RegisterClientScriptBlock(this.GetType(), "ipadKey", ipadScript(), true);
But this does not seem to work either. I don't even get the 'alert' I have added in the script to check the postback. Hopefully someone can help?
SOLVED!!!!
function pageLoad(sender, args) {
if (args.get_isPartialLoad()) {
myScroll = new iScroll('wrapper');
}
}
this will only work with asp.net update panels
The refresh method myScroll.refresh() should be invoked after updating of the update panel.
Client side:
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequestHandler);
function endRequestHandler(sender, eventArgs)
{
myScroll.refresh();
}
ClientScriptManager will just register the code, but since the <head> element isn't in the update panel, it never gets added, and even if it was wouldn't execute.
You'll need to register a client-side event handler to reinitialize the list:
$(document).ready(function() {
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequestHandler);
});
var endRequestHandler = function() { myScroll.refresh(); };

Categories

Resources