Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi All,

 

i have an aspx form for the user to input, and those data will be stored

into the database.

 

I would like to build up the form dynamically by using the data in the a

database table, named question

 

for example, i have a question table and the data in the question table is

as follow

 

select * from question

 

---------------------------------

| question_data |

---------------------------------

| 1 + 1 = ? |

| 2 - 1 = ? |

| 2 + 2 = ? |

---------------------------------

 

 

Then, in my aspx page, the form will be as follow, (i assume that the form

is built dynamically)

 

(/* aspx Label here */) (/* aspx TextBox here */)

 

1 + 1 = ? |_____________|

2 - 1 = ? |_____________|

2 + 2 = ? |_____________|

 

And all the answer in the TextBox, would be saved into the database.

 

If i would like to add question for the form, just insert the question into

the question table

 

e.g.

 

I would like to add a question "how old are you?"

 

i just "insert into question (question_data) values ('how old are you?') "

 

Then, the aspx page will be

 

(/* aspx Label here */) (/* aspx TextBox here */)

 

1 + 1 = ? |_____________|

2 - 1 = ? |_____________|

2 + 2 = ? |_____________|

how old are you? |_____________|

 

 

So, my question is, how to build up the form dynamically like the above

example?

And what server control should i use?

And how can i pass the TextBox data to the backend codehind

 

Thank you.

 

Regards,

Angus

Posted

codebehind sux.

 

<%@ Assembly Name="HB" %>
<%@ Import Namespace="HB.Data.Helper"%>
<%@ Import Namespace="HB.Data.Collections"%>
<%@ Import Namespace="HB.Data"%>
<%@ Import Namespace="System.Collections.Specialized"%>
<%@ Page language="c#"%>
<script language="C#" runat="server">
private static readonly string IdListId = "8F7A4A5A-D319-4051-80EB-E5FF03D361E2";

void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
	CreatePage();
}

void ProcessReply(object sender, System.EventArgs e)
{
//display the responses.
NameValueCollection ids = (NameValueCollection) Session[idListId];
foreach(string id in ids)
{
	string value = Request.Form[id];
	if(value == "")
		continue;
	Response.Write(string.Format("{0}={1}<br>", ids[id], Request.Form[id]));
}
}

void CreatePage()
{
NameValueCollection ids = new NameValueCollection();
DbConnectionInfo connInfo = DbConnectionInfoCollection.Get("health benefits");
GenericDbCommandHelper cmd = connInfo.CreateCommand("SELECT id, question_data FROM question");
//get the asp.net form
HtmlForm form = (HtmlForm) FindControl("Form1");
using(GenericDbDataReader rdr = cmd.ExecuteReader())
{
	//for each record, add a question to the form.
	while(rdr.Read())
	{
		Label lbl = new Label();
		lbl.Text = rdr["question_data"].ToString();
		TextBox txtBx = new TextBox();
		txtBx.ID = rdr.GetGuid("id").ToString("N");
		ids[txtBx.ID] = lbl.Text;
		
		form.Controls.Add(lbl);
		form.Controls.Add(txtBx);
		form.Controls.Add(new LiteralControl("<br/>"));
	}
}
Session[idListId] = ids;
}
</script>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
	<title>Biznatch</title>
	<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
	<meta content="C#" name="CODE_LANGUAGE">
	<meta content="JavaScript" name="vs_defaultClientScript">
	<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body>
	<form id="Form1" method="post" runat="server">
		<asp:Button id="Button1" runat="server" Text="Submit" OnClick="ProcessReply"></asp:Button>
	</form>
</body>
</HTML>

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...