RegisterStartupScript not registering scripts with nested masterpage - javascript

void RegisterScript()
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "someScript", "alert('I was called from master page!')", true);
}
The above method is called form page_load event of masterpage (Assume Site.master).
Above method registers a script on the contentpage when the contentpage directly inherits the masterpage,
but no script is registered when masterpage is nested.
i.e the contentpage inherits a masterpage (assume view.master) and this masterpage (view.master) inherits the original masterpage (Site.master)
I have already tried ScriptManager.RegisterClientScriptBlock() and this.registerclientScriptBlock() ... how to solve this?

Related

Call Parent Javascript from Iframe Code behind

I checked Calling a parent window function from an iframe but it didn't work for me. I want to call parent page javascript function from Code behind of a page inside an iframe.
Page Inside Iframe Code
Protected Sub btnContinue_Click(sender As Object, e As System.EventArgs) Handles btnContinue.Click
ScriptManager.RegisterStartupScript(Me, Me.GetType(), "StartTimer", "parent.initializeClock();", True)
End Sub
It is not working. How should I call this from code behind. It must be called from code behind.

Loading external <Div></Div> to Iframe

In my project am using a master page named as master.aspx in particular page i need to include another page into the current page, so that i use <iframe> to achieve this. while passing the url to the iframe the master page will also load inside the <iframe> how can i avoid such loading of master page inside the iframe?
i can't avoid master page in the page which i called, is it possible to call particular <Div> or <content template> in iframe?
seems you are using asp.net, maybe you can try to edit the first row in .aspx page, try to add the following code to the page which you don't want to show the master page.
Theme="" StyleSheetTheme=""
==== Update ====
if you want to hide it in iframe only, you should pass a parameter in url like ?theme=none
then handle it in the Page_load Function in .aspx.cs as follow:
protected void Page_Load(object sender, EventArgs e)
{
if(Request["theme"] == "none"){
Page.Theme = "";
Page.StyleSheetTheme = "";
}
}

jQuery call error on Page_Load

I have the following code to register a javascript function on Page_Load (also tried it on Page_Init). The javascript switches two panels from hidden to shown based on a parameter on load of page:
protected void Page_Load(object sender, EventArgs e)
{
String switchAction = "<script language='javascript'>switchactionpanel(" + (int)((Global.upAction)Enum.Parse(typeof(Global.upAction), Global.ProfileAction.ToString())) + ")</script>";
Page.RegisterClientScriptBlock("switchaction", switchAction);
}
But when the page loads I am receiving an error: $ is not defined.
I looked in Firebug and the jQuery files are being loaded however, the first file that is being loaded in the .Net tab is the page itself. I know the jquery is correct as the same code works on a different page. Where should my RegisterClientScriptBlock be put in the page lifecycle to work correctly when the page loads? Or am I going about this all wrong?
You just need to ensure that the inserted script gets inserted after the JQuery reference.
Use RegisterStartupScript instead -- that inserts the script tag before </form> closing tag.
Not sure if this is relevant but I always use:
<script type="text/javascript"...
Sorry this should have been a comment rather than an answer.
I think it has to do with the register function your using. Try using RegisterStartupScript instead of RegisterClientScriptBlock.
Page.ClientScript.RegisterStartupScript(this.GetType(), "switchaction", "<script language='javascript'>switchactionpanel(" + (int)((Global.upAction)Enum.Parse(typeof(Global.upAction), Global.ProfileAction.ToString())) + ")</script>", false);

ScriptManager is inaccessible

I am trying to invoke the js function implemented on the aspx page from a .cs class. But, ScriptManager doesn't seem to exist in .cs class. Basically, the .cs file is a part of dll that I am using in my project. I need to invoke the js function that is implemented on the aspx page, from my .cs file present in the dll.
The js function is successfuly invoked from the aspx page, but when I try the same code in .cs file it says
ScriptManager is inaccessible due to its protection level.
here is code I am trying
protected void MyMethod()
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "key", "jsfunction();", true);
}
any ideas why the same code runs successfuly on the aspx page but not from a .cs class?
ScriptManager.RegisterStartupScript accept Page or Control as first argument. Make sure you pass your current Page to the cs method
protected void MyMethod(Page page)
{
ScriptManager.RegisterStartupScript(page, typeof(UpdatePanel), new Guid().ToString() , "jsfunction();", true);
}
And call from aspx.cs page using:
MyMethod(this.Page);
Well, to overcome the above issue I simply tried with this piece of code
System.Web.UI.ScriptManager.RegisterStartupScript(this, this.GetType(), "key", "jsfunction();", true);
Notice the use of full namespace with ScriptManager.

Why is __doPostBack() not firing my bound load events?

Here's the situation: I have a variable number of dynamically created update panels on my page, so I thought I would write one method which handles all of the loading for each one.
My Updatepanel creation looks something like this:
Dim newUpdp As New UpdatePanel
newUpdatep.ChildrenAsTriggers = False
newUpdatep.UpdateMode = UpdatePanelUpdateMode.Conditional
newUpdatep.ID = Guid.NewGuid.ToString
AddHandler newUpdatep.Load, AddressOf updatep_load_method
updatep_Holder.ContentTemplateContainer.Controls.Add(newUpdp)
updatep_Holder.Update()
This creates the update panel and binds its load event to the method "updatep_load_method". This method is called as soon as the Updatepanel is inserted into the holder. The method code is as follows:
Private Sub updp_load_method(sender As UpdatePanel, e As System.EventArgs)
Dim div As New HtmlGenericControl("div")
div.InnerText = Date.Time.Now.ToString
sender.ContentTemplateContainer.Controls.Add(div)
sender.Update()
End Sub
A little later on I want to update the panel, and refresh the time. So I use the javascript __doPostBack method. According to Dave Ward, the __doPostBack method follows the full page postback lifecycle, so I figured the load event of my Update panel would be fired and that "updatep_load_method" would be called by that particular update panel...
Although the partial postback occurs, and other update panel's load events are called, my bound ones aren't. So what's happening here?
You need to recreate dynamic controls during Page_Init and have to use the same Id (so you cannot do a Guid.NewGuid.ToString() while recreating the control).
Otherwise, ASP.Net will not be able to locate your control and populate it with data from ViewState.
Here's a good article that explains more about dynamic controls.
Try implementing the RaisePostBackEvent method in your code:
protected override void RaisePostBackEvent(IPostBackEventHandler source, string eventArgument)
{
//call the RaisePostBack event
base.RaisePostBackEvent(source, eventArgument);
//add whatever code you need to execute on postback here
}

Categories

Resources