How to use radio button in an ASP. NET DataGrid

Source: Internet
Author: User

Mr. deficit was posted on 17:43:39

In a project, to use radiobutton in a DataGrid, a group of radiobutton is displayed in a column, which is mutually exclusive. Only one radiobutton can be selected at a time. For example:

First, try to use the Server Control radiobutton as the control of the DataGrid template column:

<Asp: DataGrid id = "countriesgrid" runat = "server" datakeyfield = "ID" autogeneratecolumns = "false">
<Columns>
<Asp: templatecolumn>
<Itemtemplate>
<Asp: radiobutton id = "selectradiobutton" runat = "server" groupname = "country"/>
</Itemtemplate>
</ASP: templatecolumn>
</Columns>
</ASP: DataGrid>

Here, we try to use groupname = "country" to group the radio buttons in this column so that all the radio buttons in this column become a group.

However, when the page is opened, it is found that the generated radio buttons are not in the same group, and each of them can be selected.
View Source code Found:
<Tr>
<TD> <input id = "countriesgrid _ ctl2_selectradiobutton"
Type = "radio" name = "countriesgrid: _ ctl2: Country"
Value = "selectradiobutton"/>
</TD>
</Tr>
<Tr>
<TD> <input id = "countriesgrid _ ctl3_selectradiobutton"
Type = "radio" name = "countriesgrid: _ ctl3: Country"
Value = "selectradiobutton"/>
</TD>
</Tr>

The name attribute of the radio button is the group name attribute used for grouping. Apparently generated HTMLCodeChanged the group name. Therefore, the radio buttons in this column cannot be a group.

The key to the problem is that the group name of the automatically generated radio button is not the same, so it cannot be a group. If the group name of the radio button is the same, the problem is solved.

There are two solutions as long as the group name of the radio button is the same. For now:

One is to use a non-server-side control (without the runat = "server" control, and the HTML control with runat = "server" does not work ):


value = '<% # databinder. eval (container. dataitem, "integervalue") %> '/>

the HTML code generated using such a control is:

type = "radio" name = "radioname"
value = "1"/>


type =" radio "name =" radioname "
value =" 2 "/>


The Group Name of the generated radio button is changed, therefore, it can be a set of radio buttons.

However, this poses a problem. Because non-server controls are not maintained, this means that after each submission to the server, the selection of the radio button will be lost. After each submission, the previously selected radio button becomes unselected.

OnlineArticleThe solution to this problem is to use a Java Script and add a hidden domain to automatically select the previously selected radio button each time the page is refreshed.
Although it is troublesome, the problem is solved.

But there is another question: how to obtain which radio button is selected? Because these radio buttons are not server-side.
The radio button is used as a form. Each time a page is submitted, you can find the key value with the radioname (radio button group name) as the key in the request. forms set. This key value is the value attribute value of the currently selected radio button.

As in the preceding example, if the first radio button is selected, request. Forms ["radioname"] is "1 ". The second one is "2 ". The radio button is selected.

However, this method cannot obtain which row of the DataGrid's radio button is selected. This is what we usually get when using the DataGrid.
And if we want to useProgramHow to select one of the radio buttons? In ASP. NET, the backend Code cannot obtain the status of a non-Server Control.

Speaking of this, we found that using non-server-side controls can cause many problems. Because it is a non-server-side control, the backend Code cannot access the status of these controls, and we cannot use the datagrid1.items [0]. in the form of controls [1] As radiobutton, obtain the reference of the radio button control to operate the control. We can only use the script method for some processing, so we cannot implement some of the features we want.

In the end, the server-side controls are used.
The problem with the server control is that the group name of the automatically generated radio button is different, so that it cannot be a group.

Method 1:
Since you cannot automatically select a radio button to clear the status of the previously selected radio button, you can use the program method. The event that responds to the status change of the radio button. The event clears the status of other selected radio buttons.

In this way, each time you select a radio button, it will be submitted, obviously it cannot "give users the best experience ".

Method 2:
Some people use radiobuttonlist on the Internet to implement it. Put a control like this in a cell, merge other cells in the column, and manually add the items of radiobuttonlist. This method is very ugly with no aesthetic.

Method 3:
Rewrite a server control derived from radiobutton. When the name attribute is generated, the group name attribute is used, so that the name attribute of each generated radio button is the same.

For details about this method, see the article on codeproject and the source code:
Http://www.codeproject.com/aspnet/How_group_RButtons.asp

This is a perfect solution. Because it is a server control, we can use the datagrid1.items [0]. controls [1] As radiobutton method to access the radio button control for full control.

 

This article summarizes ASP. net's research ideas on using radio button in the DataGrid. The methods in it are all about solving this problem from various perspectives on the Internet, and they are not entirely their own ideas. They are a bit "plagiarized. However, it is helpful to summarize the process of exploring this issue.

 

In this process, I found many articles on this issue on the Internet, but I simply pasted the Code with no comments. I didn't have any ideas on how to solve this problem. Why should I do this, this is to solve the problem. Instead, it depends on the reader to analyze the meaning of the Code. Why should we write it like this. Readers are exhausted and may not see how to solve this problem. In addition, some codes contain bugs.

The authors of such articles are irresponsible and waste a lot of time.

Related Article

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.