skaleem1
asked on
Calling a Javascript function from codebehind button click event
Do you know what could be the reason the following call to javascript alert is not working from the codebehind button click event? Here is the code and debugging even takes me to the line of code in the if block but I do not see the alert prompted:
protected void btnAddCurrentPart_Click(ob ject sender, EventArgs e)
{
if (!blnCurrentPartCompatible )
{
ClientScriptManager script = Page.ClientScript;
if (!script.IsClientScriptBlo ckRegister ed(this.Ge tType(), "Alert"))
{
script.RegisterClientScrip tBlock(thi s.GetType( ), "Alert", "<script type=text/javascript>alert ('Hello there')</script>");
}
return;
}
}
protected void btnAddCurrentPart_Click(ob
{
if (!blnCurrentPartCompatible
{
ClientScriptManager script = Page.ClientScript;
if (!script.IsClientScriptBlo
{
script.RegisterClientScrip
}
return;
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I believe this only needs 1 argument, the script key: IsClientScriptBlockRegiste red("Alert ")
I see it also accepts 2, never mind.
ASKER
I even changed it to:
ClientScriptManager script = Page.ClientScript;
if (!script.IsClientScriptBlo ckRegister ed("Alert" ))
{
script.RegisterClientScrip tBlock(thi s.GetType( ), "Alert", "<script type=text/javascript>alert ('Hello there')</script>");
}
but still does not work. Please see that it is being called from a button click event. Is it an issue?
ClientScriptManager script = Page.ClientScript;
if (!script.IsClientScriptBlo
{
script.RegisterClientScrip
}
but still does not work. Please see that it is being called from a button click event. Is it an issue?
ASKER
I even tried it on the OnClientClick event but still does not work:
<asp:Button ID="btnAddCurrentPart" runat="server" Text="Add Current Part" Enabled="false" OnClientClick="javascript: CheckCompa tibility() ; return false;" OnClick="btnAddCurrentPart _Click" />
function CheckCompatibility(CheckCo mpatibilit yForCurren tPartInPor tFolio) //
{
alert(CheckCompatibilityFo rCurrentPa rtInPortFo lio);
}
<asp:Button ID="btnAddCurrentPart" runat="server" Text="Add Current Part" Enabled="false" OnClientClick="javascript:
function CheckCompatibility(CheckCo
{
alert(CheckCompatibilityFo
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
HainKurt,
I know that it works when I place this in the Page_onload event, however I have a logic in the button click event and I only want this to fire when the logic is not true, the complete code is as follows:
protected void btnAddCurrentPart_Click(ob ject sender, EventArgs e)
{
RetrieveDataGateway rdg = new RetrieveDataGateway();
bool blnCurrentPartCompatible = false;
blnCurrentPartCompatible = isCurrentPartCompatibleWit hPortfolio ();
if (!blnCurrentPartCompatible )
{
ClientScriptManager script = Page.ClientScript;
if (!script.IsClientScriptBlo ckRegister ed(this.Ge tType(), "Alert"))
{
script.RegisterClientScrip tBlock(thi s.GetType( ), "Alert", "<script type=text/javascript>alert ('Hello there')</script>");
}
return;
}
else
{
//Do whatever you want to process
}
}
I know that it works when I place this in the Page_onload event, however I have a logic in the button click event and I only want this to fire when the logic is not true, the complete code is as follows:
protected void btnAddCurrentPart_Click(ob
{
RetrieveDataGateway rdg = new RetrieveDataGateway();
bool blnCurrentPartCompatible = false;
blnCurrentPartCompatible = isCurrentPartCompatibleWit
if (!blnCurrentPartCompatible
{
ClientScriptManager script = Page.ClientScript;
if (!script.IsClientScriptBlo
{
script.RegisterClientScrip
}
return;
}
else
{
//Do whatever you want to process
}
}
If you put a breakpoint... does that condition [isCurrentPartCompatibleWi thPortfoli o()] ever evaluate to 'false'... which would make the if work...
It certainly does not look like the problem is coming from the javascript.
It certainly does not look like the problem is coming from the javascript.
ASKER
Yes it does and it even steps into the if block and through the line of code that triggers the javascript without any error...
Strange... does the javascript get emitted to the browser at all? If you View Source for the page... do you see the javascript: alert('Hello there')
Try changing this line:
script.RegisterClientScrip tBlock(thi s.GetType( ), "Alert", "<script type=text/javascript>alert ('Hello there')</script>");
TO:
script.RegisterClientScrip tBlock(thi s.GetType( ), "Alert", "alert('Hello there');", true);
Try changing this line:
script.RegisterClientScrip
TO:
script.RegisterClientScrip
can you also try this:
ScriptManager.RegisterClie ntScriptBl ock(btnSub mit, typeof(Button), "Saved", "alert('Hello there');", true);
ScriptManager.RegisterClie
if you do this
response.write("<script type=text/javascript>alert ('Hello there')</script>");
what happens ;)
response.write("<script type=text/javascript>alert
what happens ;)
ASKER
Thanks for all your help however the reason was something entirely different which I found out after extensive debugging and reviewing the html view. Long story short, the button btnAddCurrentPart is part of an ajaz based update panel that was causing not to fire the client side script before the postback. What I did was to force a postback in the page load event, something like this:
btnAddCurrentPart.OnClient Click = String.Format("fnClickOK(' {0}','{1}' ,'{2}','{3 }','{4}')" , btnAddCurrentPart.UniqueID , "", "AddCurrentPart", _User, "");
In the fnClickOK function, I do all my client side validation before forcing a postback so that my button click event code behind executes:
function fnClickOK(sender, e, Flag, UserLogin, IsPrivate)
{
//Do my client side validation here:
__doPostBack(sender,e);
}
and that solved the problem. The notable thing is how the Update Panel affects the postbacks unless you design the aspx page taking into considerations with all the important options this update panel comes with...
However, because some of you pointed me to the right direction, i.e, checking the html etc., I will split the points between two of you, thanks for your help anyways.
btnAddCurrentPart.OnClient
In the fnClickOK function, I do all my client side validation before forcing a postback so that my button click event code behind executes:
function fnClickOK(sender, e, Flag, UserLogin, IsPrivate)
{
//Do my client side validation here:
__doPostBack(sender,e);
}
and that solved the problem. The notable thing is how the Update Panel affects the postbacks unless you design the aspx page taking into considerations with all the important options this update panel comes with...
However, because some of you pointed me to the right direction, i.e, checking the html etc., I will split the points between two of you, thanks for your help anyways.
ASKER
Because you pointed me to the right direction, i.e, checking the html etc., I will split the points between two of you, thanks for your help anyways.