In this article I will explain with an example, how to get the selected (checked) Text and Value of RadioButtonList on Button Click in ASP.Net using C# and VB.Net.
When the Button is clicked, the selected (checked) Text and Value of RadioButtonList will be fetched and will be displayed using JavaScript Alert Message Box.
HTML Markup
The following HTML Markup consists of an ASP.Net RadioButtonList control, a RequiredFieldValidator and a Button.
Select Fruit:
<asp:RadioButtonList ID="rblFruits" runat="server">
<asp:ListItem Text="Apple" Value="1" />
<asp:ListItem Text="Mango" Value="2" />
<asp:ListItem Text="Papaya" Value="3" />
<asp:ListItem Text="Banana" Value="4" />
<asp:ListItem Text="Orange" Value="5" />
</asp:RadioButtonList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ErrorMessage="Please select a Fruit.<br />"
ControlToValidate="rblFruits" runat="server" ForeColor="Red" Display="Dynamic" />
<asp:Button ID="btnSubmit" Text="Submit" runat="server" OnClick = "Submit" />
Get Selected Text and Value of RadioButtonList on Button Click
Inside the Click event handler, the Text and Value part of the Selected Item of the RadioButtonList is fetched and displayed using JavaScript Alert Message Box.
C#
protected void Submit(object sender, EventArgs e)
{
string message = "Selected Text: " + rblFruits.SelectedItem.Text;
message += "\\nSelected Value: " + rblFruits.SelectedItem.Value;
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('" + message + "');", true);
}
VB.Net
Protected Sub Submit(ByVal sender As Object, ByVal e As EventArgs)
Dim message As String = "Selected Text: " & rblFruits.SelectedItem.Text
message += "\nSelected Value: " & rblFruits.SelectedItem.Value
ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('" & message & "');", True)
End Sub
Screenshot
Demo
Downloads