How to compare two radio button list? With Out Using Compare Validator
In this post i will explain how to compare 2 radio button lists
with same values. And ask user to select different value for both list.
Html Code for Radio Button List 1 and 2
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>How to compare radio button list</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Label ID="lblerrorMsg" Font-Bold="true" ForeColor="red" runat="server"></asp:Label>
<div style="background-color: #d2d2d2; width: 150px; height: 160px; float: left;
padding: 5px; margin: 15px;">
<asp:Label ID="lblSports1" runat="server" Text="Sports1"></asp:Label>
<br />
<br />
<asp:RadioButtonList ID="RadioButtonList1" runat="server" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged"
AutoPostBack="true" Width="140px">
<asp:ListItem Text="Walking" Value="Walking"></asp:ListItem>
<asp:ListItem Text="Surfing" Value="Surfing"></asp:ListItem>
<asp:ListItem Text="Diving" Value="Diving"></asp:ListItem>
<asp:ListItem Text="Kayaking" Value="Kayaking"></asp:ListItem>
<asp:ListItem Text="Fishing" Value="Fishing"></asp:ListItem>
</asp:RadioButtonList>
</div>
<div style="background-color: #d2d2d2; width: 150px; height: 160px; float: left;
padding: 5px; margin: 15px;">
<asp:Label ID="lblSports2" runat="server" Text="Sports2"></asp:Label>
<br />
<br />
<asp:RadioButtonList ID="RadioButtonList2" runat="server" OnSelectedIndexChanged="RadioButtonList2_SelectedIndexChanged"
AutoPostBack="true" Width="140px">
<asp:ListItem Text="Walking" Value="Walking"></asp:ListItem>
<asp:ListItem Text="Surfing" Value="Surfing"></asp:ListItem>
<asp:ListItem Text="Diving" Value="Diving"></asp:ListItem>
<asp:ListItem Text="Kayaking" Value="Kayaking"></asp:ListItem>
<asp:ListItem Text="Fishing" Value="Fishing"></asp:ListItem>
</asp:RadioButtonList>
</div>
</form>
</body>
</html>
OutputNow in the CS page add the following code for SelectedIndexChanged for both the radio button list.
Radio Button 1:
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
// Store the selected value in View State
ViewState["SportValue1"] = RadioButtonList1.SelectedValue;
// To call the method to compare values
CompareRadioButton();
}
Radio Button 2:
protected void RadioButtonList2_SelectedIndexChanged(object sender, EventArgs e)
{
// Store the selected value in View State
ViewState["SportValue2"] = RadioButtonList2.SelectedValue;
// To call the method to compare values
CompareRadioButton();
}
Comparing Method:
protected void CompareRadioButton()
{
// To check both the radio button values are selected
if (RadioButtonList1.SelectedIndex != -1 && RadioButtonList2.SelectedIndex != -1)
{
string num1 = ViewState["SportValue1"].ToString();
string num2 = ViewState["SportValue2"].ToString();
if (num1 == num2)
{
lblerrorMsg.ForeColor = System.Drawing.Color.Red;
lblerrorMsg.Text = "Select a different value for sports1 and sports2";
}
else
{
lblerrorMsg.ForeColor = System.Drawing.Color.Green;
lblerrorMsg.Text = "Values are different";
}
}
}
In the above method we compare the 2 radio button selected values, if both are same we throw error message or else we show "Values are different" message.
Now Select the different option and see the result as below
No comments:
Post a Comment