<%@ Page Language="C#" AutoEventWireup="true" CodeFile="managesurveys.aspx.cs" Inherits="managesurveys" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<script type="text/javascript">
	function newAttribute() {
		var d = document;

		// get field labels
		var lbl_attribute_new = document.getElementById('js_lbl_attribute_new').value;
		var lbl_attribute_del = document.getElementById('js_lbl_attribute_delete').value;
		var lbl_property_new = document.getElementById('js_lbl_property_new').value;
		var lbl_property = document.getElementById('js_lbl_property').value;
		var lbl_title = document.getElementById('js_lbl_title').value;


		var container = document.getElementById('attribute_container');
		var next_inc = container.getElementsByTagName('table').length + 1;
		var toolbar = "<a href='javascript:newAttribute();'><img src='http://localhost:49366/Surveys/add_button.png' alt='Question' width='20' height='20' border='0' hspace='5' align='absmiddle'/></a> <a href='javascript:deleteAttribute(" + next_inc + ")'><img src='http://localhost:49366/Surveys/delete_button.png' alt='Question' width='20' height='20' border='0' hspace='5' align='absmiddle' /></a>";
		var toolbar2 = "<a href='javascript:newProperty(" + next_inc + ")'><img src='http://localhost:49366/Surveys/add_button.png' alt='Answer' width='20' height='20' border='0' hspace='5' align='absmiddle' /></a>Answer ";

		if (next_inc % 2 == 0) {
			var dynamicClass = 'dynamicTableEven';
		} else {
			var dynamicClass = 'dynamicTableOdd';
		}

		var table = d.createElement('table');
		table.id = 'attributeX_table_' + next_inc;
		table.className = dynamicClass;

		var tbody = d.createElement("tbody");
		var tr = d.createElement('tr');
		var tr2 = d.createElement('tr');
		tr2.id = "attributeX_tr_" + next_inc + "_0";


		var td_01 = d.createElement('td');
		td_01.style.width = '15%';
		td_01.innerHTML = toolbar + lbl_title;

		var td_02 = d.createElement('td');
		td_02.colSpan = '2';
		td_02.align = 'left';
		td_02.innerHTML = '<input type="text" name="attributeX[' + next_inc + '][name]" value="" size="60"/>';

		var td_03 = d.createElement('td');
		td_03.colSpan = '2';
		td_03.align = 'left';
		td_03.innerHTML = toolbar2;

		var td_04 = d.createElement('td');
		td_04.style.width = '15%';
		td_04.innerHTML = '&nbsp;';

		var td_05 = d.createElement('td');
		td_05.style.width = '9%';
		td_05.align = 'left';
		td_05.innerHTML = lbl_property;

		var td_06 = d.createElement('td');
		td_06.style.width = '57%';
		td_06.align = 'left';
		td_06.innerHTML = "<input type='text' name='attributeX[" + next_inc + "][value][]' value='' size='40' /><a href='javascript:deleteProperty(" + next_inc + ",\"" + next_inc + "_0\");'><img src='http://localhost:49366/Surveys/delete_button.png' alt='answer' width='20' height='20' border='0' hspace='5' align='absmiddle' /></a>";

		table.appendChild(tbody);
		tbody.appendChild(tr);
		tr.appendChild(td_01);
		tr.appendChild(td_02);
		tr.appendChild(td_03);
		tbody.appendChild(tr2);
		tr2.appendChild(td_04);
		tr2.appendChild(td_05);
		tr2.appendChild(td_06);

		container.appendChild(table);
	}


	function deleteAttribute(attribute_id) {
		var container = document.getElementById('attribute_container');

		var table = document.getElementById("attributeX_table_" + attribute_id);

		container.removeChild(table);
	}


	function newProperty(attribute_id) {
		var d = document;

		// get field labels
		var lbl_property = document.getElementById("js_lbl_property").value;
		var table = document.getElementById("attributeX_table_" + attribute_id);
		var tbody = table.getElementsByTagName('tbody')[0];
		var tr_id = table.getElementsByTagName('tr').length + 1;

		// create new HTML elements
		var tr = d.createElement('tr');
		tr.id = "attributeX_tr_" + attribute_id + "_" + tr_id;

		var td_01 = d.createElement('td');
		td_01.style.width = '15%';
		td_01.innerHTML = '&nbsp;';

		var td_02 = d.createElement('td');
		td_02.style.width = '9%';
		td_02.align = 'left';
		td_02.innerHTML = lbl_property;

		var td_03 = d.createElement('td');
		td_03.style.width = '57%';
		td_03.align = 'left';
		td_03.innerHTML = "<input type='text' name='attributeX[" + attribute_id + "][value][]' value='' size='40'/><a href='javascript:deleteProperty(" + attribute_id + ",\"" + attribute_id + "_" + tr_id + "\");'><img src='http://localhost:49366/Surveys/delete_button.png' alt='answer' width='20' height='20' border='0' hspace='5' align='absmiddle' /></a>";


		// append new elements    
		tbody.appendChild(tr);
		tr.appendChild(td_01);
		tr.appendChild(td_02);
		tr.appendChild(td_03);


	}


	function deleteProperty(attribute_id, property_id) {
		var d = document;
		var table = document.getElementById("attributeX_table_" + attribute_id);
		var tbody = table.getElementsByTagName('tbody')[0];
		var tr = d.getElementById("attributeX_tr_" + property_id);

		tbody.removeChild(tr);
	}

	function SaveValues() {
		var e;
		var allvalues = new Array();
		for (i = 0; i < form1.elements.length; i++) {

			e = form1.elements[i];

			if (e.type == 'text') {
				alert('in');
				allvalues[i] = e.value;
			}
		}
		}

