Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Page.ClientScript.RegisterClientScriptBlock not working

Tags:

c#

asp.net

I am trying to fire a pop up as shown below, but it is not working. Please help

public void btnSubmit_Click(Object o, EventArgs e)
{
    if (checkFileExists(Convert.ToString(fileInfo)))
    {
        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Msg", "<script type=\"text/javascript\"  language=\"javascript\">function showMsg(){return confirm(\"This image name already exists, do you want to replace it?\");}</script>", true);
        btnSubmit.OnClientClick = "return showMsg()";
    }
    if (something else)
    {
        // It does whatever is here but never pops the question above
    }
}

and on the button I have

 <asp:Button class="Button" ID="btnSubmit" CausesValidation="True" Text="SUBMIT" runat="server"
             OnClick="btnSubmit_Click"></asp:Button>
like image 550
user710502 Avatar asked Jun 30 '11 17:06

user710502


2 Answers

Another reason for Page.ClientScript.RegisterClientScriptBlock not working is when the page does not have a server side form e.g. <form id="form1" runat="server">... </form>

like image 178
user961954 Avatar answered Oct 17 '22 12:10

user961954


The last parameter you're sending to RegisterClientScriptBlock is true, which tells the method to wrap your script in a <script> block, but you're already doing that. It's probably not working, because it's rendering invalid (i.e. nested) script tags.

like image 30
FishBasketGordo Avatar answered Oct 17 '22 12:10

FishBasketGordo