Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hey guys, I've having trouble with this for a long while and searching and trying many many different solutions, but nothing worked correctly... I have this:

<asp:RadioButtonList ID="accomodation" runat="server" RepeatDirection="Horizontal" RepeatLayout="Flow" Width="500px">
                <asp:ListItem Text="Not Required" Value="Not Required"></asp:ListItem>
                <asp:ListItem Text="Required" Value="Required"></asp:ListItem>
            </asp:RadioButtonList>

            <asp:Panel ID="PanelAccommodation" runat="server">
                <p>
                    Number of nights (*):
                     <asp:RequiredFieldValidator ID="RequiredFieldValidator12" runat="server" ControlToValidate="numberOfNights" ForeColor="red" ErrorMessage="<- Required"></asp:RequiredFieldValidator>
                </p>
                <asp:TextBox ID="numberOfNights" runat="server" Width="50px"></asp:TextBox>

                <ajaxToolkit:FilteredTextBoxExtender ID="FilteredTextBoxExtender1" TargetControlID="numberOfNights"
                    FilterType="Numbers"  runat="server">
                </ajaxToolkit:FilteredTextBoxExtender>

                <p>Preferred Hotel:</p>
                <asp:TextBox ID="preferredHotel" runat="server" Width="450px"></asp:TextBox>

                <p>Preferred Hotel URL:</p>
                <asp:TextBox ID="preferredHotelURL" runat="server" Width="450px"></asp:TextBox>

            </asp:Panel>


and what I want is for the panel to be closed when page is loaded, open when "Required" is selected and close if/when "Not Required" is selected... also I don't want to use AutoPostBack because it goes back to the top of the page... please help!
Posted

There are two ways of doing this
1. Use UpdatePanel so that it does not go back to top of the page when you AutoPostBack
2. Use JavaScript and make the panel display:none (hides) and display:block (shows)
 
Share this answer
 
Comments
varmartins 23-Jan-15 10:28am    
I have tried using JavaScript but it isn't working properly not sure why... can you please suggest some sample code?
varmartins 26-Jan-15 5:06am    
I used this code:

$(document).ready(function () {
$('#<%= accomodation.ClientID%>').find('input:radio').click(function () {
var selVal = $('#<%= accomodation.ClientID %>').find('input:checked').val();
if (selVal == "Required") {
$("#PanelAccomodation").show("fast");
alert("I'm here!");
}
else $("#PanelAccomodation").hide("fast");
})
});

and the alert popped up when I clicked on the RadioButtonList but the panel doesn't show or hide... any ideas?
XML
Guys I was also struggling with this problem (Using jQuery to hide/show a Panel when user clicks on a value on the RadioButtonList) and searched many posts and found no answer but I was able to make it work here's how I did...

My code is as follows:

    <div>
                <p class="space">3.2. ACCOMMODATION (*)</p>

                <asp:RadioButtonList ID="accomodation" runat="server" RepeatDirection="Horizontal" RepeatLayout="Flow" Width="500px">
                    <asp:ListItem Text="Not Required" Value="Not Required"></asp:ListItem>
                    <asp:ListItem Text="Required" Value="Required"></asp:ListItem>
                </asp:RadioButtonList>

                <asp:Panel ID="PanelAccommodation" runat="server">
                    <p>
                        Number of nights (*):
                         <asp:RequiredFieldValidator ID="RequiredFieldValidator12" runat="server" ControlToValidate="numberOfNights" ForeColor="red" ErrorMessage="<- Required"></asp:RequiredFieldValidator>
                    </p>
                    <asp:TextBox ID="numberOfNights" runat="server" Width="50px"></asp:TextBox>

                    <ajaxToolkit:FilteredTextBoxExtender ID="FilteredTextBoxExtender1" TargetControlID="numberOfNights"
                        FilterType="Numbers" runat="server">
                    </ajaxToolkit:FilteredTextBoxExtender>

                    <p>Preferred Hotel:</p>
                    <asp:TextBox ID="preferredHotel" runat="server" Width="450px"></asp:TextBox>

                    <p>Preferred Hotel URL:</p>
                    <asp:TextBox ID="preferredHotelURL" runat="server" Width="450px"></asp:TextBox>
                </asp:Panel>
            </div>

and the jquery function I used:

    <pre lang="Javascript">$(document).ready(function () {
                $("#MainContent_PanelAccommodation").hide("fast");
                $('#<%= accomodation.ClientID%>').find('input:radio').click(function () {
                    var selVal = $('#<%= accomodation.ClientID %>').find('input:checked').val();
                    if (selVal == "Required") {
                        $("#MainContent_PanelAccommodation").show("fast");
                    }
                    if (selVal == "Not Required") {
                        $("#MainContent_PanelAccommodation").hide("fast");
                    }
                })
            });


notice that the ID on the jquery has "MainContent" while that ID on the HTML doesn't this is because if you inspect your code and look for your panel it will also probably show and ID similar to MainContent_PanelAccommodation since it inherits from:

<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="server">

Hope this helps people struggling with the same problem...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900