For a while I had a problem when using radio buttons with partial page updates.
The issue was the automatic postback event did not fire if the radio button attribute Checked was set to true. Apparently ASP.NET does not add the onclick client side script to radio buttons, if they are allready checked when the page is rendered.
This is actually not a problem when you are using normal page postbacks, since the radio button is re-rendered and the attribute is added to the control (it's state is no longer checked).
In AJAX however, this poses as a problem, since the control in some cases isn't re-rendered, hence not adding the attribute, which in turn makes the radio button useless as it doesn't fire any postback.
I scoured the web for a solution, but the only one which seemed to do the trick was adding custom javascript to the controls attrubute collection (onclick).
Even though it "seemed" to solve the problem and the page did a postback, the control wasn't registered in the page events, which made it virtually impossible to do anything resonable with it.
But now to the solution.
Each webcontrol contains an InputAttributes collection, which contains the attributes for the input tag on the page.
This collection can be manipulated at runtime, making it possible to set the needed attribute "checked".
All that needs to be done is the following:
RadioButton rbtnName = new RadioButton();
rbtnName.InputAttributes.Add("checked", "checked");
The ASP.NET renders the control as it is not checked, but setting the above attribute, actually does the magic, avoiding the problem all together.
No comments:
Post a Comment