asp.net radio button grouping

心已入冬 提交于 2019-11-27 20:04:14
fizch

I finally got around this by creating a plain radio button and setting the value using an server-side eval.

<input type="radio" name="radCustomer" value='<%#Eval("CustomerNumber") %>' />

Now when the application performs a postback, I check for the value of Request.Form["radCustomer"]. This works flawlessly.

CAbbott

Unfortunately, this is a well known issue with radio buttons within a repeater. One of your only options would be to create a custom server control derived from the RadioButton class and override how it renders.

EDIT: Here's a sample of what the derived class may look like:

public class MyRadioButton : RadioButton
{
    protected override void Render(HtmlTextWriter writer)
    {
        writer.Write("<input id=\"" + base.ClientID + "\" ");
        writer.Write("type=\"radio\" ");
        writer.Write("name=\"" + base.ID + "\" ");
        writer.Write("value=\"" + base.ID + "\" />");
        writer.Write("<label for=\"" + base.ClientID + "\">");
        writer.Write(base.Text);
        writer.Write("</label>");
    }
}

I fixed it in javascript

$(document).ready(function () {
        $("#divWithGridViewOrRepeater input:radio").attr("name", "yourGroupName");
    }); 

I had the same issues. I am using Literal as placeholder to render radio button onItemCreated event.

ASP.Net

<asp:Repeater ID="rpt" runat="server" OnItemCreated="rpt_OnItemCreated">
    <ItemTemplate>
        <asp:Literal ID="lit" runat="server"></asp:Literal>
    </ItemTemplate>
</asp:Repeater>

C#

protected void rpt_OnItemCreated(object sender, RepeaterItemEventArgs e) {
    Literal lit = (Literal)e.Item.FindControl("lit");
    lit.Text = "<input type=\"radio\" name=\"myGroup\">";
}

I had to modify slightly the answer posted above by r3dsky.

Here's what worked for me:

$(document).ready(function () {
        $(".divWithGridViewOrRepeater input:radio").attr("name", "yourGroupName");
    }); 

I would start by adding a value on my radiobutton Value='<%#Eval("CustomerNumber") %>'.

I made my radiobutton have autopostback set to true, and then in the event handler set all the other radio buttons to BE unselected.

Not ideal, but I need lots control over the visibility and enabled attributes of the radiobutton, and it seemed easier to let ASP.NET control that rather than resorting to client side script.

hmfarimani

This is a well known bug with the ASP.NET Repeater using RadioButtons: here best solution in my opinion

I did this:

$("input:radio").attr("name", $("input:radio").first().attr("name"));

Why? because if you replace the name property for any string you want, you will get an 'not found error'. So, you need to get the name of the first radiobutton, and rename all of them with that name. It works like a sharm ;)

My solution, similar to others:

<input id="ctlRadio" runat="server" type="radio" data-fixgroupbug="1" >

// Fixes this ASP.NET bug: if radio input is inside repeater you can't set its name.
// Every input gets set different name by ASP.NET.
// They don't behave as a group. You can select multiple radios.
function fixRadiogroupBug()
{
    $('[type="radio"][data-fixgroupbug]').click(function () {
        $(this).siblings('[type="radio"]').prop('checked', false);
    });
}

$(document).ready(function () {
    fixRadiogroupBug();
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!