jQuery call error on Page_Load - javascript

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);

Related

JavaScript function executing from browser console but not executing from Page.ClientScript.RegisterStartupScript C# method

I have a master page where I am importing a script file that has a function named ShowNotifyErrorFor(). This method is called from child pages that are using this master page when some error occurs during form validation. I am calling this function like this from C#:
double flatRate;
if (double.TryParse(flateRateTxt.Text, out flatRate) == false)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "ShowNotifyErrorFor", "ShowNotifyErrorFor(document.getElementById('" + flateRateTxt.ClientID + "'),'Invalid Rate')", true);
return;
}
When I call the same JavaScript function from Browser's console with same arguments then it is executing correctly. But it's not executing when called from C#.
The same code is working on pages that are not using any master page.
What I tried:
I checked if the page is rendering the form tag because one of the SO questions suggested this solution. But the form tag is being rendered.
I tried not to add script tags by passing false to RegisterStartupScript as the third argument. But it didn't work.
I checked if ClientScript object contains the rendered scripts and found that it has the script and I copied and pasted that script on the Browser console and it's working fine there.

Inserted JavaScript by usercontrol invokes only on first page

I have used a user control to get data from database and render it as MegaMenu anchors.
In code-behind file I have got data and rendered it by using a literal.
The megamenu.js file (containging myMega.Init method) has been inserted into header element
I have added following script tag into .ascx file.
<script type="text/javascript">
myMega.Init("mer_id", "anchor_id", "click");
</script>
I have several .aspx files which have same master page. The master page registers the User Control and contains following tag exact before .
<uc:MegaMenu id="anchors" runat="server"></uc:MegaMenu>
the problem is the myMega.Init only invokes when I go to firstpage.aspx. I have debugged by hitting f12 and choosing debugg Script. the init file invokes only first time (firstpage) not other pages. thank you for your help.
are mer_id and anchor_id elements' ids and what .Init does?
If you use jquery library you could put your code in ready event
<script type="text/javascript">
$(function() {
myMega.Init("mer_id", "anchor_id", "click");
});
</script>

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.

Trigger a page change based on a textbox edit

My problem is that, I want to call a code behind function from a JS. So that I can implement TextBox on Click event. I am NEW in JS so bear with me on this one...What I heard is that from the client side scripting you cannot call anything to the server (code behind). Here is my code:
<script type="text/javascript">
function callCodeBehind() {
<% txtAgentName_TextChanged(); %>
}
</script>
This is my Textbox:
<asp:TextBox ID="txtAgentName" runat="server" Onclick = "callCodeBehind()"></asp:TextBox>
This is my code behind:
protected void txtAgentName_TextChanged()
{
}
This function is getting fired on Page_Load(), which I don't want it to.
All I want is that to call this function txtAgentName_TextChanged() when user clicks on txtAgentName textbox.
Help!
For simplicity, you can use page methods. Check below articles for exact instructions.
Using Page Methods in ASP.NET AJAX
Using jQuery to directly call ASP.NET AJAX page methods
I think what you are looking for is AJAX. Check out the sample in the link.
You CAN call code behind from server using AJAX and jQuery library, just google it. e.g.
http://blogs.sitepoint.com/ajax-jquery/
A second issue is that the btn.click() will not work cross-browser.
you can insert a code behind method like this:
<% method(); &>

RegisterClientScriptBlock ASP.net

I want to execute alert when user clicks LinkButton1.It is not working?
protected void LinkButton1_Click(object sender, EventArgs e)
{
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "abc", "<script language=javascript>alert(hi)</script>");
}
You mistyped the function name. Change aler to alert. Also, you forgot the delimiters around the string that you are trying to alert.
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "abc", "alert('hi');", true);
use the below
<linkButton runat="server" id="lnk1" OnClientClick="alert('hi');" />
I hope your sample doesn't have a typo, but you are calling "aler" instead of "alert"!
Also, this doesn't define that your button would run this script when the user clicks the link button, it'll be executed when the page gets loaded.
For having such behavior, you need to use the "OnClientClick" control's property, and set there the name of the function - event handler - that would do the alert.
linkButton1.OnClientClick = "myEventHandler";
And define your script in some JavaScript file or by registering a client script block during the pre-render event of your container control or page.

Categories

Resources