Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET Javascript
hi ,
 
i am unable to call a javascript function on a button click .
i am getting the following error

> Microsoft JScript runtime error: 'AddTextBox' is undefined
 

Please find the code below:

Source :
            <asp:Button ID="Button2"  runat="server"   Text="Add License" 
                    onClientClick="AddTextBox()" onclick="Button2_Click1" />
                    
                   <div id="TextBoxContainer">
                   <!----Textboxes will be added here--->
                </div>
                <script type="text/javascript">
 
                    function GetDynamicTextBox(value) {
 
                        return '<input name = "DynamicTextBox" type="text" value="' + value + '"/>' + '<input type="button" value="Remove" onclick="RemoveTextBox(this)"/>'
                    }
                     function AddTextBox(){
                           var div = document.createElement('DIV');
                           div.innerHTML = GetDynamicTextBox("");
                           document.getElementById("TextBoxContainer").appendChild(div);
                           }
                    function RemoveTextBox(div) {
                        document.getElementById("TextBoxContainer").removeChild(div.parentNode);
                    }
                    function RecreateDynamicTextBox(){
                        var values=eval('=Values');
                        if(values!=null){
                          var html="";
                          for(var i=0;i<values.length;i++){>
                             html+="<div>" + GetDynamicTextBox*(values[i] + "<div>";
                             }
                             document.getElementById("TextBoxContainer").innerHTML=html;
                             }
                         }
                         window.onload=RecreateDynamicTextBoxes;
                
                </script>
 
Code of button click:
protected void Button2_Click(object sender, EventArgs e)
{
    string[] textboxvalues = Request.Form.GetValues("DynamicTextBox");
    JavaScriptSerializer serializer = new JavaScriptSerializer();
    this.Values = serializer.Serialize(textboxvalues);
}
Please help and thanks in advance....
 
Thanks
Posted 15-Apr-13 22:25pm
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

It's a syntacticle error. Since you missed a close braces ')' here:
html+="<div>" + GetDynamicTextBox*(values[i] + "<div>";
Replace the line with following:
html+="<div>" + GetDynamicTextBox*(values[i] + "<div>");
And also onload you are calling window.onload=RecreateDynamicTextBoxes; whereas your function name is RecreateDynamicTextBox
 
--Amit
  Permalink  
v2
Comments
Sophia Ranjani Elango at 16-Apr-13 8:29am
   
@Amit
Thank u so much.
_Amy at 16-Apr-13 8:30am
   
Welcome. :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

I think, in onClick event you have called
onclick="Button2_Click1"
but there is no definition for the same.
 
So by changing the onClick event to
onclick="Button2_Click"
might work for you.
  Permalink  
Comments
Sophia Ranjani Elango at 16-Apr-13 8:46am
   
thank u

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 480
1 Maciej Los 290
2 Richard MacCutchan 225
3 BillWoodruff 185
4 Suraj Sahoo | Coding Passion 155
0 OriginalGriff 8,764
1 Sergey Alexandrovich Kryukov 7,437
2 DamithSL 5,639
3 Maciej Los 5,279
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web03 | 2.8.1411028.1 | Last Updated 16 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100