Grabbing values of jquery generated html with c# -
i trying grab values of textboxs generate using jquery. textboxes added page 3 @ time. each 3 boxes represent item.
add creates 3 boxes size, price , color. seems can grab in form told request.form
gives namevaluecollection
somehow , please correct me if wrong values should stored in collection. cant seem understand. based on collection how find names of textboxes , there values?
jquery creates boxes:
<script type="text/javascript"> counter = 1; function foo() { $(".form").append('<div id=item' + counter + '><hr/><div class="inneritem"><p>size</p><input type="text" name="item" /><p>color</p><input type="text" name="item" /><p>price</p><input type="text" name="item" /></div>'); del = $("#item" + counter); del.append('<input type="button" class="remove" value="remove" id="' + counter + '"/>'); counter++; } $("body").on('click', '.remove', function () { var id = $(this).attr("id"); $("#item" + id).remove(); }); </script>
this have in codebehind. using code figure out how works.
protected void page_load(object sender, eventargs e) { if(ispostback) { namevaluecollection data; //load form variables namevaluecollection variable. data = request.form; // names of forms string array. string[] arr1 = data.allkeys; (int loop1 = 0; loop1 < arr1.length; loop1++) { response.write("form: " + arr1[loop1] + "<br>"); } } }
this codebehind generates:
form: __eventtarget
form: __eventargument
form: __viewstate
form: item
i thinking data want in item
. don't understand if right or how access in item
?
this 100% wrong. read info online , might not understand correctly.
first should give each textbox unique name, can tell them apart when posting.
$(".form").append('<div id="item' + counter + '"><hr/><div class="inneritem"><p>size</p><input type="text" name="item-size" /><p>color</p><input type="text" name="item-color" /><p>price</p><input type="text" name="item-price" /></div>');
you can arrays of these in code-behind using getvalues method
protected void page_load(object sender, eventargs e) { if (ispostback) { string[] sizes = request.form.getvalues("item-size"); string[] colors = request.form.getvalues("item-color"); string[] prices = request.form.getvalues("item-price"); } }
or can directly zip these arrays array of "items"
var items = request.form.getvalues("item-size") .zip(request.form.getvalues("item-color"), (s, c) => new { s, c }) .zip(request.form.getvalues("item-price"), (a, p) => new { size = a.s, color = a.c, price = p });
where each item have properties size, color , price.
Comments
Post a Comment