Solved

How to get values from textboxes

Posted on 2013-02-07
7
204 Views
Last Modified: 2013-03-07
Hello,

I am using javascript to add text boxes when i click add button

  $(".addButton").on("click", function () {
     


        var newTextBoxDiv = $(document.createElement('div'))
      .attr("id", 'TextBoxDiv' + counter);

        newTextBoxDiv.html(' <label class="alignme" for="repdist' + counter + '">Rep' + counter + ' - </label><label for="repdist' + counter + '">Distance:</label>' +
    ' <input  onkeypress="return IsNumber(event)"  name="repdist' + counter + '" type="text" id="repsit' + counter + '" style="width:30px;" />' +
   '  <select name="kmormile' + counter + '" id="kmormile' + counter + '"> ' +
     	'<option value="Miles">Miles</option>' +
        	'<option value="Km">Km</option>' +
           ' </select>' +
                      ' <label for="repmin' + counter + '">Min:</label>' +
                   '  <input onkeypress="return IsNumber(event)"  name="repmin' + counter + '" type="text" value="00" maxlength="2" id="repmin' + counter + '" style="width:30px;" />' +

                      ' <label for="repsec' + counter + '">Sec:</label>' +
                     '<input onkeypress="return IsNumber(event)"  name="repsec' + counter + '" type="text" value="00" maxlength="2" id="repsec' + counter + '" style="width:30px;" />');

        newTextBoxDiv.appendTo("#TextBoxesGroup");


        counter++;
    });

Open in new window


The issue is im not really sure how i can loop through them to get the values output.

Has anyone got any ideas??

The working code is shown here : http://jarrattperkins.com/training/addtraining.aspx
0
Comment
Question by:runnerjp2005
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 38863357
in line 5 you code:
.attr("id", 'TextBoxDiv' + counter);

so basically you give your textbox unique id which can be used to retrieve its value:

for(var i=0;i<counter;i++){
var val = $('#TextBoxDiv'+i).text();
}

Open in new window

0
 

Author Comment

by:runnerjp2005
ID: 38863368
how would i do this in vb.net though rather then javascript
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 38863377
do you mean that you want to get the value of your dynamic generated text-boxes values, in your code behind?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:runnerjp2005
ID: 38863423
yes thats it :)
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 38863465
since the textboxes are not aspnet controls but generated on runtime in your client side, you have a few options:

1. use aspnet hidden field control.
you add it in your aspx page:
<asp:HiddenField runat="server" ID="txtboxValues" Value='' />

Open in new window

set its value in your client side:
//create json array
var txtboxValues = [];
for(var i=0;i<counter;i++){
txtboxValues.push($('#TextBoxDiv'+i).text());
}
//assign the hidden field
$("#<%: this.txtboxValues.ClientID %>").val(txtboxValues);

Open in new window

read it in your server side:
JavaScriptSerializer js = new JavaScriptSerializer();
string [] txtboxValues =  js.Deserialize<string>(this.txtboxValues.Value);

Open in new window


2. create web service and use ajax to pass the values to server side.
check here for guideline: Walkthrough: Creating and Using AJAX-Enabled Web Service
in your VS solution, aright click in your project and add Web Service.
add new service called:
[WebMethod]
public SendValues(List<string> values){
}

in your client side you call the web service like this:
var txtboxValues = [];
for(var i=0;i<counter;i++){
txtboxValues.push($('#TextBoxDiv'+i).text());
}
var jsonText = JSON.stringify({ list: txtboxValues});
     
    $.ajax({
      type: "POST",
     url: "WebService1.asmx/SendValues",
      data: jsonText,
      contentType: "application/json; charset=utf-8",
      dataType: "json",
     success: function() { alert("success"); },
     failure: function() { alert("failed"); }
  });

Open in new window

check here to pass string array to server side via ajax:
javascript-arrays-via-jquery-ajax-to-an-aspnet-webmethod
0
 

Author Comment

by:runnerjp2005
ID: 38864468
When im reading it on client side

JavaScriptSerializer js = new JavaScriptSerializer();
string [] txtboxValues =  js.Deserialize<string>(this.txtboxValues.Value);

Open in new window


how would i add it to my sql database
0
 
LVL 10

Expert Comment

by:Monica P
ID: 38870419
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question