Select a single radio button from a grid using javascript in ASP.NET

Posted: August 4, 2008 in ASP.NET, Javascript
Tags: , ,

How to manage a radio button in a Gridview? I have tried to select a single radio button out of multiple radio buttons in a Gridview. You can perform this using two ways. The first one is to use server side code. Use checked change property of radio button and find out which radio button has raised the event? and select that radio button; and make others uncheck. You need to set the “AutoPostBack” property of radio button to true to handle “OnCheckedChanged” event.

However, the another way to do is by using javascript. You need to write the following javascript in an aspx page where you are using your Gridview.


function CheckOnOff(rdoId,gridName)
{
    var rdo = document.getElementById(rdoId);
    /* Getting an array of all the "INPUT" controls on the form.*/
    var all = document.getElementsByTagName("input");
    for(i=0;i<all.length;i++)
    {  
        /*Checking if it is a radio button, and also checking if the
        id of that radio button is different than "rdoId" */
        if(all[i].type=="radio" && all[i].id != rdo.id)
        {
            var count=all[i].id.indexOf(gridName);
            if(count!=-1)
            {
               all[i].checked=false;
            }
        }
     }
     rdo.checked=true;/* Finally making the clicked radio button CHECKED */
}

You have to set the following properties of a radio button in a Gridview, to allow this script run properly.

<asp:GridView ID="myGrid" runat="server" AutoGenerateColumns="false" >
    <Columns>
        <asp:TemplateField HeaderText="Select">
            <ItemTemplate>
                <asp:RadioButton id="rdobutton"  runat="server" OnClick="javascript:CheckOnOff(this.id,'myGrid');"></asp:RadioButton>           </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Advertisements
Comments
  1. junor says:

    Nice bit o code! Thanks with this code and some of the snippets I pulled from http://www.aspnettutorials.com I will be able to finish my projects in no time at all!

  2. suman says:

    var count=all[i].id.indexOf(gridName);
    if(count!=-1)
    {
    all[i].checked=false;
    }
    In the above lines why do we need to check the index of gridName

  3. hspinfo says:

    Thanks Suman,

    I am passing the “girdName” in a function, so that if there are more than one datagrid exists along with radio buttons in each, then I can get the exact radio button within a specific grid.

    It might possible that user might be using a usercontrol, in which he/she has put a grid with radio button. So, when the page is rendered, the ID of the grid will be change (to its ClientID), but will contain the “gridName” within that clientID. so I am checking here if the radio button which is clicked is from the specific grid that I want.

  4. suman says:

    i have a grid view of structure as below

    <asp:RadioButton ID=”RadioButton1″ GroupName=”rdoQuestion” runat=”server” Text=” />

    <asp:RadioButton ID=”RadioButton3″ runat=”server” Text=” GroupName=”rdoQuestion” />

    <asp:RadioButton ID=”RadioButton4″ runat=”server” GroupName=”rdoQuestion” Text=” />

    and there is button on the form.If the user doesnt give any question the label should be visible and as well if the user gives the option then that label visiblity should go off and i need to accomplish at client side only by means of javascript

  5. hspinfo says:

    Hi Suman,

    Sorry for my late reply,
    To achieve this task you need to check the following code and replace it.

    /*Old code*/
    if(all[i].type==”radio” && all[i].id != rdo.id)
    {
    var count=all[i].id.indexOf(gridName);
    if(count!=-1)
    {
    all[i].checked=false;
    }
    }

    /*New Code*/
    if(all[i].type==”radio” && all[i].id == rdo.id)
    {
    var count=all[i].id.indexOf(gridName);
    if(count!=-1 && all[i].checked = true)
    {
    document.getElementById(‘yourlabel_ID’).style.display = ”;
    /*or */
    document.getElementById(‘yourlabel_ID’).visible = true;
    }
    }

    hope this will solve your query….

  6. EJocys says:

    <asp:RadioButton ID=”DefaultRadioButton” runat=”server”
    GroupName=”DefaultItems”
    OnClick=”DefaultRadioButton_OnClick(this, {GridName: ‘LinkItemsListView’});”
    />

    JavaScript:

    function DefaultRadioButton_OnClick(sender, e) {
    // Getting an array of all the "input" controls.
    var items = document.getElementsByTagName("input");
    for (var i = 0; i -1
    && item != sender && item.checked) {
    // Uncheck item.
    item.checked = false;
    }
    }
    }

  7. EJocys says:

    <asp:RadioButton ID=”DefaultRadioButton” runat=”server”
    GroupName=”DefaultItems”
    OnClick=”DefaultRadioButton_OnClick(this, {GridName: ‘LinkItemsListView’});”
    />

    JavaScript (properly posted):


    function DefaultRadioButton_OnClick(sender, e) {
        // Getting an array of all the "input" controls.
        var items = document.getElementsByTagName("input");
        for (var i = 0; i < items.length; i++) {
            var item = items[i];
            // If it is a radio button, belongs to
    grid,
            // not a sender and checked then...
            if (item.type == "radio" &&
    item.id.indexOf(e.GridName) > -1
                && item != sender
    && item.checked) {
                // Uncheck item.
    item.checked = false;
            }
        }
    }

  8. EJocys says:

    Old code:

    // If it is a radio button, belongs to grid,
    // not a sender and checked then…
    if (item.type == “radio”
    && item.id.indexOf(e.GridName) > -1
    && item != sender
    && item.checked) {
    // Uncheck item.
    item.checked = false;
    }

    New code:

    // If it is a radio button, belongs to grid,
    // have same name, not a sender and checked then…
    if (item.type == “radio”
    && item.id.indexOf(“_”+e.GridName) > -1
    && item.id.indexOf(“_”+e.ButtonName) > -1
    && item != sender
    && item.checked) {
    // Uncheck item.
    item.checked = false;
    }

  9. Sid says:

    Hi hspinfo,
    I tried your code to do something similar but using a checkbox instead of a radiobutton. The checkbox is inside a gridview. This is how I tweaked the javascript:

    function CheckOnOff(spanChk,gridName)
    {
    var IsChecked = spanChk.checked;
    if (IsChecked)
    {
    spanChk.parentElement.parentElement.style.backgroundColor=’#228b22′;
    spanChk.parentElement.parentElement.style.color=’white’;
    }
    else
    {
    spanChk.parentElement.parentElement.style.backgroundColor=’white’;
    spanChk.parentElement.parentElement.style.color=’black’;
    }
    }

    During an autopostback, the item loses the hiliting. Is there a way to tweak this script?

  10. JOHNY BENTS says:

    I think this Article very good, very nice ..

  11. beto says:

    Muchas gracias!!! Funciona perfectamente.
    thanks!!! it works.. Nice!!…..

  12. akt says:

    Really Really Helpful THZ A LOT

  13. Mandar says:

    Fantastic work mann…
    saved my day… I was trying to avoid code on “OnCheckedChanged” . it was my last option.
    And then I got java script from you… !!!
    Thanks Dost (Friend)…!!!

    Mandar

  14. skillz says:

    thanks for existing :)) I’ve been trying to do this all day 😐

  15. Jasna Shyam says:

    Thanks a lot for this bit of code….

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