1

I used a javascript coding for confirm box and it shows 'javaScript Runtime error' while reading value of that textbox... Is there any alternate option for this Confirm box to work...

<script type="text/javascript">        
    function test() {
        var m = document.getElementById('<%=textbox1.ClientID%>').value;

        if (m != "") {
            var cBox = confirm("Are you sure want to add new user?");
            if (cBox == true)
                return true;
            else
                return false;
        }
    }
</script>

and I am calling it in code behind...

ScriptManager.RegisterStartupScript(Page, this.GetType(), "CallJS", "test();", true);
  • @ K3rnel31, Now the page shows server error. I'm sure that there will be an '=' in it... –  Nov 18 '13 at 12:59
  • [Here's a similar question with solution](http://stackoverflow.com/questions/6345484/passing-asp-net-client-ids-in-to-a-javascript-function) – Mark Nov 18 '13 at 13:20

1 Answers1

0

You can't do that, you are trying to get a clientId before bind the page.

By the way try something more clean

Add this block in your *.aspx

<asp:Content ID="Content3" ContentPlaceHolderID="JavaScript" Runat="Server">
    <asp:PlaceHolder ID="contentScript" runat="server" />
</asp:Content>

*.aspx.cs

string scriptJS = @"function test() {
                        var m = document.getElementById('" + textbox1.ClientID + "').value;

                        if (m != '') {
                            var cBox = confirm('Are you sure want to add new user?');
                            if (cBox == true)
                                return true;
                            else
                                return false;
                        }
                    }";

HtmlGenericControl script = new HtmlGenericControl("script");
script = new HtmlGenericControl("script");
script.Attributes.Add("type", "text/javascript");
script.InnerHtml = scriptJS;
contentScript.Controls.Add(script);

Please increase/refact this code with "string.Format".

Andrew Paes
  • 1,940
  • 1
  • 15
  • 20
  • What namespace should we add for this..? –  Nov 18 '13 at 13:26
  • If you are coding in C# web.forms, put this into Page_Load methods just to test. Before this you can test your function writing your calling after the function declaration. You don't need to declare a namespace, use the same aspx.cs namespace, puting the code inside the partial class. – Andrew Paes Nov 18 '13 at 13:32
  • Can you see the jsSript writed in you page when you see the source-code? Have you did a test with firebug console? Did you call the function? – Andrew Paes Nov 18 '13 at 14:55