Solved

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

Posted on 2011-03-16
6
880 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

743 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now