I have two radio buttons for metric and US measurements. I load the page so the metric radio button is clicked. How do I set the two buttons so when US is clicked metric unclicks and vise versa?
In order to make it work, you have to set property GroupName of both radio buttons to the same value:
<asp:RadioButton id="rbMetric" runat="server" GroupName="measurementSystem"></asp:RadioButton>
<asp:RadioButton id="rbUS" runat="server" GroupName="measurementSystem"></asp:RadioButton>
Personally, I prefer to use a RadioButtonList:
<asp:RadioButtonList ID="rblMeasurementSystem" runat="server">
<asp:ListItem Text="Metric" Value="metric" />
<asp:ListItem Text="US" Value="us" />
</asp:RadioButtonList>
Make sure their GroupName properties are set to the same name:
<asp:RadioButton GroupName="MeasurementSystem" runat="server" Text="US" />
<asp:RadioButton GroupName="MeasurementSystem" runat="server" Text="Metric" />
I can see it's an old question, if you want to put other HTML inside could use the radiobutton with GroupName propery same in all radiobuttons and in the Text property set something like an image or the html you need.
<asp:RadioButton GroupName="group1" runat="server" ID="paypalrb" Text="<img src='https://www.paypalobjects.com/webstatic/mktg/logo/bdg_secured_by_pp_2line.png' border='0' alt='Secured by PayPal' style='width: 103px; height: 61px; padding:10px;'>" />
<asp:RadioButtonList id="RadioButtonList1" runat="server">
<asp:ListItem Selected="True">Metric</asp:ListItem>
<asp:ListItem>US</asp:ListItem>
</asp:RadioButtonList>
Set the GroupName property of both radio buttons to the same value. You could also try using a RadioButtonGroup, which does this for you automatically.
Related
We have a RadioButtonList control inside a Repeater control with two options, Yes or No.
We would like to force our users to choose a Yes or No before proceeding.
In other words, if a user clicks to submit or in our case, click the Next button to go to the next without selecting either Yes or No, an error needs to be raised letting the user know that s/he must select a Yes or No to continue.
The following code uses RequiredValidator control to handle this but it does not do anything.
Any ideas how to resolve this?
Purchased:<asp:radiobuttonlist ID="rblType" runat="server" ValidationGroup ="stype" RepeatDirection="Horizontal" TextAlign="Right" style="display:inline;">
<asp:ListItem Text="Yes" />
<asp:ListItem Text="No" />
</asp:RadioButtonList>
<asp:RequiredFieldValidator id="RequiredFieldValidator1"
ControlToValidate="rblType"
ErrorMessage="required"
ValidationGroup ="stype"
runat="server"/>
Thanks a lot in advance.
I have an ASP.NET web application. I have one TextBox (txtProductName) and one DropDownList(ddlChanges) visible at the time of page load. Also there is a TextBox(txtDeliveryStatus) and a Label (lblDeliveryStatus) invisible at page load.
<asp:TextBox ID="txtProductName" runat="server" CssClass="selectstyle" MaxLength="8" Width="60" />
<asp:Label ID="lblDeliveryStatus" runat="server" Style="visibility: hidden;">Delivery Status : </asp:Label>
<asp:TextBox ID="txtDeliveryStatus" runat="server" CssClass="selectstyle" Style="visibility: hidden;"/>
<asp:DropDownList runat="server" ID="ddlChanges" CssClass="selectstyle" onchange="javascript:ddlChangesChanged();">
<asp:ListItem Text="-- Select -- " Value="-1"></asp:ListItem>
<asp:ListItem Text="Status Change" Value="1"></asp:ListItem>
<asp:ListItem Text="Product Name Change" Value="2"></asp:ListItem>
<asp:ListItem Text="Category Change" Value="3"></asp:ListItem>
</asp:DropDownList>
I will make the Label and TextBoxfor delivery status visible when user selects Status Change from the DropDownList. But the problem is that the Label and TextBox for delivery status are invisible and still they take up some space on the page between visible controls.
How to remove that space of invisible controls?
the reason the white space is still showing after you control.Visible = false is because the "br" newLine tags are remaining. Wrap them with your button tag and hide both to truly remove the white space.
You should use the display attribute:
<asp:Label ID="lblDeliveryStatus" runat="server" Style="display: none;">Delivery Status : </asp:Label>
<asp:TextBox ID="txtDeliveryStatus" runat="server" CssClass="selectstyle" Style="display: none;"/>
For completeness you can check this link.
You can use visible attribute of control.
To hide:
Control.Visibile=false;
Whenever you want to display
Control.Visible=true;
I have an aspx page (C# code page). I have some hard coded dropdownlists and for some reason they are not displaying the top list item (value). I added an extra top list item (value) and now the correct values display for the values already in it, but that extra one does not display.
The only functionality I do with my dropdownlists in my C# code is to hide or show them. And then do validation or not based on the selected value.
My aspx code:
<asp:DropDownList ID="ddlAction" runat="server" Visible="True"
AppendDataBoundItems="true" Height="25px" Width="149px">
<asp:ListItem Value="Select">Please Select</asp:ListItem>
<asp:ListItem>Yes</asp:ListItem>
<asp:ListItem>No</asp:ListItem>
</asp:DropDownList>
C# Code:
ddlAction.Visible = false;
ddlAction.Visible = true;
I use dropdownlist's regularly and have never had this problem before. Does anyone have any ideas what the issue could be?
UPDATE TO THIS ISSUE:
I added my items in my C# code as per Rahul. Did a quick test and it worked.
Now this morning, I am once again getting blanks for the first item ("Please Select").
Aspx code:
<asp:DropDownList ID="ddlAction" runat="server"
AppendDataBoundItems="True" Height="27px" Width="159px">
</asp:DropDownList>
C# code:
ddlAction.Visible = true;
ddlAction.AppendDataBoundItems = true;
ddlAction.Items.Insert(0, new ListItem("Please Select","Select"));
ddlAction.Items.Insert(1, new ListItem("Yes", "Yes"));
ddlAction.Items.Insert(2, new ListItem("No", "No"));
ddlAction.DataBind();
Rendered source code:
<select name="ctl00$ContentPlaceHolder1$ddlAction" id="ContentPlaceHolder1_ddlAction" style="height:27px;width:159px;">
<option selected="selected" value="Select"></option>
<option value="Yes">Yes</option>
<option value="No">No</option>
Try to use AppendDataBoundItems = true property of DropSownList into your .aspx page.
you may also assign value from code behind as well like
ddlAction.Items.Insert(0, new ListItem(String.Empty, String.Empty));
use AppendDataBound = true in your aspx coe.
<asp:DropDownList ID="ddlAction" AppendDataBound = true runat="server" Visible="True" Height="25px"
Width="149px">
<asp:ListItem Value="Select">Please Select</asp:ListItem>
<asp:ListItem>Yes</asp:ListItem>
<asp:ListItem>No</asp:ListItem>
</asp:DropDownList>
Edit 1
More detail about List Item
<asp:ListItem Value="-2" Text="Please Select"></asp:ListItem>
<asp:ListItem Value="0" Text="Yes"></asp:ListItem>
<asp:ListItem Value="-1" Text="No"></asp:ListItem>
I suggest you declare your DropDownList ListItems using its internal properties and defining what ListItem must be the selected one:
<asp:DropDownList ID="ddlAction" runat="server" Visible="True" AppendDataBoundItems="true" Height="25px" Width="149px">
<asp:ListItem Text="Please Select" Value="Select" Selected="True"></asp:ListItem>
<asp:ListItem Text="Yes" Value="Yes"></asp:ListItem>
<asp:ListItem Text="No" Value="No"</asp:ListItem>
</asp:DropDownList>
It's the way ASP.NET uses to work and will return you the right selected value on the server side on postbacks.
I think that you don't have to use nor the DataBind() method neither set the AppendDataBoundItems, because, you already inserted the ListItems and you aren't loading options from a database!
I think you need to tell what ListItemIndex is the selected one by seting a value to the DropDownList.SelectedIndex property.
EDIT
Also, try to read to MSDN documentation about the AppendDataBoundItems property and the enter link description here method.
I have two questions that are in the form of radio button lists in C# web application. These questions have required field validators associated to them and I also have a ValidationSummary at the end of my web control. When I click on my "Submit" button, the two required field validations appear for each of the questions right next to it and the validationsummary works correctly by stating "Answer the following questions:" and then listing each of the questions that were not selected. The problem I'm having is when I select one of the two questions, the "Required Field" message disappears next to the question, but not in the ValidationSummary. How can I get the ValidationSummary to update or refresh when one of the questions in the error messages are selected? Please let me know if I need to be more specific. Thanks for your help!
<asp:RadioButtonList ID="Question1" RepeatLayout="Flow" runat="server">
<asp:ListItem Text="Yes" Value="Yes"></asp:ListItem>
<asp:ListItem Text="No" Value="No"></asp:ListItem>
</asp:RadioButtonList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
Text="Required" ErrorMessage="Question 1"
Display="Dynamic" ControlToValidate="Question1"
EnableClientScript="true">
</asp:RequiredFieldValidator>
<asp:RadioButtonList ID="Question2" RepeatLayout="Flow" runat="server">
<asp:ListItem Text="Yes" Value="Yes"></asp:ListItem>
<asp:ListItem Text="No" Value="No"></asp:ListItem>
</asp:RadioButtonList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
Text="Required" ErrorMessage="Question 2"
Display="Dynamic" ControlToValidate="Question2"
EnableClientScript="true">
</asp:RequiredFieldValidator>
<asp:ValidationSummary ID="ValidationSummary1" runat="server"
HeaderText="Answer the following questions:"
DisplayMode="BulletList"
EnableClientScript="true"/>
<asp:Button ID="buttonSubmit" runat="server" Text="Submit"
OnClick="btnSubmit_OnClick"/>
//Code behind for button
protected void btnSubmit_OnClick(object sender, EventArgs e)
{
Response.BufferOutput = true;
Response.Redirect("~/Page.aspx");
}
I'm pretty sure the summary doesn't dynamically update by default, but rather only on form submit.
Ordinarily, you could get around this by hooking the client-side onblur event and calling ValidationSummaryOnSubmit(), but it looks like the RadioButtonList doesn't trigger this event when the selected radio button changes.
The next candidate would be the client-side onClick event, but the RadioButtonList control doesn't have an OnClientClick event.
So, the solution which worked for me when I tested it was to hook the client-side onClick event for each ListItem in the RadioButtonList. Like this:
<asp:RadioButtonList ID="Question1" RepeatLayout="Flow" runat="server">
<asp:ListItem Text="Yes" Value="Yes" onClick="ValidationSummaryOnSubmit()"></asp:ListItem>
<asp:ListItem Text="No" Value="No" onClick="ValidationSummaryOnSubmit()"></asp:ListItem>
</asp:RadioButtonList>
However, this will get rapidly ugly if you've got a bunch of these that you're going to want to do. Might consider subclassing RadioButtonList and adding a server-side addition of the ListItem onclick hook to each of the child listitems before sending it to the client, and then using your custom version of RadioButtonList instead of the stock version.
I have a combo box in a Form View and onchange i want to access a javascript function just as i'd normally do any drop down list. However, it doesn't seem to be even getting to the function
function Showused()
{
alert('eric');
}
<telerik:RadComboBox ID="RadComboBoxProvided" onchange="javascript: Showused();" runat="server" Width="50px" >
<Items>
<telerik:RadComboBoxItem runat="server" Text="Yes" Value="Y" />
<telerik:RadComboBoxItem runat="server" Text="No" Selected="true" Value="N" />
</Items>
</telerik:RadComboBox>
Simple javascript call. Any idea why this doesn't work?
The client-side event names are different for Telerik controls. The RadComboBox event for selected index changed (assuming you're using a recent version of the controls) is OnClientSelectedIndexChanged
You may want to consult the client-side programming guide for RadComboBox, or the list of client-side events.
Here's a sample for use with your example:
Javascript:
function SelectedIndexChanged(sender, eventArgs) {
var item = eventArgs.get_item();
alert("You selected " + item.get_text());
}
Markup:
<telerik:RadComboBox ID="RadComboBoxProvided" OnClientSelectedIndexChanged="SelectedIndexChanged" runat="server" Width="50px" >
<Items>
<telerik:RadComboBoxItem runat="server" Text="Yes" Value="Y" />
<telerik:RadComboBoxItem runat="server" Text="No" Selected="true" Value="N" />
</Items>
</telerik:RadComboBox>
is not a combo box, it is a custom tag, that will convert itself to an HTML combo box, to be sure what is happening there, run your server and go to that page, and then right click on the page in your browser and look at the HTML source, and finally try to locate that combo box and see how it is really rendered.