Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to disable postback of radiobutton based on some conditions in asp.net on client side?
    primarykey
    data
    text
    <p>I have a radiobutton list defined in my form. When the user selects any option, we do a postback and then business logic is executed. Now the problem is, I want to add a confirmation from user whenever he checks a radio button option; when the user selects OK then only I want to go ahead.</p> <p>Sample Flow: <br> 1. Assume FirstItem in radiobutton is checked.<br> 2. The user will go and select the second item - Then I want to add a confirmation like "Are you sure " - <br> 3. If the user selects NO <br> &nbsp; &nbsp; &nbsp; Then I should revert the check changed without the postback<br> &nbsp;&nbsp;&nbsp;&nbsp;Else <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Perform the postback and then business logic <br><br>The code is as follows:</p> <pre><code>&lt;form id="form1" runat="server"&gt; &lt;asp:ScriptManager ID="ScriptManager1" runat="server"&gt; &lt;/asp:ScriptManager&gt; &lt;div&gt; &lt;asp:UpdatePanel ID="UpdatePanelCheckBoxes" runat="server" UpdateMode="Always"&gt; &lt;ContentTemplate&gt; &lt;asp:RadioButtonList ID="rdo" runat="server" RepeatDirection="Horizontal" AutoPostBack="true" OnSelectedIndexChanged="LabourScheduleType_CheckedChanged"&gt; &lt;asp:ListItem Text="1" Value="1" onClick="confirmcheck();"&gt;&lt;/asp:ListItem&gt; &lt;asp:ListItem Text="2" Value="2" onClick="confirmcheck();"&gt;&lt;/asp:ListItem&gt; &lt;/asp:RadioButtonList&gt; &lt;/ContentTemplate&gt; &lt;/asp:UpdatePanel&gt; &lt;/div&gt; &lt;/form&gt; </code></pre> <p>The code-behind code is like this </p> <p>I wrote a javascript function also to control the postback </p> <pre><code>&lt;script type="text/javascript"&gt; function confirmcheck() { var retVal = confirm("test"); return retVal; } &lt;/script&gt; </code></pre> <p><br> I tried everything like adding an attribute like onclick and then returning false in the method and so on. Somehow the postback always happens and I do not know how to revert the check changed also ! I am going crazy over this from past 4 hours.</p> <p><strong>EDIT1:</strong></p> <p>I was searching over internet and found solution to half of my problem ! </p> <pre><code> &lt;asp:UpdatePanel ID="UpdatePanelCheckBoxes" runat="server" UpdateMode="Always"&gt; &lt;ContentTemplate&gt; &lt;asp:RadioButton ID="RadioButton1" OnCheckedChanged="aaa_cc" GroupName="aa" runat="server" AutoPostBack="true" Text="test1" /&gt; &lt;asp:RadioButton ID="RadioButton2" OnCheckedChanged="aaa_cc" GroupName="aa" runat="server" AutoPostBack="true" Text="test2" /&gt; &lt;/ContentTemplate&gt; &lt;/asp:UpdatePanel&gt; </code></pre> <p>The code behind file : </p> <pre><code>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);"); } } </code></pre> <p>Javacript goes like this:</p> <pre><code>&lt;script type="text/javascript"&gt; function RadioClick(radioButton) { if (confirm('Are you sure?')) { __doPostBack(radioButton.id,''); } else { return false; } } &lt;/script&gt; </code></pre> <p><strong>The only problem is when i return false, the value in radio button is lost i.e. none of the radio buttons are selected :(</strong></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload