Reputation:
I'm having some trouble getting the results of the drop-downs in a gridview, and haven't found any helpful articles for VB code. What I'm trying to do is build a site for tests. So far I have the gridview built w/the extra column for a drop-down list where the true/false answer will be selected. When the test is completed, there is a submit button. All is well except I need to be able to get the results of each drop-down list on post-back to a variable or array (each test contains 10 questions) so the results can be checked/graded. When the results are determined I would like to display a label in it's place and change the text value accordingly (correct or incorrect), so I'll need to be able to enumerate these as well (i.e. label1.text="Correct!", label2.text="Incorrect!", etc...).
Code so far.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1" BackColor="White" BorderColor="#3366CC"
BorderStyle="None" BorderWidth="1px" CellPadding="1">
<RowStyle BackColor="White" ForeColor="#003399" />
<Columns>
<asp:BoundField DataField="Question" HeaderText="Question"
SortExpression="Question" />
<asp:TemplateField HeaderText="Answer">
<ItemTemplate>
<% If Not IsPostBack Then%>
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlAnswer"
DataTextField="torf" DataValueField="torf">
</asp:DropDownList>
<%Else%>
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
<%End If%>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
<PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
<SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
<HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
</asp:GridView>
The preceding code loads 10 rows, 2 columns (column1:Question | column2-[DropDownLists]:Answer). I'm a freshman when it comes to development, so if you need additional info let me know.
Upvotes: 0
Views: 936
Reputation:
Here is how I handled it: created a page level private variable to hold our list of correct/incorrect values Private Answers as New List(Of Boolean) 'creates a strongly typed list of t/f values
in Page.Load
if IsPostBack then 'iterate through each row for each gridRow As GridViewRow in GridView1.Rows 'get the selected value for this answer Dim answer as string = CType(gridRow.FindControl("DropDownList1"),DropDownList).SelectedValue 'add this answer to the list Answers.Add(IsCorrectAnswer(answer)) next end if
the IsCorrectAnswer function determines whether or not the answer given for this question is correct and returns a Boolean value. You would need to write this function to suit your needs.
in Button1.Click handler
'rowCounter will act as an index to the answers Dim rowCounter as Integer = 0 For Each gridRow as GridViewRow in GridView1.Rows 'grid_RowDataBound handles the binding of single row grid_RowDataBound(gridRow, rowCounter) rowCounter+=1 Next
finally
Private Sub grid_RowDataBound(gridRow as GridViewRow, rowCounter as Integer) 'make the dropdown invisible CType(gridRow.FindControl("DropDownList1"),DropDownList).Visible = False 'because we'll be acting on two properties of the label, I shove it in a variable first for ease of access Dim label As Label = CType(gridRow.FindControl("Label1"),Label) 'set the label to visible label.Visible = True 'set the text label.Text = Iif(Answers(rowCounter),"Correct", "Incorrect") End Sub
Someone probably has a cleaner solution than this, but I do know this works, at least as far as I understand the issue you are facing. Good luck
Upvotes: 1
Reputation:
check the RowDataBound event of the gridview object. It takes two parameters: (byval sender as Object, byval e as GridViewRowEventArgs). With it, you can set the value of the label in each row to 'Correct' or 'Incorrect' on the postback.
see the example at http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx for more information
as for getting the answers into a variable, here are a couple of options
1) check viewstate. It has some overhead, so be cautious with it. 2) save the data to a Session object
Upvotes: 0
Reputation: 26436
What about binding the Visible attribute to Page.IsPostBack
(note this is in C# since I'm not familiar with the syntax for VB.NET... I'm sure something similar would work):
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlAnswer"
DataTextField="torf" DataValueField="torf" Visible='<%# !Page.IsPostBack %/>></asp:DropDownList>
<asp:Label ID="Label1" runat="server" Text="" Visible='<%# Page.IsPostBack %/></asp:Label>
</ItemTemplate>
Upvotes: 0