Question

I'm trying to do the simplest thing, to show an alert popup through javascript from my code-behind of a user control (ascx.cs).

I've tried

protected void btnSave_Click(object sender, EventArgs e)
{
    ScriptManager.RegisterStartupScript(btnSave, GetType(), "uniqueID", "alert('hello');", true);
}

as well as

Page.ClientScript.RegisterStartupScript(GetType(), "uniqueID", "alert('hello');", true);

but nothing seems to work for me. The control is located within an RadAjaxPanel (telerik) in an ASPX page.

I'm missing something obvious here. Any ideas what?

EDIT: The javascript is not injected into the html source as far as I can see. I look for the ID and the actual alert statement in the html.

Was it helpful?

Solution 2

Ah, the problem was the Telerik RadAjaxPanel. I just had to set the EnableOutsideScripts to true on them, like so:

<telerik:RadAjaxPanel ID="ajaxpanel" runat="server" LoadingPanelID="ajaxLoadingPanel" EnableOutsideScripts="true">

And then I could use the following code:

ScriptManager.RegisterStartupScript(btnSave, GetType(), "uniqueID", "alert('hello');", true);

OTHER TIPS

Pass page's type to client script type parameter :

Page.ClientScript.RegisterClientScriptBlock(typeof(MyPage), 
    "myPostBackScript", script, true);

EDIT : Normally this code works, but I don't know how telerik's control effects the injected script. I think you should open HTML source of the page and try to find injected script. This will help us to solve your problem.

Do you have <form runat="server" in your page?

do you call if from onClienClick event of the button? whatever control you're using, make sure you're calling the js from a client-side event

also, take a look at this for ideas

EDIT

you cannot CALL js from the server, js is CLIENT-SIDE script. what you can do is to Register your script AFTER you do all the checking, and tie it to onload event. see this SO question for details

EDIT2

check out the last post on this page

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top