当用户检查“其他”按钮时,使文本框可见

时间:2014-04-04 01:13:52

标签: javascript jquery asp.net vb.net

我有一个单选按钮,可以从数据库中动态获取其值。

其中一个数据库值称为“其他”#。

如果用户选中此Other单选按钮,请显示文本框,以便用户输入。

有谁知道怎么做?

以下代码无效。

<tr>
 <td>
  <asp:RadioButtonList ID="RadioButtonList1" runat="server"></asp:RadioButtonList>
    <asp:TextBox ID="txtOther" runat="server" Visible="false" Font-Bold="False"></asp:TextBox>
  <asp:CheckBoxList ID="CheckBoxList1" runat="server"></asp:CheckBoxList>
  <asp:TextBox ID="TextBox1" runat="server" Columns="30" Font-Bold="False" Rows="5"
                                            TextMode="MultiLine"></asp:TextBox>
 </td>
</tr>

Protected Sub DataList1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles DataList1.ItemDataBound
    If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
        Dim anstype As HiddenField = e.Item.FindControl("HiddenField1")
        'Dim questionid As Label = e.Item.FindControl("Label3")
        Dim questionid As HiddenField = e.Item.FindControl("HiddenField2")
        Dim rbl As RadioButtonList = e.Item.FindControl("RadioButtonList1")
        Dim cbl As CheckBoxList = e.Item.FindControl("CheckBoxList1")
        Dim txt As TextBox = e.Item.FindControl("TextBox1")
        Dim ds As DataSet = GetDataSet(questionid.Value)
        Select Case anstype.Value
            Case "S"
                rbl.Visible = True
                cbl.Visible = False
                txt.Visible = False
                rbl.DataSource = ds
                rbl.DataTextField = "Choice"
                rbl.DataValueField = "ChoiceID"
                rbl.DataBind()
            Case "M"
                rbl.Visible = False
                cbl.Visible = True
                txt.Visible = False
                cbl.DataSource = ds
                cbl.DataTextField = "Choice"
                cbl.DataValueField = "ChoiceID"
                cbl.DataBind()
            Case "T"
                rbl.Visible = False
                cbl.Visible = False
                txt.Visible = True
        End Select
    End If
End Sub

Protected Sub RadioButton1_OnCheckedChanged(sender As Object, e As EventArgs)
    Dim RadioButton1 As RadioButtonList = TryCast(sender, RadioButtonList)
    If RadioButton1 IsNot Nothing Then
        If RadioButton1.SelectedValue = "Other" Then
            Dim datalistrow As DataList = TryCast(RadioButton1.NamingContainer, DataList)
            Dim TxtOther As TextBox = TryCast(datalistrow.FindControl("TxtOther"), TextBox)
            TxtOther.Visible = True
        End If
    End If

End Sub

我还要补充说,其他的单选按钮值是33。

换句话说,<input id="DataList1_RadioButtonList1_5_2_5" type="radio" name="DataList1$ctl06$RadioButtonList1" value="33" />

非常感谢

2 个答案:

答案 0 :(得分:0)

Tairoc,

您绑定到单选按钮的值为

                rbl.DataTextField = "Choice"
                rbl.DataValueField = "ChoiceID"

比如说数据库值是Val1,Val2,Val3和其他

这四个选项中的一个是radiobuttonlist的选定值,您要设置..

如果您在DataList1_ItemDataBound事件中设置了radiobuttonlist选定值..

您可以按如下方式编写代码..

<tr>
 <td>
  <asp:RadioButtonList ID="RadioButtonList1" runat="server"></asp:RadioButtonList>
    <asp:TextBox ID="txtOther" runat="server" Visible="false" Font-Bold="False"></asp:TextBox>
  <asp:CheckBoxList ID="CheckBoxList1" runat="server"></asp:CheckBoxList>
  <asp:TextBox ID="TextBox1" Visible="false"  runat="server" Columns="30" Font-Bold="False" Rows="5"
                                            TextMode="MultiLine"></asp:TextBox>
 </td>
</tr>

Protected Sub DataList1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles DataList1.ItemDataBound
    If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
        Dim anstype As HiddenField = e.Item.FindControl("HiddenField1")
        'Dim questionid As Label = e.Item.FindControl("Label3")
        Dim questionid As HiddenField = e.Item.FindControl("HiddenField2")
        Dim rbl As RadioButtonList = e.Item.FindControl("RadioButtonList1")
        Dim cbl As CheckBoxList = e.Item.FindControl("CheckBoxList1")
        Dim txt As TextBox = e.Item.FindControl("TextBox1")
        Dim ds As DataSet = GetDataSet(questionid.Value)
        Select Case anstype.Value
            Case "S"
                rbl.Visible = True
                cbl.Visible = False
                txt.Visible = False
                rbl.DataSource = ds
                rbl.DataTextField = "Choice"
                rbl.DataValueField = "ChoiceID"
                rbl.DataBind()

      // you juss binded the radiobuttonlist but you havent set the selected value..
     // there should be selected value "Other" so that based on the selected value we                             
     //can make the textbox1 Visible

            Case "M"
                rbl.Visible = False
                cbl.Visible = True
                txt.Visible = False
                cbl.DataSource = ds
                cbl.DataTextField = "Choice"
                cbl.DataValueField = "ChoiceID"
                cbl.DataBind()
            Case "T"
                rbl.Visible = False
                cbl.Visible = False
                txt.Visible = True
        End Select
    End If
End Sub

如果未在RowDataBound事件中设置radiobutton选定值 一旦单选按钮列表加载了内容

比如说

第1行)Val1 Val2 Val3其他

第2行)Val1 Val2 Val3其他

第3行)Val1 Val2 Val3其他

当用户选择&#34;其他&#34; radiobutton列表中的选项然后在Radiobutton OnSelectedIndexChanged事件中必须这样写..

    Protected Sub RadioButtonList1_OnSelectedIndexChanged(sender As Object, e As EventArgs)
    Dim RadioButton1 As RadioButtonList = TryCast(sender, RadioButtonList)
    If RadioButton1 IsNot Nothing Then
        Dim datalistrow As DataList = TryCast(RadioButton1.NamingContainer, DataList)
        Dim TxtOther As TextBox = TryCast(datalistrow.FindControl("TxtOther"), TextBox)

        If RadioButton1.SelectedValue = "Other" Then

            TxtOther.Visible = True
        Else
            TxtOther.Visible = False
        End If
    End If

End Sub


Protected Sub TextBox1_OnTextChanged(sender As Object, e As EventArgs)
    If TextBox1.Text IsNot Nothing Then
        RadioButton1.Items.Add(TextBox1.Text)
    End If
End Sub

答案 1 :(得分:0)

试试这个..

  Protected Sub TextBox1_OnTextChanged(sender As Object, e As EventArgs)
    Dim TextBox1 As TextBox = TryCast(sender, TextBox)
    If TextBox1 IsNot Nothing Then
        Dim RadioButton1 As RadioButtonList = TryCast(TextBox1.Parent.FindControl("RadioButton1"), RadioButtonList)
        If TextBox1.Text IsNot Nothing Then
            RadioButton1.Items.Add(TextBox1.Text)
        End If
    End If

End Sub