Radionbuttons checking in ASP.NET - c#

It's possible check if one of radio buttons named of 'foo' is selected? For example:
<input type="radio" name="foo" id="baa" value="..." runat="server" />
<input type="radio" name="foo" id="baa1" value="....." runat="server" />
the question is: Is there some <asp:.. that can test if one(not both, only one can be selected) of above checkbox if selected?
I hope this is clear. Thanks in advance.

EDIT
For you requirement it better to go for RadioButtonList with RequiredFieldValidator. Below is example
<asp:RadioButtonList
ID="RadioButtonList1"
runat="server"
RepeatColumns="3">
<asp:ListItem>abcd</asp:ListItem>
<asp:ListItem>xyz</asp:ListItem>
</asp:RadioButtonList>
<asp:RequiredFieldValidator
ID="ReqiredFieldValidator1"
runat="server"
ControlToValidate="RadioButtonList1"
ErrorMessage="select atleast one radiobutton!">*
</asp:RequiredFieldValidator>
You need to group radio button GroupName="foo" is group in which two radio button resides , so at a time only one get slected
<asp:panel runa="server" id="container">
<asp:RadioButton id="Radio1" GroupName="foo"
Text="Beef" BackColor="Pink" runat="server"/>
<br />
<asp:RadioButton id="Radio2" GroupName="foo"
Text="Pork" BackColor="Pink" runat="server"/>
</asp:panel>
Note even in your html you need to specify groupname for the radio button to select one out of the group of radio button
To check ratio button selected or not just use this linq operation
bool isradchecked=container.Controls.OfType<RadioButton>)
.Any(r => r.Checked);
here container of radiobutton is Asp:Panel.

Related

Creating a Label for a checkbox inside a repeater

<asp:Repeater runat="server" ID="Repeater1" OnItemDataBound="countryDataBound">
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
<div class="accordion">
<span><h3><%# Eval("key") %>
<div class="panelCountry">
<input type="checkbox" runat="server" class="chbSelectAll"/><label>SelectAll</label>
<asp:CheckBoxList
ID="chbListCountries"
CssClass="chbList"
RepeatColumns="4"
RepeatDirection="Horizontal"
DataTextField ="Name"
DataValueField = "CountryCode"
runat="server">
<asp:ListItem></asp:ListItem>
</asp:CheckBoxList>
</div>
</div>
</ItemTemplate>
<FooterTemplate>
</FooterTemplate>
</asp:Repeater>
I have a dialog containing a repeater of all the countries in the world grouped by continent. Each continent is separated by a jquery accordion. Inside each accordion there is a checkboxlist of countries, with a checkbox at the top that functions as a select all. I need to have the label associated with the select all, be responsive. By this i mean if i click the label i need to be able to trigger the checkbox to select all.
I considered
<label for="IDHERE">Select all</label>
I cannot however figure out how to correctly target the checkbox so that when i click the specific label it only selects the check boxes in this specific checkboxlist.
Is there anyway to create an ID for each "select all" checkbox that is unique from the other seven so that when the label is clicked on it correclty selects the checkboxes in its respective accordion.
for reference, when the checbox is clicked this javascript runs:
$(document).on('change', '.chbSelectAll', function (e) {
var selectAllValue = $(this).prop('checked')
var panel = $(this).closest('.panelCountry');
var checkboxes = panel.find('input[type=checkbox]:not(.chbSelectAll)');
checkboxes.prop('checked',selectAllValue);
});
Visual view of the problem
If you don't need the runat=server tag you can create your own label for= by using the ItemIndex.
<input type="checkbox" class="chbSelectAll" id="CheckBox_<%# Container.ItemIndex %>" />
<label for="CheckBox_<%# Container.ItemIndex %>">SelectAll</label>
But why not use an aspnet CheckBox? Then you won't have that problem anyway.
<asp:CheckBox ID="CheckBox1" runat="server" Text="SelectAll" CssClass="chbSelectAll" />

multiple radio buttons but only one won't fire onCheckedChange asp.net

