Pls Help: Datagrid the solution?

V

VB Programmer

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.
 
K

Karl

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="QuestionId"
AutoGenerateColumns="False">
<Columns>
<asp:BoundColumn DataField="Question" />
<asp:TemplateColumn>
<ItemTemplate>
<asp:textbox ID="txt" Runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
</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.Count - 1
Dim item As DataGridItem = grid.Items(i)
Dim questionId As Integer = CInt(grid.DataKeys(item.ItemIndex))
Dim answerBox As TextBox = CType(item.FindControl("txt"), TextBox)
If Not answerBox Is Nothing Then
Dim answer As String = answerBox.Text
'SAVEANSWER(questionId, 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_ItemDataBound(ByVal sender As Object, ByVal e As
DataGridItemEventArgs) Handles grid.ItemDataBound
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType =
ListItemType.AlternatingItem Then
Dim plc As PlaceHolder = CType(e.Item.FindControl("plc"),
PlaceHolder)
If Not plc Is Nothing Then
Dim dr As DataRowView = CType(e.Item.DataItem, DataRowView)
Select Case CInt(dr("type"))
Case 1
Dim txt As New TextBox
txt.Text = CStr(dr("value"))
plc.Controls.Add(txt)
Case 2
Dim ddl As New DropDownList
plc.Controls.Add(ddl)
End Select
End If
End If
End Sub


Hope that helps.
Karl
 
V

VB Programmer

AnswerBox.Text always returns "" even if there are values in it. Any ideas
why? Everything else seems ok.
 
K

Karl

You are rebinding your grid on postback.

Wherever you have your grid.DataSource = xxx and grid.Databind() commands,
wrap it in an if NOT Page.IsPostbackThen ... end if

karl
 
V

VB Programmer

Thanks so much Karl!!!!!!

Karl said:
You are rebinding your grid on postback.

Wherever you have your grid.DataSource = xxx and grid.Databind() commands,
wrap it in an if NOT Page.IsPostbackThen ... end if

karl

we'd
 

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,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top