How to default radio button to checked depending on condition - webforms

I have a checkout page with a collection of payment methods displayed in a radio button list;
<dx:ASPxRadioButtonList runat="server" ID="rblPaymentMethod" ClientInstanceName="rblPaymentMethod" Border-BorderStyle="None">
<ClientSideEvents SelectedIndexChanged="function(s,e) { ShowPaymentDetails(rblPaymentMethod.GetValue()); }" />
<Items>
<dx:ListEditItem Text="Credit Account" Value="AccountPayment" />
<dx:ListEditItem Text="Credit / Debit Card" Value="CardPayment" />
<dx:ListEditItem Text="MEARS Account" Value="MearsPayment" />
<dx:ListEditItem Text="PayPal" Value="PayPalPayment" />
</Items>
<ValidationSettings ValidationGroup="vgPaymentDetails">
<RequiredField IsRequired="True" ErrorText=" You must select a payment method" />
</ValidationSettings>
</dx:ASPxRadioButtonList>
If you are a 'Mears' customer, only the 'MEARS Account' option will be shown. This is done with the below; The property 'IsMearsAccount' is controlled by a bit field in my DB.
Private Sub ShowCorrectPaymentMethods(objCustomer As Customer)
'Mears payment only available to Mears account
If objCustomer.IsMearsAccount Then
RemoveAllPaymentMethods()
Else
RemovePaymentMethod("MearsPayment")
End If
What I want to do is default the radio button list for the 'MEARS Account' option to be checked when you navigate to the tab. This is what it currently looks like when you navigate to the tab;
https://i.stack.imgur.com/GpGF2.png
I originally thought this could be done in a javascript function however the js function doesn't take into account the condition of whether this customer is 'MEARS' or not, it just defaults to this every time no matter what type of customer that is used.

Related

Using radiobuttons in mvc webgrid?

I have a WebGrid on my form, inside that I have a column called 'duration'. In this column I'm displaying 2 radio buttons, so the Admin should select one validity period for that user on every row, either 30 days or 90 days and submit. And here I want to get the selected radio button value submitted value and the Id of that row so that I can perform the database activities with that values.
grid.Column("Duration", format: #<text><div style="width: 30em"><table><tr><td><input type="radio" name="group1" value="30"> 30 Days<br>
</td><td><input type="radio" name="group1" value="90"> 90 days<br>
</td></tr></table></div></text>)))
Here the problem is when I'm giving the same group name the radio buttons we can only select one radio button in the entire grid, but how I want is that he should select one radio button on each row. Also how can retrieving be done?
A separate name should be used for each 'group' of radio buttons. Out of all radio buttons that have the same name, only one can be selected.
With MVC, you can use model binding; when your form posts back, the model is built and the properties are assigned based on the name or id values.
To use this, you need to create inputs using methods such as:
#Html.RadioButtonFor(Function(m) m.MyProperty) #* ... *#
******* edit -- clearer explanation *****************
Let's use two 'groups.' A user can select a first name and an occupation. To limit the number of selections within a group, each control group needs its own value for the radio button's 'name' property.
#Html.RadioButtonFor(Function(m) m.NameSelected, "Jane", New With { .name = "UserName" }) Jane <br />
#Html.RadioButtonFor(Function(m) m.NameSelected, "Jack", New With { .name = "UserName" }) Jack <br />
#Html.RadioButtonFor(Function(m) m.Occupation, "Lumberjack", New With { .name = "Occupation" }) Lumberjack <br />
#Html.RadioButtonFor(Function(m) m.Occupation, "Waiter/Waitress", New With { .name = "Occupation" }) Waiter/Waitress
The radio buttons for the names will now be selectable without affecting the radio button selections in the occupation group, and vice versa.

Conditionally show dialog in action method

Well, I have a form with a lot of buttons, when user click on a button some method must be executed in ManagedBean, a "crazy" logic. So I have 2 solutions for that and i would like to know the suggetions of you:
First Solution
<p:commandButton actionListener="#{myBean.someMethod}" value="Execute method 1" process="#this" />
public void someMethod(ActionEvent event){
if (selectedCustomer){
myCrazyLogicHere();
RequestContext.getCurrentInstance().update("formAlternative:componentToUpdate");
RequestContext.getCurrentInstance().execute("someAlternativeDialog.show()");
}else{
addErrorMessage("Select a Customer before click on Method 1");
}
}
Second Solution
<p:commandButton actionListener="#{myBean.someMethod}" update=":formAlternative:componentToUpdate" oncomplete="someAlternativeDialog.show()" value="Execute Method 1" />
public void someMethod(ActionEvent event){
if (selectedCustomer){
myCrazyLogicHere();
}else{
addErrorMessage("Select a Customer before click on Method 1");
}
}
So, I have a problem in "Second Solution". When commandButton complete your cycle, it will execute "someAlternativeDialog.show()", I need put a condition to that, just show dialog if "selectedCustomer" is true. In "First Solution" this problem is already solved, because i'm doing all in ManagedBean, the JSF just call the method.
So, my doubt is : What is the best form to work ? If form 2 is better, how can i condition the dialog show ?
You shouldn't be performing validation in action method in first place. You should be performing validation using a true validator. E.g., using required="true"
<h:selectOneMenu value="#{myBean.selectedCustomer}" required="true"
requiredMessage="Select a Customer before click on Method 1">
...
</h:selectOneMenu>
When validation using a true validator fails, then JSF already won't invoke the command button's action. This way you can get rid of the whole if-else block and addErrorMessage() in action method. If validation fails, PrimeFaces will set an indicator args object with a validationFailed property which you can use in oncomplete:
<p:commandButton ... oncomplete="if (!args.validationFailed) someAlternativeDialog.show()" />

