Problems with Radio button in the datalist

974 views Asked by At

Guys before asking this questions I did a lot of research on this topic but I am not able to detect the problem.I have radio button in the datalist and I am trying to get the selected radio button value.But it is showing me false for all radio button on submit button.My datalist is like this:

<asp:DataList ID="dlEmails" RepeatLayout="Flow" runat="server" >
        <HeaderTemplate>
            <table>
                <tr>
                    <th>Select Email Address </th>

                    <th>Status</th>

                </tr>
        </HeaderTemplate>
        <ItemTemplate>

            <tr>
                <td>


                    <asp:RadioButton ID="rbtnSelect"  Text='<%#Eval("Emails") %>' onclick='fnrad(this);' GroupName="a" Checked='<%#Eval("Primary") %>' runat="server" /><br />
                    (<asp:Label ID="lablel" runat="server" Text='<%#Eval("Verified") %>'> </asp:Label>)
                </td>

                <td valign="middle">
                    <asp:Label ID="lblID" Style="display: none;" runat="server" Text='<%#Eval("Id") %>'> </asp:Label>
                    <asp:Label ID="Label1" runat="server" Text='<%#Eval("Main") %>'> </asp:Label>

                </td>

            </tr>


        </ItemTemplate>
        <FooterTemplate>
            </table>

        </FooterTemplate>
    </asp:DataList>

Javascript for allowing only single radio button selection at a time is like this:

   <script>
    function fnrad(rbtn) {


        var radioList = document.getElementsByTagName("input");

        for (var i = 0 ; i < radioList.length; i++) {

            if (radioList[i].type == "radio") {

                radioList[i].name = 'a';
                radioList[i].checked = false;

            }
        }

        rbtn.checked = true;
        rbtn.setAttribute("Checked","checked");

    }


</script>

And my code behind is like this:

public partial class Member_EmailList : System.Web.UI.Page
{
    EmailsBAL _mbl = new EmailsBAL(SessionContext.SystemUser);
    DataSet _ds = new DataSet();
    URLMessage URLMessage = new URLMessage();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            LoadData();
        }
    }

    private void LoadData()
    {
        _mbl.LoadByUser(_ds, 1);//SessionContext.SystemUser;
        dlEmails.DataSource = _ds.Tables[_mbl.SqlEntityX];
        dlEmails.DataBind();
    }
    protected void lnkConfirm_Click(object sender, EventArgs e)
    {
        RadioButton rb;
        Label lbl;
        int id = 0;
        foreach (DataListItem di in dlEmails.Items)
        {
            rb = (RadioButton)di.FindControl("rbtnSelect");


                if (rb.Checked == true)
                {
                    lbl = (Label)di.FindControl("lblID");
                    id = WebHelper.Cast(lbl.Text, 0);

                }


        }

        //Response.Redirect("~/Member/ConfirmEmail.aspx?" + URLMessage.Encrypt("SystemUser=" + SessionContext.SystemUser + "Id=" + id.ToString()));    

    }}
1

There are 1 answers

0
Chris On

I tried your javascript function and experienced your problem

I would like to suggest to use Jquery on your javascript function. I refactored your jav fuction to this.

function fnrad(rbtn) {

    $('input').removeAttr('checked');

    $(rbtn).prop('checked', true);

}

This works perfectly on my end. Please let me know if you still encountering the issue.