프로그래밍/ASP.NET
Repeater 리피터에 라디오버튼을 넣었을때 중복체크 안되게 하는법
쇠주는참이슬
2012. 4. 25. 11:13
Repeater 안에 RadioButton을 넣을 경우..
실행해보면 다 따로논다. GroupName을 줘도 따로논다.
리피터를 실행 후 소스보기를 클릭해서 보면
각 Row마다 앞에 있는 ctl101.. ctl102.. 이렇게 증가되어 있다.
해결방법은 아래와같이
function SetUniqueRadioButton(nameregex, current) { re = new RegExp(nameregex); for (i = 0; i < document.forms[0].elements.length; i++) { elm = document.forms[0].elements[i] if (elm.type == 'radio') { if (re.test(elm.name)) { elm.checked = false; } } } current.checked = true; } Repeater_ItemDataBound에 아까 만들어 놓은 함수를 등록시켜주면 된다. protected void rptPortfolios_ItemDataBound(object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem) return; RadioButton rdo = (RadioButton)e.Item.FindControl("RadioButton1"); string script = "SetUniqueRadioButton('Repeater1.*rdoGroupName',this)"; rdo.Attributes.Add("onclick", script); }출처 : http://www.codeguru.com/csharp/csharp/cs_controls/custom/article.php/c12371/ASPNET-Tip-Using-RadioButton-Controls-in-a-Repeater.htm