dynamic server control

A

angus

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
 
K

Karl Seguin

Keeping the example simple, like you have, it's really quite easy (code
isn't compiled, so there could be errors, just going off the top of my head
to give you ideas):

HTML

<asp:repeater id="questions" runat="server">
<HeaderTemplate>
<table border="0">
<HeaderTemplate>
<ItemTemplate>
<Tr>
<td><%# DataBinder.Eval(Container.DataItem, "Question_Data")
%><input type="hidden" id="questionId" runat="server" value='<#
DataBinder.Eval(Container.DataItem, "Question_ID")%>' /></td>
<td><asp:textbox id="answer" runat="server" ><asp:</td>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:repeater>
<asp:button id="Save" runat="server" Text="Save" />


CodeBehind

protected questions as Repeater
protected withevents save as Button

Sub Page_Load
questions.DataSource = GetAllQuestions()
questions.DataBind()
end sub

protected sub Save_OnClick(s as object, e as ComandEventArgs) Handles
Save.Click
for each item as RepeaterItem in Repeater.Items
dim answer as TextBox = ctype(item.FindControl("answer"), textbox)
dim questionId as HtmlInputHidden =
ctype(item.FindControl("questionId"), HtmlinputHidden)
if not answer is nothing andalso not questionId is nothing
SaveAnswerToDataBase(questionId.Value, answer.Text)
'fictisious function to save values
end if
next
end sub

publicshared function GetQuestions() as DataTable
dim cacheKey as string = "allQuestions"
dim dt as DataTable = ctype(HttpRuntime.Cache(cacheKey), DataTable)
if dt is nothing then
dim connection as new SqlConnection(CONNECTION STRING)
dim command as new SqlCommand("SELECT * FROM Question", connection)
dim da as new SqlDataAdapter(command)

try
connection.open()
da.fill(dt)
finally
connection.dispose()
command.dispose()
da.dispose()
end try

HttpRuntime.Cache.Insert(cacheKey, dt, null, DateTime.Now.AddHours(1),
TimeSpan.Zero)
end if
return dt
end function


Hope this sends you in the right direction.
Karl
 
A

angus

Hi Karl,

I get what you mean, and the problem solved.
Thank you so much

Regards,
Angus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top