如何处理许多与QUOT;是/否如果是<显示文本>"表格 [英] How to handle many "yes/no if yes <display textbox>" on Form

查看:89
本文介绍了如何处理许多与QUOT;是/否如果是<显示文本>"表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建在ASP.Net形式复制一个文件形式(我在设计上没有发言权,我只是用它重建任务的数字)。这种形式的线沿线的许多问题回答是或不是。如果是,指定多少。我目前正在通过列出的问题处理,然后有一个组中有两个单选按钮,有一句话说是和一个不。为了使这一点prettier,我已经使用Ajax的UpdatePanel将只显示一个文本框来保存这个是值,如果用户选择是被。

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.

现在我已经能够成功地做到这一点,但每一个问题是它自己的单选按钮组,拥有自己的面板更新的知名度,这意味着,目前我做的方式有很多多余的$ C的$ C像

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

等了有这样的是/否选项的每一个问题。我毫不怀疑有一个更好的方式来做到这一点。我不知道是否有一种方法,我可以通过与单选按钮组关联的面板,所以我可以用在code一个方法,将火为单选按钮回发,像(不是真正的code)

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

还是有更好的方式来处理这样的问题?我开出了不同的方法,它是否会削减冗余code说我现在打字的数量。我使用VB,但我知道C#所以,如果有人用流利的回答中,我会没有问题间preting它。

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.

任何帮助很多AP preciated。

Any help is much appreciated.

推荐答案

下面是一个工作code:

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>

请注意,所有的单选按钮具有相同的处理程序OnCheckedChanged并有自己的AutoPostBack =真

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

您可以把UpdatePanel的必要

You can put UpdatePanel where necessary

//后面code:

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;
    }
}

您也可以使用数据绑定控件(如GridView控件),但你有你自己的问题有一个列表。

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

快乐编码

这篇关于如何处理许多与QUOT;是/否如果是&lt;显示文本&GT;&QUOT;表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