I have a set of 3 radio buttons, but the first button doesn't fire the onCheckedChange event when you click on it. I put a breakpoint in my c# code at the SSTypeChanged function so I know it's not being called when you click on the Pullback button. Can anyone tell me what I'm missing? Thanks in advance.
<asp:RadioButton ID="rbn_SSPullback" runat="server"
Text="Pullback" CssClass="msRadioButton"
Font-Size="12" GroupName="MSType"
OnCheckedChange="SSTypeChanged" AutoPostBack="true" />
<br />
<asp:RadioButton ID="rbn_SSDeadlength" runat="server"
Text="Deadlength" CssClass="msRadioButton"
Font-Size="12" GroupName="MSType"
OnCheckedChanged="SSTypeChanged" AutoPostBack="true" />
<br />
<asp:RadioButton ID="rbn_SSModular" runat="server"
Text="Modular (Bar Feed Only)" CssClass="msRadioButton"
Font-Size="12" GroupName="MSType"
OnCheckedChanged="SSTypeChanged" AutoPostBack="true" />
OnCheckedChange="SS should be OnCheckedChanged="SS
You have a syntax error on the first one.

Radio Button Losing Focus After Postback

I am having an issue with posting back after selecting an option in a radio button.
After "yes" is selected, it must post back to load a text box beneath; however, if no is selected, the text box must not appear.
I had found a solution to other post back issues across my site at http://www.codeproject.com/Articles/17571/Maintain-focus-between-postbacks-in-ASP-NET-2-0-al (I added radio button the list of "CurrentControl is "'s), but for a reason beyond myself, it doesn't seem to apply to radio buttons.
As I have just created my account, I can't post pictures yet, but when the page posts back, the focus is shifted to the last text box or drop down list that has been focused.
Aspx for Radio Button:
<table class="dataentry">
<tr>
<td class="column1">
<asp:Label ID="lblPrevEmployed" runat="server" Text="Have you ever been employed by our company?"meta:resourcekey="lblPrevEmployed"></asp:Label>
</td>
<td class="column2">
<asp:RadioButton ID="rdoPrevEmployed_Yes" runat="server" GroupName="PrevEmployed" AutoPostBack="True" OnCheckedChanged="rdoPrevEmployed_OnCheckedChanged" Text="Yes" meta:resourcekey="rbPrevEmployedYes" />
<asp:RadioButton ID="rdoPrevEmployed_No" runat="server" GroupName="PrevEmployed" AutoPostBack="True" OnCheckedChanged="rdoPrevEmployed_OnCheckedChanged" Text="No" meta:resourcekey="rbPrevEmployedNo" />
<asp:CustomValidator ID="cvPrevEmployed" runat="server" ErrorMessage="You must select either Yes or No." ValidationGroup="Group" OnServerValidate="cvPrevEmployed_ServerValidate" Display="Dynamic" meta:resourcekey="cvPrevEmployed"></asp:CustomValidator>
</td>
</tr>
</table>
Try something like this:
(I've used a RadioButtonList instead of individual RadioButtons)
One div is for the radio button list and the second div is for displaying the description associated with the selected radio button.
Also a notification label will be enabled (which would be disabled by default) with a message.
[HTML]
<asp:Label ID="NotifyLabel" runat="server" Text="" ForeColor="Red" Font-Bold="true"></asp:Label>
<br />
<div id="SelAccTypeSelect" runat="server" style="float: left; margin-right: 20px;">
<asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatDirection="Horizontal" AutoPostBack="True" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged">
<asp:ListItem Value="0" Text="Account Type 1" />
<asp:ListItem Value="1" Text="Account Type 2" />
</asp:RadioButtonList>
</div>
<div id="SelAccTypeDesc" runat="server" style="width: 920px;"></div>
The is an array containing the available descriptions that correspond to the value of the radio buttons.
[CODE BEHIND]
private void InitializeSelAccTypeDesc()
{
SelAcctDescription[0] = "<p>This account type is for users who want to do something like this.</p>";
SelAcctDescription[1] = "<p>This account type is for other users who want to do something like that.</p>";
}
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
// get the selected account type
AccTypeByte = Convert.ToByte(RadioButtonList1.SelectedValue.ToString());
// notify the user with the value of the selected radio button
NotifyLabel.Visible = true;
NotifyLabel.Text = string.Format("{0} was the value selected. - ", AccTypeByte);
// replace the html in the div with the html from the selected array element
SelAccTypeDesc.InnerHtml = SelAcctDescription[Convert.ToInt32(AccTypeByte)];
}
*NOTE: 0 could be 'No' and 1 could be 'Yes'
You can add an if statement around the NotifyLabel, to only show based on the value of AccTypeByte.

very simple c# submit button

