[ASP.NET 2.0] GridView 에 RadioButton 추가
일반적인 라디오버튼 추가로는 버튼 그룹이 지어지지 않기 때문에 (랜더링 순서에 의함) 리터럴을 통해서 추가시켜야함.
1. aspx 단의 GridView 에 ItemTemplate 항목으로 리터럴 추가
<asp:TemplateField HeaderText="선택">
<ItemTemplate>
<asp:Literal ID="RadioButtonMarkup" runat="server"></asp:Literal>
</ItemTemplate>
<ItemStyle Width="80" />
</asp:TemplateField>
2. cs 단에 리피터에 라디오버튼 추가 코드생성 ,SuppliersSelectedIndex 으로 선택한 라디오버튼의 인덱스(즉 RowIndex)를 가져올수 있음.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// 라디오버튼 설정
Literal output = (Literal)e.Row.FindControl("RadioButtonMarkup");
output.Text = string.Format(
@"<input type='Radio' name='SuppliersGroup' " +
@"id='RowSelector{0}' value='{0}'", e.Row.RowIndex);
if (SuppliersSelectedIndex == e.Row.RowIndex)
output.Text += @" checked='checked'";
output.Text += " />";
}
}
private int SuppliersSelectedIndex
{
get
{
if (string.IsNullOrEmpty(Request.Form["SuppliersGroup"]))
return -1;
else
return Convert.ToInt32(Request.Form["SuppliersGroup"]);
}
}