views:

933

answers:

4

Hello,

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.

A: 

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

Dror
A: 

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.

Canavar
I've tried that, same result unfortunately.
Microserf
A: 

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

roman m
It's called from the server side button click event, since I have to do some server side checks before I throw up the alert box.
Microserf
+1  A: 

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