?
Solved

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

Posted on 2011-03-16
6
Medium Priority
?
916 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses
Course of the Month15 days, 5 hours left to enroll

770 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