0

I am aware that I can user RadioButton web control instead of the input type="radio" with runat="server". However, we are trying to modernize our application and trying to stick with HTML5 generic controls (so that they are easy to migrate when we finally have MVC).

I have radio buttons as bellow.

<input type="radio" name="XYZ" value="abc" id="optabc" runat="server"/>abc<br/>
<input type="radio" name="XYZ" value="def" id="optdef" runat="server"/>def<br/>
<input type="radio" name="XYZ" value="ghi" id="optghi" runat="server"/>ghi<br/>
<input type="radio" name="XYZ" value="jklk" id="optjkl" runat="server"/>jkl<br/>
<input type="radio" name="XYZ" value="mno" id="optmno" runat="server"/>mno<br/>
<input type="radio" name="XYZ" value="pqr" id="optpqr" runat="server"/>pqr<br/>

I handle the click event as below.

protected void Page_Load(object sender, EventArgs e)
        {
            optabc.ServerChange += new System.EventHandler(this.Server_Change);
            optdef.ServerChange += new System.EventHandler(this.Server_Change);
            optghi.ServerChange += new System.EventHandler(this.Server_Change);
            optjkl.ServerChange += new System.EventHandler(this.Server_Change);
            optmno.ServerChange += new System.EventHandler(this.Server_Change);
            optpqr.ServerChange += new System.EventHandler(this.Server_Change);
        }


protected void Server_Change(object sender, EventArgs e)
    {
        if (optabc.Checked)
            //do something
        else if (optmno.Checked)
            //do something
    }

I have made sure AutoEventWireup="true". However, Server_Change method never gets executed when radio button is selected.

socialMatrix
  • 1,423
  • 5
  • 20
  • 36
  • Are you clicking a button to cause a postback? or are you expecting it to just happen when you click a radio button? – dmeglio Jul 27 '15 at 18:42
  • expecting it to just happen when clicking a radio button – socialMatrix Jul 27 '15 at 19:10
  • 1
    That's the problem. Radio buttons don't trigger anything on the server side. You'll need to submit the form for the server to know something changed. There's no `AutoPostBack` when you're not using the ASP.NET Web Controls. You'll need to write JavaScript to submit the form when they click one. – dmeglio Jul 27 '15 at 19:11
  • I tried adding AutopostBack=true attribute, and that didn't work either. e.g. pqr
    – socialMatrix Jul 28 '15 at 15:45
  • 1
    No, my point is you *can't* autopostback. You'd need to use an ``. What you want to do is something like `document.getElementById('THEIDOFYOURFORM').submit();` of course though, that's going to cause a complete page reload (postback). – dmeglio Jul 28 '15 at 15:46
  • Ok yeah, that did the trick :-). So even if I set AutopostBack=True for HTMLControls, it won't postback? Can you possibly give me a link to documentation? I'd like to learn this in detail and not just solve a problem. – socialMatrix Jul 28 '15 at 16:29
  • 1
    There is no AutoPostBack on an HTMLGenericControl. So setting it accomplishes nothing because it doesn't exist. From ASP.NET's perspective, setting is no different than . It will just ignore that attribute and render it to the page because ASP.NET doesn't know what to do with it. There's really no documentation on it, it's just not a feature that exists. – dmeglio Jul 28 '15 at 16:35

0 Answers0