I have created a custom user control which contains a radiobutton and some another controls.
In my page, I use more than one instance of this control, so the user can check the radiobutton of all the instances of this control.

the question:
How to force that only one radiobutton to be selected.

Dont have the radio button in the control. Use a repeater to render radio button, user control pairs.

Hi,

You can set the GroupName Property of the Radio Button. Check this topic in MSDN--RadioButton.GroupName Property

I have implemented radio button in a gridview,where in user can select any one of the radiobutton in the gridview.If you need i can post the solution.

ok,please send me this solution

Hi Bright,
I am at office,it may take 3 hours to reach home.I will post the solution without fail.
Thanks
ravichandra

aspx

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
        BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px"
        CellPadding="4" DataKeyNames="ProductID" DataSourceID="SqlDataSource1" PageSize="5"
        Style="z-index: 100; left: 241px; position: absolute; top: 65px" OnRowCreated="GridView1_RowCreated">
        <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
        <RowStyle BackColor="White" ForeColor="#330099" />
        <Columns>
            [B]<asp:TemplateField HeaderText="Select">
            <ItemTemplate>
            <asp:Literal ID="RadioButtonMarkup" runat="server"></asp:Literal>
            </ItemTemplate>
            </asp:TemplateField>[/B]
            <asp:BoundField DataField="ProductID" HeaderText="ProductID" ReadOnly="True" SortExpression="ProductID" />
            <asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" />
        </Columns>
        <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
        <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
    </asp:GridView>

I have used Template field and within the template instead of a radiobutton i have used a Literal control which will be converted into RadioButton in code behind.

The literal control is converted into a HTML radio button and to make the ids of each radiobutton unique i am passing the rowindex of the grid.This makes all the radiobutton unique and user can select any one of the radiobutton.

When the page is displayed no radio button is checked initially.(Note* if you need you can make the first radiobutton as checked by default by removing the commented code //(!Page.IsPostBack && e.Row.RowIndex==0).

aspx.cs

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Literal output = (Literal)e.Row.FindControl("RadioButtonMarkup");
            output.Text=string.Format(@"<input type=radio name='ProductGroup' "+@"id='RowSelector{0}' value='{0}' 

",e.Row.RowIndex);

            if((ProductsSelectedIndex==e.Row.RowIndex))// ||(!Page.IsPostBack && e.Row.RowIndex == 0))

                output.Text += @"checked='checked' ";
            output.Text += "/>";
        

        }
    }

The following property ProductsSelectedIndex is accessed to check whether any of the radiobutton is checked, if none is

checked then it returns -1 or it return an integer value and if this value is equal to the row index then that radiobutton is

made checked.

private int ProductsSelectedIndex
    {
        get
        {
            if (string.IsNullOrEmpty(Request.Form["ProductGroup"]))
                return -1;
            else
                return Convert.ToInt32(Request.Form["ProductGroup"]);
        }
    }

Finally you may click on Button to perform whatever is required for your requirement.

Have alook at the following sample code:

protected void Button1_Click(object sender, EventArgs e)
    {
        if (ProductsSelectedIndex < 0)
        {
            Label2.Text = "Please make a selection";
            Label2.Visible = true;
        }
        else
        {
            int productId = Convert.ToInt32(GridView1.DataKeys[ProductsSelectedIndex].Value);
            Label1.Text = productId.ToString();
            Label2.Visible = false;
        }


    }

If the ProductSelectedIndex < 0, it means none of the radio button is selected and i have used a label control to display
the message "Please make a selection"
If any one the radio button is checked i am retrieving the key values and displaying it in the Label1.

If still you have doubt, i can clarify the issue.


Thanks Mr Scott Mitchel(Microsoft)
Good Luck.

ravichandra

Thanx 4 ur interest.

The code within Button1_Click function throws a run time exception. Use this one instead.

protected void Button1_Click(object sender, EventArgs e)
    {
        if (ProductsSelectedIndex < 0)
        {
            Label2.Text = "Please make a selection";
            
        }
        else
        {
            GridViewRow gd = GridView1.Rows[ProductsSelectedIndex];
            Label2.Text = gd.Cells[0].Text;   // replace 0 with the column whose text you want
        }


    }

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.