I have several ImageButtons on the top of my page, then I have a textbox and button in the middle. If you type in the textbox then hit enter on the keyboard the browser follows the link of the first ImageButton instead of the submit button next to the textbox. I have ran into this in the pase and had to put the imagebuttons on the bottom of the page for it to work correctly, that is not an ok fix in this case. I tried setting UseSubmitBehavior="true" , but that does nothing. I tried putting the textbox and button in a separate DIV and a separate panel, didn't work either
TOP of the page
<div style="position:absolute; left: 70% ; top: 5%;">
<asp:ImageButton ID="imgFB" runat="server" ImageUrl="Images/facebook_icon.jpg" PostBackUrl="http://www.facebook.com/832586561" />
<asp:ImageButton ID="imgLI" runat="server" ImageUrl="Images/linkedin_logo.jpg" PostBackUrl="http://www.linkedin.com/pub/scott-selby/33/304/44a" />
<asp:ImageButton ID="imgCB" runat="server" ImageUrl="Images/careerbuilder_logo.jpg" PostBackUrl="http://www.careerbuilder.com" />
<asp:ImageButton ID="imgCP" runat="server" ImageUrl="Images/codeplex_logo.jpg" PostBackUrl="http://www.codeplex.com" />
</div>
Middle of Page
<div ID="formPanel" runat="server" style="position:absolute; top:235px;">
<asp:TextBox ID="txtNewCity" runat="server"></asp:TextBox>
<asp:Button ID="btnChangeCity" runat="server" Text="Change City" UseSubmitBehavior="true" />
</div>
You may set the default button via <form/> attribute.
<form defaultbutton="btnChangeCity" id="form1" runat="server">
...
</form>
Or use Panel control to set default button.
<asp:Panel ID="Panel1" runat="server" DefaultButton="btnChangeCity">
<asp:TextBox ID="txtNewCity" runat="server"></asp:TextBox>
<asp:Button ID="btnChangeCity" runat="server" Text="Change City" />
</asp:Panel>
You surround your controls with <div> which is a good idea, but to have it run at all you also need <form>. In case you just didn't include it, the submit button also has to have OnClick="sub" attribute.
Read more here.

check Validation of a cotrol from code behind - causing to show Validator Text

hi my dear friends :
i have a button with CausesValidation Property to false...
it's code like below :
<telerik:RadButton ID="RadbtnViewImage" runat="server" Text="view" CausesValidation="False" EnableEmbeddedSkins="False" Skin="BlackByMe" Font-Names="Tahoma" Width="80px" onclick="RadbtnViewImage_Click">
</telerik:RadButton>
also i have a RadComboBox with a CostumValidator that is in relationship with that RadComboBox...
it's code like below :
<div class="EditRow">
<div class="RightEditColumn">
imageGroupName
</div>
<div class="LeftEditColumn">
<telerik:RadComboBox ID="RadcbImageGroupInrpvEdit" runat="server" DataSourceID="sdsImagesGroup"
DataTextField="Title" DataValueField="ID" EnableEmbeddedSkins="False" Skin="BlackByMe2"
AppendDataBoundItems="True" MarkFirstMatch="True" LoadingMessage="loading...."
CausesValidation="False" ValidationGroup="B">
<Items>
<telerik:RadComboBoxItem runat="server" Text="plz select one" Value="0" />
</Items>
</telerik:RadComboBox>
</div>
<div class="ValDivInrpvEdit">
<div style="display: inline;">
<span id="spnOfcvImageGroupInrpvEdit" class="ttTarget">
<asp:CustomValidator ID="cvImageGroupInrpvEdit" runat="server" ControlToValidate="RadcbImageGroupInrpvEdit"
Display="Dynamic" ValidationGroup="B"
onservervalidate="cvImageGroupInrpvEdit_ServerValidate">
<span class="imgValContainerInrpvEdit">
<asp:Image ID="img4cvImageGroupInrpvEdit" CssClass="imgValidateInrpvEdit" runat="server" AlternateText="attention"
ImageUrl="~/Images/Exclamation.png" /></span>
</asp:CustomValidator>
</span>
<div id="tt44cvImageGroupInrpvEdit" class="ttContent">
plz choose
</div>
</div>
</div>
</div>
when the button is clicked i just want to check only that RadCombobox Validation and show the CustomValidator Text of it to users / not the other validators !
how can i do that ?
(the other controls In my page plus that RadComboBox have a same ValidationGroup -> Mean "B")
thanks in advance
Attach a second validator for this scenario with its own distinct validation group. This way, you can fire this one validator for this specific scenario, and use the original validator to validate when the entire form is processed. Depending on how you need to validate, you can use Required (set InitialValue to the initial value of the combo), or Custom.
HTH.

Categories