Radiobutton is used to select a single option.ArticleThis section describes how to apply this feature to the DataGrid Control.
First, we know that all the controls in the DataGrid will have a single uniqueid after the page is generated, so you put radionbuttonlist as the control directly in the DataGrid, even if you set groupname to the same, but it will be different after running, so this method is difficult to implement. That's not to say no. I have seen someone adding it to the template column.
<Itemtemplate>
<Input type = "radio" id = "rdoaction" runat = "server"/>
</Itemtemplate>
Then, the JS + httpxml technology is used to refresh the entire page and bind the radionbutton to the selected items in JS. This method is also feasible, but the simple problem is complicated.
Next I will introduce a very simple method.
Create a template column in The DataGrid, put a label Web Control in it, and add the following to the itemdatabound event of the DataGrid: Code :
If E. Item. itemtype = listitemtype. alternatingitem or E. Item. itemtype = listitemtype. Item then
Dim R as label
R = E. Item. findcontrol ("label2 ")
R. Text = "<input type = radio name = 'myradiogroup' value =" & E. Item. cells (1). Text & ">"
End if
In this way, the radion button is output in this lable, which enables you to select only one radion button at the same time.
The above is equivalent to completing a part of the task, followed by how to get the value of the selected row.
Add an event to any button
Private sub button#click (byval sender as system. Object, byval e as system. eventargs) handles button1.click
Label3.text = request. Form ("myradiogroup ")
Dim I as datagriditem
For each I in maid
If I. itemtype = listitemtype. alternatingitem or I. itemtype = listitemtype. Item then
Dim R as label
R = I. findcontrol ("label2 ")
If R. Text. indexof (label3.text)> 0 then
R. Text = "<input type = radio name = 'myradiogroup' value =" & I. cells (1). Text & "Checked>"
Else
R. Text = "<input type = radio name = 'myradiogroup' value =" & I. cells (1). Text & ">"
End if
End if
Next
End sub
This ensures that the selected radionbutton is still selected after pageback.
This method solves the problem of using radionbutton in the DataGrid.
Download related code: http://files.cnblogs.com/carlwave/radbuttonindatagrid.rar (VB.net)