JavaScript not getting triggered - javascript

I have problem with javascript. I have a master page (Page1.master) and Aspx page (Page2.aspx) when i redirecting from Page1.master to Page2.aspx, my Javascript function not getting triggered.
I have ScriptManager in both master and aspx page
In my master page, i have like this
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
In my aspx page, i have UpdatePanel in addition to ScriptManager.
It looks like
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="upp" runat="server" UpdateMode="Always">
//other controls
</asp:UpdatePanel>
Here My javascript function:
ClientScriptManager cs = Page.ClientScript;
Type cstype = this.GetType();
string csname = "MyScript";
if (!cs.IsClientScriptBlockRegistered(cstype, csname))
{
System.Text.StringBuilder js = new System.Text.StringBuilder();
js.Append("<script language=JavaScript>");
js.Append("function closeWindow() {");
js.Append("if(myWindow == null) {");
js.Append("var myWindow = window.open('', 'myWindow'); }");
js.Append("myWindow.close();");
js.Append("}");
js.Append("</script>");
cs.RegisterClientScriptBlock(cstype, csname, js.ToString());
}
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "CloseWindowScript", "closeWindow();", true);
Can anyone help me to make it work..
Thanks

Related

How to pass query string to popup window with javascript in asp.net?

How to pass query string to popup window with javascript in asp.net?
window.open("HoCCV.aspx?CSS=" + Request["CSS"].ToString(), "newWindow", "width=450,Height=300,addressbar=no,top=200, left=250");
You can pass like this in button click
<asp:Button ID="btnSubmit" Text="Submit" runat="server" OnClick="btnSubmit_Click" />
protected void btnSubmit_Click(object sender, EventArgs e)
{
string url = "HoCCV.aspx?Id=123&Name=Abc";
string script = "window.open('" + url + "', 'popup_window', 'width=1024,height=768,left=100,top=100,resizable=yes');";
ClientScript.RegisterStartupScript(this.GetType(), "popUp", script, true);
}
First Method
Url string
var strURL="yourfilepath?string1="+string1+"&string2="+string2+"&string3=+string3+&string4="+string4
Then use window.open()
var mywindow = window.open(strURL,'','width=690,height=1000,scrollbars=1,resizable=1,top=10,left=200');
Second Method
use can use hidden fileds
<asp:HiddenField ID="samplehiddenfield" runat="server" Value="hidden value"/>
onclick
onclick="window.open('filepath'+passValue(),'FeedbackWindow','width=960,height=640,scrollbars=yes,resizable=yes,status=yes')"
Then Javascript
<script type="text/javascript">
function passValue() {
var hidden_value = '?' + document.getElementById("<%= samplehiddenfield.ClientID %>").value;
return hidden_value;
}
</script>
<script type="text/javascript">
function OpenPopUp() {
var input = '<%= Request.QueryString["CSS"].ToString() %>';
window.open("HoCCV.aspx?CSS=" + input, "newWindow", "width=450,Height=300,addressbar=no,top=200, left=250");
}
</script>
<asp:Button ID="btnSubmit" Text="Submit" runat="server" OnClientClick="OpenPopUp();" />

AjaxManager not found...null

I have a User Control and a Content Page. In the Content Page, I have a RadDockLayout control inside Update panel like below. I have an Image button, Once user swaps the position of RadDock by Drag Drop. User presses save button to save the order in database.
<asp:updatepanel id="UpdatePanel1" runat="server">
<ContentTemplate>
<telerik:RadDockLayout runat="server" OnLoadDockLayout="RadDockLayout1_LoadDockLayout"
ID="RadDockLayout1" OnSaveDockLayout="RadDockLayout1_SaveDockLayout">
<telerik:RadDockZone runat="server" Orientation="Horizontal" ID="HealthZone" Visible="false"
BorderStyle="None" Style="margin: 0;">
</telerik:RadDockZone>
<telerik:RadDockZone runat="server" Orientation="Horizontal" ID="RadDockZone1" BorderStyle="None"
Style="margin: 0; width:540px;">
</telerik:RadDockZone>
<telerik:RadDockZone runat="server" Orientation="Horizontal" ID="AdZone" Visible="false"
BorderStyle="None" Style="margin: 0;" Width="324px">
</telerik:RadDockZone>
</telerik:RadDockLayout>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="imgsave" EventName="Click" />
</Triggers>
</asp:updatepanel>
During the save process, I call another function. which is also under User Control.
public void RePopulateOverview(UserControl overviewuc)
{
RePopulated = true;
CurrentDockStates.Clear();
RadAjaxManager AjaxManager = RadAjaxManager.GetCurrent(this.Page);
AjaxManager.AjaxSettings.Clear();
AjaxManager.AjaxSettings.AddAjaxSetting(AjaxManager, overviewuc);
//InitiateAjaxRequest
RadScriptManager.RegisterStartupScript(this.UpdatePanel1,
this.GetType(), "OverviewReLoadScript" +
Guid.NewGuid().ToString().Replace("-", "a"),
"javascript:InitiateAjaxRequest();", true);
}
and below is the corresponding javaScript Function like below.
<script type="text/javascript">
function InitiateAjaxRequest() {
if ($find("<%= Telerik.Web.UI.RadAjaxManager.GetCurrent(this.Page).ClientID %>") != null)
$find("<%= Telerik.Web.UI.RadAjaxManager.GetCurrent(this.Page).ClientID %>").ajaxRequest();
else {
alert("AjaxManager not found..." + $find("<%= Telerik.Web.UI.RadAjaxManager.GetCurrent(this.Page).ClientID %>"));
}
}
</script>
My Issue is When I presses Save Button second time it crashes and Error Message is below.
AjaxManager not found...null
It should be like this.
var ID;
function InitiateAjaxRequest() {
if (ID == null) {
ID = $find("<%= Telerik.Web.UI.RadAjaxManager.GetCurrent(this.Page).ClientID %>");
ID.ajaxRequest();
}
else
ID.ajaxRequest();
}
Try this as a work-around:
<script type="text/javascript">
var ajaxMgrInstance = null;
function InitiateAjaxRequest() {
if (ajaxMgrInstance == null) {
ajaxMgrInstance == $find("<%= myRadAjaxManager.ClientID %>");
}
if (ajaxMgrInstance != null) {
ajaxMgrInstance.ajaxRequest();
}
else {
alert("AjaxManager not found..." + "<%= myRadAjaxManager.ClientID %>");
}
}
</script>
Meanwhile I'll try to better locate the root cause of the issue.
EDIT: Updated $find call to match syntax of Telerik's API documentation.

get_postBackElement() is always undefined

<asp:ScriptManager ID="ScriptManager1" ScriptMode="Release" runat="server">
</asp:ScriptManager>
<script type="text/javascript">
Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequestHandler);
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
function BeginRequestHandler(sender, args) {
var elem = args.get_postBackElement();
alert("begin " + elem.value);
}
function EndRequestHandler(sender, args) {
alert("end request handler");
}
</script>
This is my simple attempt to retrieve the postback element, triggered from my UpdatePanel. My update panel looks like this:
<asp:UpdatePanel ID="UpdatePanel_Project" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<asp:DropDownList ID="DropDownList_Project" runat="server">
</asp:DropDownList>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="DropDownList_Customer" />
</Triggers>
</asp:UpdatePanel>
I believe I have did everything correctly...
Any ideas?
You have to set the ClientIDMode property value of the control (the DropDownList_Customer drop down list in this case) to AutoID. Please view my reply here.
What is your postback trigger ? This control seems to be missing DropDownList_Customer
<asp:AsyncPostBackTrigger ControlID="DropDownList_Customer" />
I finally solved this pain, here's my solution.
Basically we just need to override Microsoft's _uniqueIDToClientID function so it doesn't ignore our Static client Ids on postback elements.
You'll just need to add the following code at the bottom of your page.
if (Sys.WebForms.PageRequestManager) {
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm._uniqueIDToClientID = function (uniqueID) {
var clientID = uniqueID.replace(/\$/g, "_");
if (document.getElementById(clientID)) {
return clientID;
}
var lastDollar = uniqueID.lastIndexOf("$");
if (lastDollar === -1) {
return clientID;
}
if (lastDollar+1 > uniqueID.length) {
return clientID;
}
var staticID = uniqueID.slice(lastDollar + 1);
return document.getElementById(staticID) ? staticID : clientID;
};
}
Now, get_postBackElement() in your BeginRequestHandler will no longer be undefined!
Just make sure our code is executed after Microsoft's MicrosoftAjaxWebForms.js because we are overriding its _uniqueIDToClientID function.
Note: My function always returns the default WebForms ClientID if the element exists on the page. Only if the element cannot be found on
the page, does it check to see if an element with a static ID exists
on the page. If it exists, the staticID is used, otherwise it
defaults back to the default WebForms ClientID.

Load a javascript function while page loads

Hi
I want to run a javascript function when the page loads. But as my page derives from the master page there is no form . The is my aspx file
<%# Page Title="" Language="VB" MasterPageFile="~/Site.Master" AutoEventWireup="false"
CodeFile="test3.aspx.vb" Inherits="test3" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="Server">
<script language="javascript">
var m_Names = new Array();
function LoadArray() {
PageMethods.Load_Array(onSucceeded, onFailed);
}
function onSucceeded(result, userContext, methodName) {
m_Names = result;
}
function onFailed(error, userContext, methodName) {
alert("An error occurred")
}
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="Server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
<asp:TextBox ID="txt_model_code" runat="server"></asp:TextBox><br />
<br />
<input type="button" value="db Function" /><br />
</asp:Content>
I want to run the LoadArray() function initialy when the page loads. This function is calling a pagemethod given in aspx.vb code file..
Partial Class test3
Inherits System.Web.UI.Page
<System.Web.Services.WebMethod()>
Public Shared Function Load_Array() As String()
Dim Model_Name_old As String()()
Dim mod_code As String()
Dim mod_name As String()
Dim cod_upper As Integer
//calling webservice that retunrs a jagged array
Dim ins As New localhost_insert_model.dbModel
Model_Name_old = ins.get_Model_Name("A")
mod_code = Model_Name_old(0)
mod_name = Model_Name_old(1)
Return mod_name
End Function
End Class
So how can i load the javascrip LoadArray() function onPageLoad in this scenario??
This one should work
<script language="javascript">
var m_Names = new Array();
window.onload = function ()
{
LoadArray();
}
.....your functions
</script>
I think you can use $document.ready() from jQuery.
If you don't need the whole page to have loaded completely you could just call it after you created the functions?
var m_Names = new Array();
function loadArray() {
PageMethods.Load_Array(onSucceeded, onFailed);
}
function onSucceeded(result, userContext, methodName) {
m_Names = result;
}
function onFailed(error, userContext, methodName) {
alert("An error occurred")
}
loadArray();
You can use this code:
Sys.Application.add_load(function(e) { LoadArray(); });
Is its name suggest, this is a page_load handler, which pretty much duplicates in functionality is server-side counter part.

Using ASP.NET Variable in Javascript inside UpdatePanel

I have a javascript function inside UpdatePanel. The variable should be updated on UpdatePanel partial postback.
Using the below code, the javascript variable jsonStringASPX will always have the initial value, regardless of what JsonString ASP.NET variable has.
<script type="text/javascript">
function pageLoad(){
LoadExpenseList();
}
</script>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always">
<ContentTemplate>
<script type="text/javascript">
function LoadExpenseList() {
var jsonStringASPX = <%= JsonString %>;
var jsonHeaderStringASPX = <%= JsonHeaderString %>;
//Works with the Strings
}
</script>
</ContentTemplate>
</asp:UpdatePanel>

Categories