DataGrid에 RadioButtonList를 넣는 것을 물어 본 분이 있어 간단한 샘플을 만들어 봤다.
##########################
##### WebForm1.aspx #####
##########################
<asp:DataGrid id="DataGrid1" runat="server">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:RadioButtonList Runat="server" ID="rblTest" RepeatDirection="Horizontal">
</asp:RadioButtonList>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
##########################
##### WebForm1.aspx.vb #####
##########################
Private Sub Page_Load...
If Not IsPostBack Then
Me.DataGrid1.DataSource = MakeTable()
Me.DataGrid1.DataBind()
End If
End Sub
Private Function MakeTable() As DataTable
' This Function From MSDN
' Create a DataTable.
Dim myTable As DataTable = New DataTable("myTable")
' Create a DataColumn and set various properties.
Dim myColumn As DataColumn = New DataColumn
myColumn.DataType = System.Type.GetType("System.Decimal")
myColumn.AllowDBNull = False
myColumn.Caption = "Price"
myColumn.ColumnName = "Price"
myColumn.DefaultValue = 25
' Add the column to the table.
myTable.Columns.Add(myColumn)
' Add 10 rows and set values.
Dim myRow As DataRow
Dim i As Integer
For i = 0 To 9
myRow = myTable.NewRow()
myRow("Price") = i + 1
' Be sure to add the new row to the DataRowCollection.
myTable.Rows.Add(myRow)
Next i
Return myTable
End Function
Private Sub DataGrid1_ItemDataBound...
If e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.Item Then
Dim rblTest As RadioButtonList = CType(e.Item.FindControl("rblTest"), RadioButtonList)
rblTest.DataSource = MakeTable()
rblTest.DataTextField = "Price"
rblTest.DataValueField = "Price"
rblTest.SelectedValue = 1
rblTest.DataBind()
End If
End Sub
Private Sub Button1_Click...
For Each Ctrl As Control In Me.DataGrid1.Items
Dim rblTest As RadioButtonList = CType(Ctrl.FindControl("rblTest"), RadioButtonList)
Response.Write(rblTest.SelectedValue.ToString & "<br>")
Next
End Sub
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment