JamesYTL
JamesYTL

Reputation: 344

Disable RadioButton (NOT in Radio Button list) from server side

Currently using 3 radio buttons and grouped them using GroupName.

Radio Button's code

 <asp:Panel class="RBPnl" runat="server" ID="pnl_rb">
      <asp:RadioButton ID="rb" runat="server" GroupName="rbg_RB" Text="Radio Button" CssClass="RB" />
           <label class="RBDesc">Radio Button Descriptions</label>
 </asp:Panel>

Now based on certain condition I want to disable a radio button from code behind. Searched SO & Google and most solution mentioned two methods below :

  1. rb.enabled = false;

Result :

<span class="aspNetDisabled RB">
<input id="cph_content_rb" type="radio" name="ctl00$cph_content$rbg_rb" value="rb">
<label for="cph_content_rb">Radio Button</label></span>
  1. rb.Attributes.add("disabled","disabled");

Result:

<span class="RB" disabled="disabled">
<input id="cph_content_rb" type="radio" name="ctl00$cph_content$rbg_rb" value="rb">
<label for="cph_content_rb">Radio Button</label></span>

As you can see I tried both but still able to check the supposedly disabled radio button because it should disable the input but the code disable at span instead.

I know javascript can solve this by adding disabled attributes to the children (input) of radiobutton but I want to know the method to disable at server side (c#)

Upvotes: 0

Views: 1142

Answers (1)

Shiyao Wang
Shiyao Wang

Reputation: 29

I guess your problem is: Asp.net WebForm backend code does not change your html dynamically.

You may compare WebForm with WinForm to see the difference:

  • In WinForm desktop application, your application window automatically refresh itself several times a second. When your code set the radio button to disabled, AFTER your application windows REFRESH itself in a few mill seconds, you can see the radio button BECOME disabled.
  • In WebForm web application, your webpage DOES NOT REFRESH AUTOMATICALLY. It only gets refreshed when your webpage send something back to the server (e.g. Button click event) and then the WHOLE PAGE will get refreshed.

So you must be clear about the execution order of each step happened after the webpage sends back something and before it gets rendered in the browser:

  1. Something happened in the browser (e.g. Entering a url and press enter, clicking a button on the loaded page, etc.)
  2. The browser sends a request to the server. If it's triggered by something on your webpage, the request will include a lot of information including a large thing called ViewState.
  3. Your server parse the ViewState to a C# object (Your webpage is compiled as a C# class like in WinForm desktop application). If it does not contain a ViewState, server will new an object for you.
  4. According to the request, certain code is executed. (e.g. OnXxxClicked, etc.)
  5. Then a method in your webpage object, which is inherited from its BaseClass, is invoked and it will render itself as a html string and an encoded viewstate string inside that html string.
  6. Finally this long html string will be sent to the browser.

So you must make sure your code which disables the radio button is executed in Step 3, and you must know what action on the webpage will trigger Step 1.

Upvotes: 0

Related Questions