0

I'm creating a form in ASP.Net to replicate a paper form (I have no say in the design, I'm tasked with merely recreating it digitally). This form has many questions along the lines of "Answer yes or no. If yes, specify how much". I'm currently handling it by listing the question, and then having two radiobuttons in a group, one saying "yes" and one "no". To make this a little prettier, I've been using Ajax updatepanels that will only display a textbox to hold this yes value if the user selects Yes.

Now I've been able to do this successfully, but each question is its own radiobutton group and has its own panel to update visibility, which means that the way I'm currently doing it there is a lot of redundant code like

Protected Sub rdoShowOriginalEquipment(ByVal sender As Object, ByVal e As System.EventArgs)
    If rdoOEYes.Checked = True Then
        pnlOriginalEquipment.Visible = True
    ElseIf rdoOENo.Checked = True Then
        pnlOriginalEquipment.Visible = False
    End If
End Sub

And so on for every question that has a yes/no option like that. I have no doubt there is a better way to do this. I was wondering if there is a way I could pass the panel associated with the radiobutton group so I could use a single method in the code that would fire for all radiobutton postbacks, something like (not real code)

Protected Sub showPanel(RadioButtonGroup, panel)
    If rdoYes.Checked = True Then
        panel.Visible = True
    ElseIf rdoNo.Checked = True Then
        panel.Visible = False
    End If
End Sub

Or is there a better way to handle questions like this? I'm open to a different approach if it would cut down on the amount of redundant code that I'm typing now. I'm using VB, but I know C# so if someone is fluent in that with an answer I'd have no problem interpreting it.

Any help is much appreciated.

JMill
  • 3
  • 1

1 Answers1

1

Here is a working code:

<asp:Panel ID="Question1" runat="server">
    <asp:RadioButton GroupName="Q1" runat="server" ID="Q1Yes" Text="Yes" OnCheckedChanged="AnswerChanged" AutoPostBack="true" />
    <asp:RadioButton GroupName="Q1" runat="server" ID="Q1No" Text="No" OnCheckedChanged="AnswerChanged" AutoPostBack="true" />
    <asp:Panel runat="server" ID="Q1Panel">Some text here</asp:Panel>
</asp:Panel>

<asp:Panel ID="Question2" runat="server">
    <asp:RadioButton GroupName="Q2" runat="server" ID="Q2Yes" Text="Yes" OnCheckedChanged="AnswerChanged" AutoPostBack="true" />
    <asp:RadioButton GroupName="Q2" runat="server" ID="Q2No" Text="No" OnCheckedChanged="AnswerChanged" AutoPostBack="true" />
    <asp:Panel runat="server" ID="Q2Panel">Some text here</asp:Panel>
</asp:Panel>

Note that all radio button have the same handler for OnCheckedChanged and have their AutoPostBack=True

You can put UpdatePanel where necessary

//Code behind:

protected void AnswerChanged(object sender, EventArgs e)
{
    RadioButton rbAnswer = (RadioButton)sender;
    if (rbAnswer.Checked)
    { 
        string panelID = rbAnswer.GroupName + "Panel";
        if (rbAnswer.Text == "Yes")
                rbAnswer.Parent.FindControl(panelID).Visible = true;
         else
                rbAnswer.Parent.FindControl(panelID).Visible = false;
    }
}

You can also use DataBound controls (e.g. GridView) but you will have your questions has a list.

Happy Coding

codingbiz
  • 26,179
  • 8
  • 59
  • 96