I have a radio button list set up in my form. Upon selecting an option, a postback occurs followed by the execution of some business logic. However, I am facing an issue where I need to prompt the user for confirmation when they select a radio button option; only upon selecting "OK" should the process proceed.
Here is a breakdown of the desired flow:
1. The FirstItem in the radio button list is initially checked.
2. If the user attempts to select another item, I want to display a confirmation message like "Are you sure?"
3. If the user selects "NO"
Then I should revert the change without triggering a postback
Else
Perform the postback and execute the business logic
The provided code snippet showcases the implementation:
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
<asp:UpdatePanel ID="UpdatePanelCheckBoxes" runat="server" UpdateMode="Always">
<ContentTemplate>
<asp:RadioButtonList ID="rdo" runat="server" RepeatDirection="Horizontal" AutoPostBack="true"
OnSelectedIndexChanged="LabourScheduleType_CheckedChanged">
<asp:ListItem Text="1" Value="1" onClick="confirmcheck();"></asp:ListItem>
<asp:ListItem Text="2" Value="2" onClick="confirmcheck();"></asp:ListItem>
</asp:RadioButtonList>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
The accompanying code-behind snippet manages this functionality
A JavaScript function has also been implemented to handle the postback
<script type="text/javascript">
function confirmcheck() {
var retVal = confirm("test");
return retVal;
}
</script>
Various approaches were attempted to prevent the unwanted postback but the issue persists. Further exploration led to a partial solution!
<asp:UpdatePanel ID="UpdatePanelCheckBoxes" runat="server" UpdateMode="Always">
<ContentTemplate>
<asp:RadioButton ID="RadioButton1" OnCheckedChanged="aaa_cc" GroupName="aa" runat="server" AutoPostBack="true" Text="test1" />
<asp:RadioButton ID="RadioButton2" OnCheckedChanged="aaa_cc" GroupName="aa" runat="server" AutoPostBack="true" Text="test2" />
</ContentTemplate>
</asp:UpdatePanel>
The backend code was also tweaked according to the newfound solution:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
RadioButton1.Attributes.Add("onClick", "this.blur(); return RadioClick(this);");
RadioButton2.Attributes.Add("onClick", "this.blur(); return RadioClick(this);");
}
}
Corresponding JavaScript adjustments were made:
<script type="text/javascript">
function RadioClick(radioButton)
{
if (confirm('Are you sure?'))
{
__doPostBack(radioButton.id,'');
}
else
{
return false;
}
}
</script>
An outstanding issue arises when returning false causes the selected value in the radio button to be lost :(