?
Solved

How to hide/display text box based on radio buttons

Posted on 2013-02-03
4
Medium Priority
?
452 Views
Last Modified: 2013-02-21
Hello there.

This is my problem: I have in an input form a radio button  created within the same form, which captures for the app a value, based on wich there are textboxes which should be displayed/hidden. This proyect is coded in VB.NET behind a VS module with MS SQL Server db as BE.
0
Comment
Question by:LUISOFO
4 Comments
 
LVL 20

Accepted Solution

by:
informaniac earned 1500 total points
ID: 38849816
<asp:RadioButton ID="rb1" runat="server" AutoPostBack="true" 
            oncheckedchanged="rb1_CheckedChanged" />

protected void rb1_CheckedChanged(object sender, EventArgs e)
        {
            txt2.Visible = true;
        }

Open in new window

0
 
LVL 29

Expert Comment

by:Kumaraswamy R
ID: 38849976
try

document.getElementById('controlid').style.display='block';

'controlid is ur textbox name in page (see in view source and give textbox name)

morehttp://forums.asp.net/t/1479133.aspx/1?javascript+to+hide+control+on+radio+button+click
0
 
LVL 10

Expert Comment

by:Monica P
ID: 38849988
u can make use of javascript also to make this done

<input type="radio" class="yesRadio" onClick="showTextBox()"/>

<input type="text" class="textBox" hidden/>

Open in new window


use script
[b]  <script src="js/jquery-1.4.1.min.js" type="text/javascript"></script>[/b]
<script type="text/javascript>
function showTextBox() {
$(".textBox").show();
}
</script>

Open in new window


http://www.hscripts.com/tutorials/javascript/dom/radio-events.php

<script src="js/jquery-1.4.1.min.js" type="text/javascript"></script>
Do include the script file under js folder in your project., if dont have download from here
http://jquery.com/download/

or

u can use
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
Need Internet access for above URl
0
 
LVL 26

Expert Comment

by:Alan Warren
ID: 38872463
You can use the asp:RadioButtonList OnSelectedIndexChanged event to loop through the controls in an asp:FormView, get a handle on the controls you want to toggle visibility for.

Here's a sample showing how to loop through all controls in the ReadOnly Template of an asp:FormView, setting visibility of certain TextBox controls based on the Text value of the selected RadioButtonList item:
<%@ Page Language="VB" %>

<!DOCTYPE html>

<script runat="server">

    Protected Sub RadioButtonList1_SelectedIndexChanged(sender As Object, e As EventArgs)
        Dim fv As FormView = Nothing
        Dim tb As TextBox = Nothing
       If TypeOf sender.namingcontainer Is FormView Then
            fv = sender.namingcontainer
            If fv.CurrentMode = FormViewMode.ReadOnly Then
                
                For Each t As Table In fv.Controls
                    For Each r As TableRow In t.Rows
                        For Each c As TableCell In r.Cells
                            For Each ctl As Control In c.Controls
                                If TypeOf ctl Is TextBox Then
                                    If ctl.ID.Contains(sender.Text) Then
                                        ctl.Visible = True
                                        tb = ctl
                                        tb.Text = ctl.ID.ToString
                                    Else
                                        ctl.Visible = False
                                    End If
                                End If
                            Next
                        Next
                    Next
                Next
            End If
        End If

    End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:FormView ID="FormView1" runat="server" DataSourceID="SqlDataSource1">
            <ItemTemplate>
                ID:
                <asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID")%>' />
                <br />
                <asp:RadioButtonList ID="RadioButtonList1" runat="server" AutoPostBack="True"  OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged">
                    <asp:ListItem>Group1</asp:ListItem>
                    <asp:ListItem>Group2</asp:ListItem>
                </asp:RadioButtonList>
                <br />
                <br />
                <!-- Group1 controls -->
                <asp:TextBox ID="TextBox1_Group1" runat="server" Visible="false" BackColor="LemonChiffon" />
                <asp:TextBox ID="TextBox2_Group1" runat="server" Visible="false" BackColor="LemonChiffon" />
                <!-- Group2 controls -->
                <asp:TextBox ID="TextBox1_Group2" runat="server" Visible="false" BackColor="LemonChiffon" />
                <asp:TextBox ID="TextBox2_Group2" runat="server" Visible="false" BackColor="LemonChiffon" />
            </ItemTemplate>
        </asp:FormView>

        <asp:SqlDataSource ID="SqlDataSource1" runat="server" SelectCommand="select 1 as ID union all select 2 union all select 3" ConnectionString="<%$ ConnectionStrings:cnAkoJo %>"></asp:SqlDataSource>
    </div>
    </form>
</body>
</html>

Open in new window

Alan
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question