开发者

Mutually exclusive selection of Radiobutton in gridView.ASP.NET C#

    <asp:TemplateField HeaderText="Select One">

    <ItemTemplate&g开发者_开发技巧t;
    <asp:RadioButton ID="RadioButton1" runat="server" />

    </ItemTemplate>

    </asp:TemplateField> 

aspx.cs

protected void Button1_Click(object sender, EventArgs e)
{
    foreach (GridViewRow di in GridView1.Rows)
    {
        RadioButton rad = (RadioButton)di.FindControl("RadioButton1");

        if (rad.Checked&&rad!=null)
        {
            s = di.Cells[1].Text;
        }

    }

    Response.Redirect("applicants.aspx?form=" +s);

}

I'm selecting the rows that are selected with this but I have a problem here I want user to be able to select only one radiobutton but its allowing all the radiobuttons to be selected at once.Can you help me in removing this problem please. please.

Mutually exclusive selection of Radiobutton in gridView.ASP.NET C#


Maybe I'm too late here on the party but this will do the trick, check out here......

This might be useful for someone watching this answer in the future.


In the ASP page you need to set the GroupName property to be the same for all the radio buttons, e.g.:

<asp:RadioButton ID="RadioButton1" runat="server" GroupName="RadioGroup" />


well for that you can use follwing code first of all you should define the groupName.The follwing code will work

 <asp:RadioButton ID="RadioButton1" OnCheckedChanged="rbSelector_CheckedChanged" AutoPostBack="true" GroupName="Apply" runat="server"></asp:RadioButton>

C#

protected void rbSelector_CheckedChanged(object sender, System.EventArgs e)
{
    foreach (GridViewRow oldrow in GridView2.Rows)
    {
        ((RadioButton)oldrow.FindControl("RadioButton1")).Checked = false;
    }


    //Set the new selected row
    RadioButton rb = (RadioButton)sender;
    GridViewRow row = (GridViewRow)rb.NamingContainer;
    ((RadioButton)row.FindControl("RadioButton1")).Checked = true;
}


You can try this link to select single radiobutton in grid : http://www.c-sharpcorner.com/uploadfile/krishnasarala/select-single-radio-button-in-gridview-in-Asp-Net/


Using the GroupName property by itself won't work, each radio button will still get a unique name attribute since they're in different rows of the grid.

One option is to emit the radio button markup manually using a Literal control (example). This will make it easy to group the radio buttons on the client-side, but requires a bit more work to determine which button was selected on postback.

When I needed this behavior, I found it easier to keep the radio buttons as server-side controls, and just enforce the button group w/ jQuery. Put your RadioButton in a TemplateField as you've shown, then add this code to uncheck all the other buttons when one is checked:

$(document).ready(function () {
    // could also pass in a unique ID selector
    createManualRadioButtonGroupForGridView(".myGridViewClass");
});

function createManualRadioButtonGroupForGridView(gridViewSelector) {
    $(gridViewSelector + " input[type=radio]").change(function () {
        var checkedRadioButton = this;
        $(gridViewSelector + " input[type=radio]").each(function (e) {
            if (this !== checkedRadioButton) {
                $(this).prop("checked", false);
            }
        });
    });
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