•
•
•
•
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
![]() |
•
•
Join Date: Feb 2008
Posts: 24
Reputation:
Rep Power: 1
Solved Threads: 0
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.
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
•
•
Join Date: Mar 2008
Posts: 2
Reputation:
Rep Power: 0
Solved Threads: 0
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.
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.
•
•
Join Date: Feb 2005
Location: Braintree, UK
Posts: 1,161
Reputation:
Rep Power: 7
Solved Threads: 58
Dont have the radio button in the control. Use a repeater to render radio button, user control pairs.
•
•
Join Date: Mar 2008
Posts: 20
Reputation:
Rep Power: 1
Solved Threads: 4
aspx
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
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.
Finally you may click on Button to perform whatever is required for your requirement.
Have alook at the following sample code:
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
<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
•
•
Join Date: Jul 2008
Posts: 1
Reputation:
Rep Power: 0
Solved Threads: 0
The code within Button1_Click function throws a run time exception. Use this one instead.
Thanks
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
}
} Last edited by Tekmaven : 11 Days Ago at 3:09 am. Reason: Code tags
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb ASP.NET Marketplace
Similar Threads
- Python GUI Problem (Python)
- add radiobutton list or checkboxes list (VB.NET)
- Vey hard problem.. no idea how to solve ??? (JavaScript / DHTML / AJAX)
Other Threads in the ASP.NET Forum
- Previous Thread: Reading data from Excell file.
- Next Thread: How to make a SMS Gateway



Linear Mode