Solved

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

Posted on 2011-03-16
6
895 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

821 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