How get selected item from asp:RadioButtonList in VB Code Behind without PostBack

I am writing an asp.net 4.0 web site that contains a RadioButtonList control on one of the web pages.  What I want to achieve is to determine which radio button is checked using the code behind in VB and do some action on the web page like displaying in a label which item was checked.

I would prefer to not use a post back as I am trying to do as much on the client side using Ajax controls as possible.

I have included the .aspx code that shows the <asp:RadioButtonList...>.  See "Content2, Panel1" section.

I have also included the .aspx.vb code but it does not contain any RadioButtonList code as I don't know how to query the button selection and fill in the label.

Thank you for the help
===========================================================================
Here is the aspx code:  (see "Content2, Panel1" section)
===========================================================================

<%@ Page Title="Retail Reports" Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="Retail_Reports.aspx.vb" Inherits="_Default" %>

<asp:Content ID="Content1" ContentPlaceHolderID="Headers" Runat="Server">
    <asp:ScriptManager ID="ScriptManager1" runat="server" />   

    <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always">
        <ContentTemplate>
            <asp:Label  ID="lblWelcome" runat="server" Text = "" style="position:absolute; top:0px; left:950px; z-index: 1;
                visibility:visible; height: 20px; width: 300px; text-align:right;"><p><%= mWelcomeMessage%></p>
            </asp:Label>
            <asp:Label ID="lblPageName" runat="server" Text="Retail Reports" style="position:absolute; top:61px; left:833px; z-index: 1;
                visibility:visible; height: 20px; width: 290px; font-size:xx-large; color: Maroon; text-align:right" >
            </asp:Label>
        </ContentTemplate>
    </asp:UpdatePanel>     
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="Body" Runat="Server">
    <asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Always">
        <ContentTemplate>
            <asp:Label  ID="Label1" runat="server" Text = "Reports" style="position:absolute; top:230px; left:130px; z-index: 1;
                visibility:visible; height: 20px; width: 70px; text-align:left;" 
                Font-Bold="True" Font-Underline="False" ForeColor="Maroon">
            </asp:Label>
            <asp:Label  ID="Label2" runat="server" Text = "__________________________________" style="position:absolute; top:230px; left:130px; z-index: 1;
                visibility:visible; height: 20px; width: 70px; text-align:left;" 
                Font-Bold="True" Font-Underline="False" ForeColor="Maroon">
            </asp:Label>

            <asp:Panel ID="Panel1" runat="server"
                style="position:absolute; top:255px; left:140px; z-index: 1; visibility:visible; height: 200px; width: 250px;" >

                <asp:radiobuttonlist id="Reports_RadioButtonList" runat="server" OnSelectedIndexChanged="Get_Selected_RadioButton"
                    style="position:absolute; top:10px; left:10px; z-index: 1; visibility:visible; height: 100px; width: 230px; "
                        Font-Bold="False" ForeColor="Maroon">
                  <asp:listitem id="radRetail_Class_Sales" runat="server" value="Retail Class Sales" />
                  <asp:listitem id="radRetail_Executive_Flash_Sales" runat="server" value="Retail Executive Flash Sales" />
                  <asp:listitem id="radRetail_Kiosk_and_Store_Net_Sales" runat="server" value="Retail Kiosk and Store Net Sales" />
                </asp:radiobuttonlist>        
            </asp:Panel>
         
        </ContentTemplate>
    </asp:UpdatePanel>     
</asp:Content>

===========================================================================
Here is the aspx.vb code
===========================================================================

Imports System.DirectoryServices.AccountManagement

Partial Class _Default
    Inherits System.Web.UI.Page

    Public mUserName As String
    Public mInt As Integer
    Public mWelcomeMessage As String

    Public Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            mUserName = Trim(System.Web.HttpContext.Current.User.Identity.Name)
            If mUserName > "" Then
                mInt = InStr(mUserName, "\")
                If mInt > 0 Then
                    mUserName = Mid(mUserName, mInt + 1, Len(mUserName) - (mInt))
                    mWelcomeMessage = "Welcome:&nbsp;&nbsp;&nbsp;" & mUserName
                Else
                    mWelcomeMessage = "Welcome:&nbsp;&nbsp;&nbsp;" & mUserName
                End If
            Else
                mWelcomeMessage = ""
            End If
        Catch
            If Err.Number <> 0 Then
                mUserName = ""
            End If
        End Try
    End Sub
End Class

Open in new window

LVL 7
EYoungAsked:
Who is Participating?
 
SAMIR BHOGAYTAFreelancer and IT ConsultantCommented:
Hello,

<asp:RadioButtonList ID="rblTakeHoliday" runat="server" AutoPostBack="True"
                    onselectedindexchanged="rblTakeHoliday_SelectedIndexChanged"
                    RepeatDirection="Horizontal">
                    <asp:ListItem Value="Yes">Yes</asp:ListItem>
                    <asp:ListItem Value="No">No</asp:ListItem>
           <asp:ListItem Value="Maybe">Maybe</asp:ListItem>
                </asp:RadioButtonList>

rblTakeHoliday.Items[0].Selected = false;
rblTakeHoliday.Items[1].Selected = false;
rblTakeHoliday.Items[2].Selected = true;

if(!IsPostBack){//SET YOUR RADIO BUTTON LIST DATA}

// Suppose at databse value "MayBe" is saved. Assign it into a variable the try to set
String myValue="MayBe";
rblTakeHoliday.SelectedIndex=
rblTakeHoliday.Items.IndexOf(rblTakeHoliday.Items.FindByValue(myValue));
0
 
soujanya_gCommented:
Hi
You can check by writting javascript for onchang or onclick event of radio button list or use ajax  update panels  to appearing postbacks
0
 
EYoungAuthor Commented:
Yes I know.  What I am looking for is an example of VB code showing how to do that.  Can you provide an example using VB?
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
EYoungAuthor Commented:
Sorry for the delay in getting back to you.  I got pulled off onto another project.  Will test this out early next week.  Thanks
0
 
EYoungAuthor Commented:
I am getting the following error:
Error      1      'rdlAutoOrManual_SelectedIndexChanged' is not a member of 'ASP.retail_reports_aspx'.      C:\Program Files\Microsoft Visual Studio 10.0\TTA_Applications\tta-intranet\Retail_Reports.aspx      38      
What I would like to have happen is to click one of the RadioButtons in the list and have the selected button's name appear in the label (Label2).  For example, if the user clicks the Auto button instead of the Manual button, then the word "Auto" would appear in Label2.

I use the "Code Behind" method instead of the "In Line" method.  Should I add an event in the Code Behind file for:  rdlAutoOrManual_SelectedIndexChanged?

There is no valid reason for wanting to do this other than my trying to figure out how postback works.  Web coding is just so different from Windows coding that I am having a tough time grasping the concepts.

Thank you for your help.
Here is the code for Retail_Reports.asps:

<asp:Content ID="Content2" ContentPlaceHolderID="Body" Runat="Server">
    <asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Always">
        <ContentTemplate>
            <asp:tabcontainer ID="TabContainer1" runat="server" ActiveTabIndex="0"
                style="position:absolute; top:200px; left:175px; z-index: 1; visibility:visible; "
                    ForeColor="Maroon" Height="550px" Width="950px">

                <asp:TabPanel ID="Specialty_Retail_Flash_Summary" runat="server" HeaderText="Specialty Retail Flash Summary"
                    style="height: 200px; width: 700px">
                    <ContentTemplate>
                        <asp:Panel ID="Panel1" runat="server" BorderColor="Maroon" BorderStyle="Solid" 
                            BorderWidth="1px" Height="200px" Width="895px"
                            style="position:absolute; top:45px; left:25px" Font-Underline="False">

                            <asp:Label ID="Label1" runat="server" Text="Schedule:" Font-Underline="True" Font-Bold="True" 
                                style="top:50px; left:50px;"></asp:Label>

                            <asp:RadioButtonList ID="rdlAutoOrManual" runat="server" AutoPostBack="True"
                                onselectedindexchanged="rdlAutoOrManual_SelectedIndexChanged"
                                RepeatDirection="Vertical">
                                <asp:ListItem Text="Automatically run report daily at:" Value="Auto" />
                                <asp:ListItem Text="Manually run report now for date: " Value="Manual" />
                            </asp:RadioButtonList>

                            <asp:Label ID="Label2" runat="server" Text="lblText"></asp:Label>

                        </asp:Panel>

                    </ContentTemplate>
                </asp:TabPanel>

                <asp:TabPanel ID="Specialty_Retail_Flash_Detail" runat="server" HeaderText="Specialty Retail Flash Detail"
                    style="height: 250px; width: 700px">
                </asp:TabPanel>
            </asp:tabcontainer>

        </ContentTemplate>
    </asp:UpdatePanel>     
</asp:Content>


==============================================================================
Here is the code for "Code Behind" file:  Retail_Reports.aspx.vb:
==============================================================================

Imports System.DirectoryServices.AccountManagement

Partial Class _Default
    Inherits System.Web.UI.Page

    Public mUserName As String
    Public mInt As Integer
    Public mWelcomeMessage As String

    Public Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            mUserName = Trim(System.Web.HttpContext.Current.User.Identity.Name)
            If mUserName > "" Then
                mInt = InStr(mUserName, "\")
                If mInt > 0 Then
                    mUserName = Mid(mUserName, mInt + 1, Len(mUserName) - (mInt))
                    mWelcomeMessage = "Welcome:&nbsp;&nbsp;&nbsp;" & mUserName
                Else
                    mWelcomeMessage = "Welcome:&nbsp;&nbsp;&nbsp;" & mUserName
                End If
            Else
                mWelcomeMessage = ""
            End If
        Catch
            If Err.Number <> 0 Then
                mUserName = ""
            End If
        End Try
    End Sub

End Class

Open in new window

0
 
EYoungAuthor Commented:
Thank you for the help.  Eventually I was able to struggle my way through it and figure out how it works.

Thank you also for providing a code sample.  Seeing the code really helped me to better understand the logic.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.