Radiobutton Problem

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2008
Posts: 52
Reputation: brightline is an unknown quantity at this point 
Solved Threads: 0
brightline brightline is offline Offline
Junior Poster in Training

Radiobutton Problem

 
0
  #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 5:03 am. Reason: Extensive use of colour tags is not good idea
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 2
Reputation: john8098 is an unknown quantity at this point 
Solved Threads: 0
john8098 john8098 is offline Offline
Newbie Poster

Re: Radiobutton Problem

 
0
  #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 11:48 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 1,181
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Solved Threads: 67
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: Radiobutton Problem

 
0
  #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 Quick reply to this message  
Join Date: Nov 2006
Posts: 32
Reputation: sierrasoft is an unknown quantity at this point 
Solved Threads: 0
sierrasoft sierrasoft is offline Offline
Light Poster

Re: Radiobutton Problem

 
0
  #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 Quick reply to this message  
Join Date: Mar 2008
Posts: 20
Reputation: ravichandra is an unknown quantity at this point 
Solved Threads: 5
ravichandra ravichandra is offline Offline
Newbie Poster

Radiobutton Problem

 
0
  #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 Quick reply to this message  
Join Date: Feb 2008
Posts: 52
Reputation: brightline is an unknown quantity at this point 
Solved Threads: 0
brightline brightline is offline Offline
Junior Poster in Training

Re: Radiobutton Problem

 
0
  #6
Mar 27th, 2008
ok,please send me this solution
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 20
Reputation: ravichandra is an unknown quantity at this point 
Solved Threads: 5
ravichandra ravichandra is offline Offline
Newbie Poster

Re: Radiobutton Problem

 
0
  #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 Quick reply to this message  
Join Date: Mar 2008
Posts: 20
Reputation: ravichandra is an unknown quantity at this point 
Solved Threads: 5
ravichandra ravichandra is offline Offline
Newbie Poster

Re: Radiobutton Problem

 
0
  #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

  1.  
  2. protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
  3. {
  4. if (e.Row.RowType == DataControlRowType.DataRow)
  5. {
  6. Literal output = (Literal)e.Row.FindControl("RadioButtonMarkup");
  7. output.Text=string.Format(@"<input type=radio name='ProductGroup' "+@"id='RowSelector{0}' value='{0}'
  8.  
  9. ",e.Row.RowIndex);
  10.  
  11. if((ProductsSelectedIndex==e.Row.RowIndex))// ||(!Page.IsPostBack && e.Row.RowIndex == 0))
  12.  
  13. output.Text += @"checked='checked' ";
  14. output.Text += "/>";
  15.  
  16.  
  17. }
  18. }

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.

  1.  
  2. private int ProductsSelectedIndex
  3. {
  4. get
  5. {
  6. if (string.IsNullOrEmpty(Request.Form["ProductGroup"]))
  7. return -1;
  8. else
  9. return Convert.ToInt32(Request.Form["ProductGroup"]);
  10. }
  11. }


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

Have alook at the following sample code:

  1.  
  2. protected void Button1_Click(object sender, EventArgs e)
  3. {
  4. if (ProductsSelectedIndex < 0)
  5. {
  6. Label2.Text = "Please make a selection";
  7. Label2.Visible = true;
  8. }
  9. else
  10. {
  11. int productId = Convert.ToInt32(GridView1.DataKeys[ProductsSelectedIndex].Value);
  12. Label1.Text = productId.ToString();
  13. Label2.Visible = false;
  14. }
  15.  
  16.  
  17. }

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 Quick reply to this message  
Join Date: Feb 2008
Posts: 52
Reputation: brightline is an unknown quantity at this point 
Solved Threads: 0
brightline brightline is offline Offline
Junior Poster in Training

Re: Radiobutton Problem

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

Re: Radiobutton Problem

 
0
  #10
Jul 16th, 2008
The code within Button1_Click function throws a run time exception. Use this one instead.

  1. protected void Button1_Click(object sender, EventArgs e)
  2. {
  3. if (ProductsSelectedIndex < 0)
  4. {
  5. Label2.Text = "Please make a selection";
  6.  
  7. }
  8. else
  9. {
  10. GridViewRow gd = GridView1.Rows[ProductsSelectedIndex];
  11. Label2.Text = gd.Cells[0].Text; // replace 0 with the column whose text you want
  12. }
  13.  
  14.  
  15. }
Thanks
Last edited by Tekmaven; Jul 16th, 2008 at 4:09 am. Reason: Code tags
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the ASP.NET Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC