How to select asp.net RadioButton with javascript when autopostback is on
Asked Answered
S

1

2

Scenario:

  1. multiple radio buttons with a given GroupName and AutoPostBack="true".
  2. for styling purposes, the radio button is hidden with js and the click on its container (a td) is handled via js
  3. when clicking the td, the script 'clicks' the input element and triggers asp.net's auto postback

It does postback and hits PageLoad on the server, but the event in the codebehind is not triggered.

Satanic answered 11/12, 2012 at 19:34 Comment(0)
S
3

I posted this in case anyone is unfortunate enough to hit this problem.

Problem is very accurately mentioned in a comment on this answer https://mcmap.net/q/295730/-calling-onclick-on-a-radiobutton-list-using-javascript.

There is however one thing with the .click(): If you change the selected value of a radio with javascript like this, the 'change' event does not fire in IE (I tried IE8) – Michiel Reyers

That seems to mess with asp.net's postback event handling. So for it to pick it up, we explicitly select it first:

$(this).find("input").prop("checked", true);
$(this).find("input").click();
Satanic answered 11/12, 2012 at 19:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.