Click here to Skip to main content
16,011,839 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 :
HTML
<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:
C#
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
v3

It's a syntacticle error. Since you missed a close braces ')' here:
JavaScript
html+="<div>" + GetDynamicTextBox*(values[i] + "<div>";

Replace the line with following:
JavaScript
html+="<div>" + GetDynamicTextBox*(values[i] + "<div>");

And also onload you are calling window.onload=RecreateDynamicTextBoxes; whereas your function name is RecreateDynamicTextBox

--Amit
 
Share this answer
 
v2
Comments
Sophia Ranjani Elango 16-Apr-13 8:29am    
@Amit
Thank u so much.
_Amy 16-Apr-13 8:30am    
Welcome. :)
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.
 
Share this answer
 
Comments
Sophia Ranjani Elango 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900