Make Single RadioButton Selection in Gridview using Javascript

Problem

Here, I will be adding two columns in a GridView. One will contain a RadioButton, and the other one will have some text. The user however, can select only one RadioButton at a time. Multi-select needs to be disabled. The problem is that each row contains a new RadioButton so the user was able to select all the RadioButtons.

Solution

Solution is only one thing, javascript. On click on one of the radio button, all other radiobuttons should be disabled. The javascript code is as follows:-


<script language="javascript" type="text/javascript">
	function SelectSingleRadiobutton(rdbtnid) {
		var rdBtn = document.getElementById(rdbtnid);
		var rdBtnList = document.getElementsByTagName("input");
		for (i = 0; i < rdBtnList.length; i++) {
			if (rdBtnList[i].type == "radio" && rdBtnList[i].id != rdBtn.id)
			{
				rdBtnList[i].checked = false;
			}
		}
	}
</script>

The definition of Grid is defined below. Note: the javascript call for the radio Button:-


<asp:GridView ID="gvdata" runat="server" CssClass="Gridview" AutoGenerateColumns="false" >
	<Columns>
		<asp:TemplateField>
			<ItemTemplate>
				<asp:RadioButton id="rdbUser" runat="server" OnClick="javascript:SelectSingleRadiobutton(this.id)" />
			</ItemTemplate>
		</asp:TemplateField>
		<asp:BoundField DataField="SomeData" HeaderText="Some Text"/>
	</Columns>
</asp:GridView>

 

Thanks…

 

Advertisements

One thought on “Make Single RadioButton Selection in Gridview using Javascript

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s