Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variable number of radio buttons in a gridview 1

Status
Not open for further replies.

chpicker

Programmer
Apr 10, 2001
1,316
0
0
I've been fighting ASP over this problem for a week now and I just can't figure out how to do it.

I'm using a GridView as an example. I don't strictly need to use it; I could use any of the other data display controls if you think that would work better. GridView is what I've been trying to use.

Let's say I'm creating an online test. The questions and answers are in a SQL Server table. Each question can have any number of answers, but only one answer is correct. So I populate the GridView with a Select statement that gives me one row per question. In the RowDataBound event, I perform another Select to get the answers to that row's question. What I need to figure out is: 1) How do I put these answers into radio buttons? 2) How do I read all of the answers at once when the user clicks Submit?

I'm seriously at a loss. I created a TemplateField with an <asp:RadioButtonList> control. I haven't been successful with using FindControl to get the RadioButtonList; I keep getting the "not set to an instance of an object" error.

Has anyone done what I'm trying to do? How do you populate each radio button set, and how do you read them all back after submission?

Ian
 
can you post some of the html of your gridview, and your RowDataBound event so we can see if there is something that jumps out to help you solve this issue?
 
Well, I was successful in getting the questions to display correctly. But I still can't figure out how to retrieve the selected answers. Here's what I have:

Default.aspx
Code:
<asp:GridView ID="gvQuestions" runat="server" AutoGenerateColumns="false">
  <Columns>
    <asp:TemplateField>
    <ItemTemplate>
      <asp:Label ID="lblQuestion" runat="server" Text='<%# eval("question") %>'></asp:Label>
      <asp:RadioButtonList ID="rdoAnswers" runat="server"></asp:RadioButtonList>
    </ItemTemplate>
    </asp:TemplateField>
  </Columns>
</asp:GridView>
<asp:Button id="btnSubmit" runat="server" Text="Submit" />
<asp:SqlDataSource ID="DSQuestions" runat="server" ConnectionString="<%$ ConnectionStrings:SqlServer %>"></asp:SqlDataSource>

Default.aspx.vb
Code:
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  Dim sql As String = "select questionid,question from testquestions order by questionid"
  DSQuestions.SelectCommand = sql
  gvSubEvents.DataSourceID = "DSQuestions"
End Sub

Protected Sub gvQuestions_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvQuestions.RowDataBound
  If e.Row.RowType = DataControlRowType.DataRow Then
    Dim drv As Data.DataRowView = e.Row.DataItem
    Dim sql As String = "select answerid,answer from testanswers where questionid='" & drv("questionid") & "' order by answerid"
    Dim DSAnswers As New SqlDataSource(ConfigurationManager.ConnectionStrings("SqlServer").ConnectionString, sql)
    Dim DVAnswers As Data.DataView = DSAnswers.Select(SystemWeb.UI.DataSourceSelectArguments.Empty)
    Dim radio As RadioButtonList = e.Row.FindControl("rdoAnswers")
    Dim rb As ListItem
    For Each row As Data.DataRow In DVAnswers.Table.Rows
      rb = New ListItem
      rb.Text = row.Item("answer")
      rb.Value = row.Item("answerid")
      radio.Items.Add(rb)
    Next
  End If
End Sub

Protected Sub btnSubmit_Click(Byval sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
  ' What should go here?
End Sub

Now you can see how I constructed the grid. In my button's Click method, how can I access my radio buttons to see which option was selected in each by the user? Is there a property or method of the grid that I can use to find each one?

Ian
 
submit will look something like this
Code:
if (this.IsValid)
{
   foreach(GridViewRow row this.gvQuestions.Rows)
   {
      if(row.RowType != RowType.DataRow) continue;

      RadioButtonList radio = (RadioButtonList)row.FindControl("rdoAnswers");
      if(row == null) continue;

      string answer = radio.SelectedValue;

      //add answer to dto collection or save to database or something else
   }
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
also arent you missing the postback check in pageload?
especially now that you are wiring up a button event?

Code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  [b]If Not Page.IsPostBack Then[/b]
  Dim sql As String = "select questionid,question from testquestions order by questionid"
  DSQuestions.SelectCommand = sql
  gvSubEvents.DataSourceID = "DSQuestions"
  [b]End If[/b]
End Sub
 
Yes! Jason, you are a life-saver. I've been trying to find the right combination of properties for days now. Thanks a ton!

Adam, I do appreciate the input, but I actually clipped that code from a MUCH longer and more complicated page. The code that I put in Page_Load is actually in a custom Sub.

Ian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top