//			    alert('in');
//			    for (var i in form1.elements) {
//			        if (i.tagName=='INPUT')
//			            alert(form1.id);
//			    }
//	}
</script>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table width="97%" cellpadding="0" cellspacing="0">
          <tr>
		  <td colspan="2"> &nbsp;Survey Name:<input type="text" name="Survey Name:" id="survey name" value="" size="60" style="width: 400px; margin-left: 58px" /></td>
		  </tr>
		  <tr>
            <td width="79%" align="left" id="attribute_container"><input type="hidden" name="js_lbl_title" id="js_lbl_title" value="Question:" />
              <input type="hidden" name="js_lbl_property" id="js_lbl_property" value="Answer:" />
              <input type="hidden" name="js_lbl_property_new" id="js_lbl_property_new" value="New Property" />
              <input type="hidden" name="js_lbl_attribute_new" id="js_lbl_attribute_new" value="New Attribute" />
              <input type="hidden" name="js_lbl_attribute_delete" id="js_lbl_attribute_delete" value="Delete Attribute" />
              <%--<input type="hidden" name="js_lbl_price" id="js_lbl_price" value="Default Value" />--%>
               <table width="96%" class="dynamicTableOdd" id="attributeX_table_0">
                <tbody>
                  <tr>
                    <td width="15%"><a href="javascript: newAttribute(1)"><img src="http://localhost:49366/Surveys/add_button.png" alt="add question" width="20" height="20" border="0" hspace="5" align="absmiddle" /></a> Question:</td>
                    <td align="left" colspan="2"><input type="text"	name="attributeX[0][name]" value="" size="60" style="margin-right: 0px" /></td>
                    <td colspan="2" align="left"><a href="javascript: newProperty(0)"><img src="http://localhost:49366/Surveys/add_button.png" alt="add answer" width="20" height="20" border="0" hspace="5" align="absmiddle" /></a> Answer</td>
                  </tr>
                  <tr id="attributeX_tr_0_0">
                    <td width="15%" align="left">&nbsp;</td>
                    <td width="9%" align="left">Answer:</td>
                    <td align="left" width="57%"><input type="text"	name="attributeX[0][value][]" value="" size="40" /> </td>
                    
                  </tr>
                </tbody>
              </table>
			  
		    </td>
          </tr>
      </table>
	  <asp:Button ID="btnsave" runat="server" Text="Save" OnClientClick="SaveValues()" onclick="btnsave_Click" />
	  </div>
	  <input type="hidden" runat="server" id="hdn" />
    </form>
</body>

</html>
<script type="text/javascript">
	function SaveValues() {

		var e;
		var allvalues = new Array();
		var arrposition = 0;
		for (i = 0; i < form1.elements.length; i++) {

			e = form1.elements[i];

			if (e.type == 'text') {


				allvalues[arrposition] = e.value;
				arrposition = arrposition + 1;
			}

		}
		document.getElementById("<%=hdn.ClientID%>").value = allvalues;

	}
</script>

Dani AI

Generated

A more robust pattern is to stop trying to parse lots of ad-hoc input names on the server and submit a single structured payload instead. Build a JSON object on the client that represents the survey (survey name, list of questions, each question's list of answers), then send that JSON to the server (a hidden runat=server field or an AJAX WebMethod). JSON preserves the hierarchy, survives commas/special characters, and is trivial to deserialize in C#.

Example client-side pattern (collect question blocks, produce JSON, place it in a hidden field):

var survey = { Name: document.getElementById('txtSurveyName').value, Questions: [] };

document.querySelectorAll('.question-block').forEach(function(block) {
  var q = { Text: (block.querySelector('.qText')||{}).value || '', Answers: [] };
  block.querySelectorAll('.aText').forEach(function(a){ q.Answers.push(a.value || ''); });
  survey.Questions.push(q);
});

document.getElementById('hfSurveyJson').value = JSON.stringify(survey);

Server-side deserialize into plain C# types, then persist with a simple parent-child insert sequence inside a transaction:

public class Survey { public string Name { get; set; } public List<Question> Questions { get; set; } }
public class Question { public string Text { get; set; } public List<string> Answers { get; set; } }

protected void btnSave_Click(object sender, EventArgs e)
{
    var json = hfSurveyJson.Value;
    var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
    var survey = serializer.Deserialize<Survey>(json);

    // Persist: insert survey -> get surveyId -> insert questions (with surveyId) -> insert answers (with questionId)
    // Use a SqlTransaction or an ORM (EF) and parameterized commands.
}

Practical tips: insert the parent (Survey) first and obtain its identity (SCOPE_IDENTITY or EF SaveChanges), then insert questions and answers using that id. Use parameterized queries or an ORM to avoid SQL injection, wrap DB work in a transaction, validate lengths and required fields, and log errors. To debug, set a breakpoint in the save handler and inspect the JSON payload. This approach replaces brittle string-joining and manual parsing while making the server code straightforward and maintainable (recommendation for ).

Recommended Answers

All 2 Replies

whats your problem?

Search on google you gets of post regarding saving the data in database, then if you get any problem feel free to ask your problem

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.