Grabbing values from Ajax AutoCompleteExtender with "Tab" or "Enter"

I have a setup where a user can enter a zip code into an ASP.NET TextBox control, and I have an AutoCompleteExtender from the Ajax Control Toolkit attached to that textbox. It gets its data from a static page method on the ASPX page.
When the user starts typing a Swiss zip code, e.g. 3 and then waits a brief moment, a list of matching zip code shows up - something like:
3000 - Bern
3001 - Bern
and so on. Works like a charm.
The normal way to pick one of the options shown is to move your mouse pointer to the list and select the one you want, click on it or press Enter, and get the zip code into that textbox (and the city name into a second textbox next to it).
Now, I got some additional requirements from my project manager:
we would like to be able to just press Enter without going into the list of choices to select one - he'd like to just get the first (or often times: only) entry shown put into those two textboxes...
we would like to be able to enter a valid 4-digit zip code and then just press Tab and move out of the textbox for the zipcode, and have the first (possibly only) entry with that zipcode be chosen and "selected" (and stuffed into the two textboxes).
Seems like a tall order to me (I'm not a great Javascript guru at all.....) - any ideas?
This is my ASP.NET page (in a standard ASP.NET 4.0 webforms sample app - with a master page; the script is simplified; in reality, I'm splitting up the text 3001 - Bern at the dash and stick the first part into the zip code and the second part into the city textbox):
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<script type="text/javascript">
function IAmSelected(source, eventArgs) {
$get('tbxCity').value = eventArgs.get_value();
}
</script>
<asp:ScriptManager runat="server" EnablePageMethods="True" />
<asp:Literal runat="server" ID="litPrompt" Text="Please enter zip code: " />
<asp:TextBox runat="server" ID="tbxZipcode" MaxLength="10" />
<act:AutoCompleteExtender runat="server" ID="acZipCode" TargetControlID="tbxZipcode" MinimumPrefixLength="1"
CompletionInterval="25" ServiceMethod="GetMatchingZipCodes" CompletionSetCount="15"
OnClientItemSelected="IAmSelected" />
<asp:TextBox runat="server" ID="tbxCity" MaxLength="50" />
</asp:Content>
and my code-behind (this, too, is simplified - of course, in reality, I get this data from a Entity Framework data model):
[WebMethod]
[ScriptMethod]
public static string[] GetMatchingZipCodes(string prefixText, int count)
{
return new string[] { "3000 - Bern", "3001 - Bern", "4000 - Basel", "6000 - Lucerne", "6001 - Lucerne" };
}
Check the FirstRowSelected property of AutoCompleteExtender. From your requirements seems like it's exactly what you need.

How to preselect struts html:radio button

How to preselect struts 1 html:radio button?
Thanks
Maria
You should have a value in your Form object that holds the data for your radio button. You'll need to pre-fill that in your Form before forwarding to the JSP (or default it in the Form's constructor if it will always default to that value).
Action.java
form.setRadioValue("123");
View.jsp
<html:radio property="radioValue" value="123" /> // <-- this radio will be checked
<html:radio property="radioValue" value="456" /> // <-- this radio will not be checked

Setting SqlDataSource SelectParameters from form text box

I am trying to use a text box input as a SqlParameter but it only goes into DataSelecting when the page first loads. Not after the from is submitted.
Here is the code on the aspx page.
protected void DataSelecting(object sender, SqlDataSourceSelectingEventArgs e)
{
e.Command.Parameters["#zip"].Value = ZipBox.Text;
}
"
SelectCommand="SELECT Name FROM Names WHERE (ZipCode = #zip)"
OnSelecting="DataSelecting">
SelectParameters>
parameter Name="zip" DefaultValue="1" />
SelectParameters>
SqlDataSource>
FORM
id="ZipSearch" runat="server" action="Default.aspx" method="post">
TextBox ID="ZipBox" runat="server" />
Button id="btnSubmit" Text="Submit" runat="server" />
FORM
Thanks for your help,
Matt
You need to place that code in the button click event. Selecting event is for different purpose.
Old reply (Before OP's comment) :
What do you have in button click event?
The Selecting event would fire before your select command is executed. Hence if your button click event is firing any command, the Selecting event won't be fired.

Resources