The grid is a good candidate for this sort of work. Let's assume your data
has a "QuestionId ", and "Question" field, your datagrid would look something
like:
<asp:DataGrid id="grid" runat="server" DataKeyField="Q uestionId"
AutoGenerateCol umns="False">
<Columns>
<asp:BoundColum n DataField="Ques tion" />
<asp:TemplateCo lumn>
<ItemTemplate >
<asp:textbox ID="txt" Runat="server" />
</ItemTemplate>
</asp:TemplateCol umn>
</Columns>
</asp:DataGrid>
<asp:Button ID="Save" Runat="server" />
with a save button at the bottom. In your save button click code, you would
loop through each item of the grid and pull out the QuestionID (note that's
the DataKeyField of the grid) and the answer (which will be in teh control
"txt"):
For i As Integer = 0 To grid.Items.Coun t - 1
Dim item As DataGridItem = grid.Items(i)
Dim questionId As Integer = CInt(grid.DataK eys(item.ItemIn dex))
Dim answerBox As TextBox = CType(item.Find Control("txt"), TextBox)
If Not answerBox Is Nothing Then
Dim answer As String = answerBox.Text
'SAVEANSWER(que stionId, Answer)
End If
Next
Now, if you need to support more than just a textbox (ie, if that's dynamic
with the question), it gets a little more complicated. Basically, we'd add
a "TypeID" column to our data, we'd remove the textbox from our grid and put
a placeholder instead. Then, in the ItemDataBound event we'd do something
like:
Private Sub Grid_ItemDataBo und(ByVal sender As Object, ByVal e As
DataGridItemEve ntArgs) Handles grid.ItemDataBo und
If e.Item.ItemType = ListItemType.It em OrElse e.Item.ItemType =
ListItemType.Al ternatingItem Then
Dim plc As PlaceHolder = CType(e.Item.Fi ndControl("plc" ),
PlaceHolder)
If Not plc Is Nothing Then
Dim dr As DataRowView = CType(e.Item.Da taItem, DataRowView)
Select Case CInt(dr("type") )
Case 1
Dim txt As New TextBox
txt.Text = CStr(dr("value" ))
plc.Controls.Ad d(txt)
Case 2
Dim ddl As New DropDownList
plc.Controls.Ad d(ddl)
End Select
End If
End If
End Sub
Hope that helps.
Karl
"VB Programmer" <Do************ *****@jEmail.co m> wrote in message
news:%2******** **********@TK2M SFTNGP10.phx.gb l...
I have an ASP.NET form that allows people to answer survey questions.
There could be from 5 to an unlimited amt of questions, depending on which
questions the admin wants to users to answer (he sets it up previously).
The questions are in a db.
Is a datagrid the best way to display this data? What do you suggest?
After they enter the info I want to be able to save it back to the db.
Any helpful links/examples?
Thanks.