Link to home
Start Free TrialLog in
Avatar of theartha
thearthaFlag for United States of America

asked on

ASP.NET: Dynamically create textboxes in Javascript

I am trying to create dynamic text boxes in asp.net. When I type any number in the courselengthTexbox, i need to get that many classtimes textboxes using AJAX.    


   <tr id="CourseLengthTextField">
                              <th scope="row"><label>Course Length</label></th>
                              <td>
                        <asp:TextBox ID="CourseLengthTextBox" CssClass="textbox" runat="server" ></asp:TextBox>
                              </td>
                        </tr>

               
              <tr id="ClassTimesTextField">
                              <th scope="row"><label>Course Times</label></th>
                              <td>
                       
                       
                       
                              </td>
                        </tr>

Any advice.

Thanks.
Avatar of Alfredo Luis Torres Serrano
Alfredo Luis Torres Serrano
Flag of United States of America image

ASKER CERTIFIED SOLUTION
Avatar of Member_2_4913559
Member_2_4913559
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of theartha

ASKER

@ddayx10:

Thanks for the code.

Your solution is perfect for me. Unfortunately, there is on onChange in <asp:TextBox> and I used onTextChanged.

<asp:TextBox ID="CourseLengthTextBox" CssClass="textbox" runat="server" OnTextChanged="BuildClassTimes(this.value);"></asp:TextBox>


Error      27      ) expected      
Error      28      ) expected      
Error      29      Invalid expression term ')'
Error      30      Invalid expression term ')'

------------------------------
           <tr id="CourseLengthTextField">
                              <th scope="row"><label>Course Length</label></th>
                              <td>
                        <asp:TextBox ID="CourseLengthTextBox" CssClass="textbox" runat="server" OnTextChanged="BuildClassTimes(this.value);"></asp:TextBox>

                              </td>
                        </tr>
---------------------------------

Please advice.

Thanks.
@ddayx10:

If I remove the semi colon at OnTextChanged="BuildClassTimes(this.value)"

<asp:TextBox ID="CourseLengthTextBox" CssClass="textbox" runat="server" OnTextChanged="BuildClassTimes(this.value)"></asp:TextBox>

I got this error message:

program_ascx does not contain a defintion for 'BuildClassTimes'

Please advice.

Thanks.
OK...where to begin :)

1. Don't remove the semi-colon, it creates a syntax error in JavaScript if any other code is attached, and in this case there obviously is.

2. The onchange and OnTextChanged events are entirely different. The 1st is a JavaScript event the second is a server-side event. You can't replace one with the other the way you have it causes the error. If you have a server-side OnTextChanged event attached to your textbox then that is seperate. This works fine syntactically, but not so much toward your end goal.

Ex.

<asp:TextBox ID="CourseLengthTextBox" runat="server"
      onchange="BuildClassTimes(this.value);"
      ontextchanged="CourseLengthTextBox_TextChanged" />

This will fire both the JavaScript and build the inputs and then it will fire the server-side event assuming you've got something causing the postback (like a button or you applied the "autopostback" property to the textbox).

This will function without any errors. Serious!


3. Now your stated goal is at odds with your setup it seems. Why go through all the trouble of writing client-side stuff when you're just going to post back anyway?

a. What is causing the postback? For you to have an OnTextChanged event you have to have something that is posting the page back, what is it? If you are indeed "posting back" to the page to an "OnTextChanged" event (which I'm doubting see my notes below) then you're whole stated case is way different than your original question and you need to clarify what you are up to in that OnTextChanged event and why you need the specific JavaScript solution you originally asked for.

Because I can show you how to do that, but I seriously doubt its the right direction to go at that point. We could accomplish the same goal likely with a lot less effort and a lot more sense.

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
I am having a suspicion that what you were trying to communicate to me was that the Intellisense for Visual Studio was not showing you an "onchange" event for the asp:textbox control and so you chose the OnTextChanged event.

If this is the case. Stop! Use the event name I showed you "onchange". It doesn't show up in the Intellisense but it is still correct in this instance.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Thank You.