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.
Related
I have an ASP.NET website and I have a forgot password page where the user enters their email address in a text box and when they click the button to retrieve password, the event runs as fine.
Only problem is if the user types in their email address and presses ENTER instead, it runs a search (I have a search bar at the top of the page) and so the result comes back as 'search query not found'. But this search bar at the top is on a different ASP.NET page.
So anyway, I want the event onclick to run when the user presses enter and not run a search query. Does anyone have any ideas? I've searched on this site but not really found the answer I need.
There are couple ways you can do this. If it is a webform and you have your textbox wrapped with an asp:panel you can do as below:
<asp:Panel ID="p" runat="server" DefaultButton="myButton">
<%-- Text boxes here --%>
<asp:Button ID="myButton" runat="server" />
</asp:Panel>
If its not a webform or you want to move away from that try below:
<asp:TextBox ID="TextBox1" runat="server" onkeypress="return EnterEvent(event)"></asp:TextBox>
<asp:Button ID="Button1" runat="server" style="display:none" Text="Button" />
function EnterEvent(e) {
if (e.keyCode == 13) {
__doPostBack('<%=Button1.UniqueId%>', "");
}
}
And in the codebehind fire your button click in the page load based on the parameters of the postback.
Specify the "defaultbutton" property to the ID of (event you want to fire).
You can specify the "defaultbutton" property at the Form level (in the form tag)
Or else you can define them at panel level in the tag.
The form level setting is overridden at the panel level setting.
The Event Handler for the specified button gets fired simulating a true submit button functionality.
<form id="sampleform" runat="server" defaultbutton="button1">
<div>
<asp:TextBox ID="textBox1" runat="server"></asp:TextBox>
<asp:Button ID="button2" runat="server" Text="Cancel" OnClick="Button2_Click" />
<asp:Button ID="button1" runat="server" Text="Ok" OnClick="button1_Click" />
<asp:Panel ID="panel1" runat="server" defaultbutton="Button5">
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
<asp:Button ID="Button5" runat="server" Text="Button5" OnClick="Button5_Click" />
</asp:Panel>
</div>
</form>
In this example, Button1 is the default button for the form (Type something in Textbox 1 and hit enter, button1_Click gets fired). But for "Panel 1", default button will be Button 5 (Type in Textbox3 or Textbox 5 and hit enter).
You can have any number of panels with different default button for each panel.
Adding an ASP Panel around the text box and button with the id of the button name as the property for DefaultButton was the best way to do this.
<asp:Panel ID="p" runat="server" DefaultButton="myButton">
<%-- Text boxes here --%>
<asp:Button ID="myButton" runat="server" />
</asp:Panel>
We using Ajax Accordion pane. We dynamically binding ajax accordion from data set. So we not sure how much pane wil be bind.
Now we added few textbox and 1 button in each accordion pane which is mandotory so we used required field validator.
The problem is when i click buton in first pane textbox in first pane showing "It is required" which is fine but even if i typed textbox in first pane. I am not able to save since textbox in remaining pane showing "It is required". Please guide me.
<cc1:Accordion ID="accRcvDetails" runat="server" SelectedIndex="-1" ContentCssClass="accordion-content" RequireOpenedPane="false" HeaderCssClass="accordion-header"
HeaderSelectedCssClass="accordionSelected" OnItemCommand="accRcvDetails_ItemCommand" OnItemDataBound="accRcvDetails_ItemDataBound">
<HeaderTemplate>
</HeaderTemplate>
<ContentTemplate>
<td class="searchlabel">
<asp:Label ID="lblNotes" runat="server" Text="Notes:"></asp:Label>
</td>
<td class="searchtext" colspan="3">
<asp:TextBox runat="server" ID="txtNotes" Width="100%" TextMode="MultiLine" Height="35px" />
<asp:RequiredFieldValidator ControlToValidate="txtNotes" Text="Note Field is Required" runat="server" />
</td>
<td>
<td style="text-align: right" colspan="8">
<asp:Button ID="btnSave" CssClass="searchbutton Accbtnsave" runat="server" CommandName="Save" Text="Save" />
</td>
</ContentTemplate>
</cc1:Accordion>
You need to mention ValidationGroup for every textbox and button, as you are binding the accordion at runtime, so need to bind ValidationGroup at runtime with some property that has unique/different value for every accordian. Lets say you are binding accordion with rows of Groups, you can mention groupId in ValidationGroup then. Your updated code must looks alike:
<asp:RequiredFieldValidator ControlToValidate="txtNotes" Text="Note Field is Required" runat="server" ValidationGroup='<%# Eval("GroupId") %>'/>
<asp:Button ID="btnSave" CssClass="searchbutton Accbtnsave" runat="server" CommandName="Save" Text="Save" ValidationGroup='<%# Eval("GroupId") %>'/>
This way each accordion has its own ValidationGroup and clicking on button will not fire the validators in other accordions
On my admin pages I've got a "Log out" button. On one page there's this form you can fill in and submit and it has some validator controls. The problem is that when I am on this page, I can't log out, because it wants the textboxes to be filled in, even though it's - obviously - not submitted via the log out button. Are the validators executing everytime you try to leave this page, even though I'm not trying to submit a form? To be clear: it works for every other page, it's just the validation here that stops it.
Form code:
<p>
<asp:Label ID="lblA" runat="server" Text="LabelA"></asp:Label><br />
<asp:TextBox ID="txtA" runat="server"></asp:TextBox>
*
<asp:RequiredFieldValidator
ID="rfvA"
runat="server"
ControlToValidate="txtA"
ErrorMessage="Required"
Display="Dynamic">
</asp:RequiredFieldValidator>
</p>
<p>
<asp:Label ID="lblB" runat="server" Text="LabelB"></asp:Label><br />
<asp:TextBox ID="txtB" runat="server"></asp:TextBox>
*
<asp:RequiredFieldValidator
ID="rfvB"
runat="server"
ControlToValidate="txtB"
ErrorMessage="Required"
Display="Dynamic">
</asp:RequiredFieldValidator>
</p>
Logout button:
<asp:Button runat="server" Text="Log out" ID="btnLogout" OnClick="btnLogout_Click"/>
When Log out is clicked:
protected void btnLogout_Click(object sender, EventArgs e)
{
FormsAuthentication.SignOut();
FormsAuthentication.RedirectToLoginPage();
}
Any ideas?
add cause validation false to your button
<asp:Button runat="server" CausesValidation="False" Text="Log out" ID="btnLogout" OnClick="btnLogout_Click"/>
Also you can use the ValidationGroup property of the controls.
Assign the same ValidationGroup to the group of controls(and RequiredFieldValidator) which you require for validation like username and password text boxes and login button(In your case to the two text boxes) and assign the different ValidationGroup to the other controls(In your case to logout button).
So the validation fires only when the same validation group's button has been clicked.
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.
I'm making dynamic radio buttons by using ASP.NET. Where there a validation control that I want to align side of this radios buttons.
Code:
<asp:RadioButtonList
ID="tiposeg"
runat="server"
RepeatColumns="3">
<asp:ListItem>a</asp:ListItem>
<asp:ListItem>b/asp:ListItem>
</asp:RadioButtonList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator15"
ControlToValidate="tiposeg"
ErrorMessage='<% # foo.ErrorMessages.EmptyField %>'
runat="server"
/>
The problem is: if making by using <asp:RadioButtonList, the <asp:RequiredFieldValidator is displayed into next line, unlike if I do it by using "HTML pure", that is displayed side of control (without need to use CSS). I hope this is clear. Thanks in adavnce.
EDIT
in other words, it's possible put the <span> generated by asp:RequiredFieldValidator inside <table> generated by <asp:RadioButtonList?
EDIT 2
My current ASP.NET code;
<asp:RadioButtonList
ID="RadioButtonList1"
runat="server"
RepeatColumns="3">
<asp:ListItem>abcd</asp:ListItem>
<asp:ListItem>xyz</asp:ListItem>
</asp:RadioButtonList>
</li>
<li style="display:inline">
<asp:RequiredFieldValidator
ID="ReqiredFieldValidator1"
runat="server"
ControlToValidate="RadioButtonList1"
ErrorMessage="select atleast one radiobutton!"
/>
The form:
What I'm getting:
What I'm expecting:
Do you really want to arrange the radio buttons in a grid with multiple rows and columns? If not, delete the RepeatColumns attribute and add RepeatLayout="Flow" and RepeatDirection="Horizontal":
<asp:RadioButtonList runat="server" ID="RadioButtonList1"
RepeatLayout="Flow" RepeatDirection="Horizontal">
This will render the radio buttons without a containing <table>, so the validator will appear to the right of the last radio button.
Could you achieve what you want by putting the controls in DIVs and floating them both left?