Use radion button to select the row of the DataGrid (use radio buttons in the DataGrid)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.