Member_2_5230414
asked on
How to get values from textboxes
Hello,
I am using javascript to add text boxes when i click add button
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
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++;
});
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
do you mean that you want to get the value of your dynamic generated text-boxes values, in your code behind?
ASKER
yes thats it :)
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:
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:
javascript-arrays-via-jque ry-ajax-to -an-aspnet -webmethod
1. use aspnet hidden field control.
you add it in your aspx page:
<asp:HiddenField runat="server" ID="txtboxValues" Value='' />
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);
read it in your server side:JavaScriptSerializer js = new JavaScriptSerializer();
string [] txtboxValues = js.Deserialize<string>(this.txtboxValues.Value);
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"); }
});
check here to pass string array to server side via ajax:javascript-arrays-via-jque
ASKER
When im reading it on client side
how would i add it to my sql database
JavaScriptSerializer js = new JavaScriptSerializer();
string [] txtboxValues = js.Deserialize<string>(this.txtboxValues.Value);
how would i add it to my sql database
If u not find any solution, can u make a try on it
http://forums.asp.net/t/1325456.aspx
http://forums.asp.net/t/1220252.aspx
http://www.developerfusion.com/thread/53003/dynamically-adding-textboxes/
http://forums.asp.net/t/1325456.aspx
http://forums.asp.net/t/1220252.aspx
http://www.developerfusion.com/thread/53003/dynamically-adding-textboxes/
ASKER