User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the ASP.NET section within the Web Development category of DaniWeb, a massive community of 375,238 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,112 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our ASP.NET advertiser: Lunarpages ASP Web Hosting
Views: 1372 | Replies: 9
Reply
Join Date: Feb 2008
Posts: 24
Reputation: brightline is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
brightline brightline is offline Offline
Newbie Poster

Question Radiobutton Problem

  #1  
Mar 13th, 2008
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.
Last edited by peter_budo : Mar 27th, 2008 at 4:03 am. Reason: Extensive use of colour tags is not good idea
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Mar 2008
Posts: 2
Reputation: john8098 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
john8098 john8098 is offline Offline
Newbie Poster

Solution Re: Radiobutton Problem

  #2  
Mar 14th, 2008
dim ctl as control
for each ctl in controls
if type of ctl is radiobutton then
if ctl.name <>radiobuttonSELECTED.name then
ctl.check=false
endif
endif
next

i think this may solve your problem. thank's me.
Last edited by john8098 : Mar 14th, 2008 at 10:48 am.
Reply With Quote  
Join Date: Feb 2005
Location: Braintree, UK
Posts: 1,161
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Rep Power: 7
Solved Threads: 58
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: Radiobutton Problem

  #3  
Mar 14th, 2008
Dont have the radio button in the control. Use a repeater to render radio button, user control pairs.
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote  
Join Date: Nov 2006
Location: Nasik-India
Posts: 21
Reputation: sierrasoft is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
sierrasoft sierrasoft is offline Offline
Newbie Poster

Re: Radiobutton Problem

  #4  
Mar 15th, 2008
Hi,

You can set the GroupName Property of the Radio Button. Check this topic in MSDN--RadioButton.GroupName Property
Reply With Quote  
Join Date: Mar 2008
Posts: 20
Reputation: ravichandra is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 4
ravichandra ravichandra is offline Offline
Newbie Poster

Help Radiobutton Problem

  #5  
Mar 17th, 2008
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.
Reply With Quote  
Join Date: Feb 2008
Posts: 24
Reputation: brightline is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
brightline brightline is offline Offline
Newbie Poster

Re: Radiobutton Problem

  #6  
Mar 27th, 2008
ok,please send me this solution
Reply With Quote  
Join Date: Mar 2008
Posts: 20
Reputation: ravichandra is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 4
ravichandra ravichandra is offline Offline
Newbie Poster

Re: Radiobutton Problem

  #7  
Mar 27th, 2008
Hi Bright,
I am at office,it may take 3 hours to reach home.I will post the solution without fail.
Thanks
ravichandra
Reply With Quote  
Join Date: Mar 2008
Posts: 20
Reputation: ravichandra is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 4
ravichandra ravichandra is offline Offline
Newbie Poster

Re: Radiobutton Problem

  #8  
Mar 27th, 2008
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>
            <asp:TemplateField HeaderText="Select">
            <ItemTemplate>
            <asp:Literal ID="RadioButtonMarkup" runat="server"></asp:Literal>
            </ItemTemplate>
            </asp:TemplateField>
            <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
Reply With Quote  
Join Date: Feb 2008
Posts: 24
Reputation: brightline is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
brightline brightline is offline Offline
Newbie Poster

Re: Radiobutton Problem

  #9  
Mar 27th, 2008
Thanx 4 ur interest.
Reply With Quote  
Join Date: Jul 2008
Posts: 1
Reputation: pfdsilva is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
pfdsilva pfdsilva is offline Offline
Newbie Poster

Re: Radiobutton Problem

  #10  
11 Days Ago
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
Last edited by Tekmaven : 11 Days Ago at 3:09 am. Reason: Code tags
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb ASP.NET Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the ASP.NET Forum

All times are GMT -4. The time now is 4:26 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC