Solved

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

Posted on 2011-03-16
6
891 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:EYoung
  • 4
6 Comments
 
LVL 5

Expert Comment

by:soujanya_g
ID: 35154753
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
 
LVL 7

Author Comment

by:EYoung
ID: 35154864
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
 
LVL 11

Accepted Solution

by:
SAMIR BHOGAYTA earned 500 total points
ID: 35155771
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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 7

Author Comment

by:EYoung
ID: 35176068
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
 
LVL 7

Author Comment

by:EYoung
ID: 35182380
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
 
LVL 7

Author Closing Comment

by:EYoung
ID: 35184713
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

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB 2005 Tooltips on Form Load Event 15 29
Executing SSIS with a Stored Procedure Permissions 12 59
VB: Convert 2 dates to specific format 24 48
Linq asp.net mvc 13 6
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

776 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