i need to restrict users selecting check boxes multiple times, only one check box should be seleted in my asp.net form ,
i am not using a checkbox list
Please help
Use something like this:
<input type="radio" name="foo" value="bar1" id="bar1"> <label for="bar1">Bar One</label>
<input type="radio" name="foo" value="bar2" id="bar2"> <label for="bar2">Bar Two</label>
<input type="radio" name="foo" value="bar3" id="bar3"> <label for="bar3">Bar Three</label>
Use Javascript. Call a method whenever a CheckBox is clicked that takes the ID of that CheckBox as an argument and then unchecks all other Checkboxes in the given context.
Related
I'm using bootstrap-toggle in one of the forms in ASP.NET:
<input type="checkbox" id="IsValidated" data-toggle="toggle" data-on="Validated" data-off="Not Validated" data-onstyle="success" data-offstyle="danger" value="Y" >
But when switching on the toggle, and check the state of the checkbox in the code behind after posting the form, always I find that the checkbox is not checked!
How to capture the state of the checkbox?
Add name attribute to your checkbox element
<input type="checkbox" name="IsValidated" id="IsValidated" data-toggle="toggle" data-on="Validated" data-off="Not Validated" data-onstyle="success" data-offstyle="danger" value="Y" >
Access the checkbox value on server side using
Request.Form["IsValidated"]
I need to remove this span tag its come automatically when i click on radio button
I think you used class="with-gap" in <input> like this :
<input class="with-gap" name="group3" type="radio" checked />
Remove it like this :
<input name="group3" type="radio" checked />
And your problem will be solve. Read here for more info.
I have these inputs in my twig file :
<input type="text" name="txtNom" id="txtNom" value="{{user.nom}}" />
<input type="text" name="txtPrenom" id="txtPrenom" value="{{user.prenom}}" />
<input type="radio" name="rbSexe" id="rbHomme" onclick="changeGender(this.id);" />
<input type="radio" name="rbSexe" id="rbFemme" onclick="changeGender(this.id);" />
So, for calling those inputs in my Controller, I use the name attribute, for the first two it's okay :
$utilisateur->setNom($request->get('txtNom'));
$utilisateur->setPrenom($request->get('txtPrenom'));
but those with radio type have the same name, so how can I call specific one of them?
$utilisateur->setSexe(?????????);
I solved the problem :
I give the inputs a value, and make the name looks like an array:
<input type="radio" name="rbSexe[]" value="Homme" id="rbHomme" onclick="changeGender(this.id);" />
<input type="radio" name="rbSexe[]" value="Femme" id="rbFemme" onclick="changeGender(this.id);" />
and for call it in Controller, I use this :
$s = $request->get('rbSexe',0)[0];
I would like to display a form that lets a user enter up to 10 rows of information. If they need to go over, I'm going to use an "add additional row" button that will add one row at a time. What will my Model class look like for something like this? When I use javascript to add a new row, how can I tie that new row into the Model as well?
This article from Phil Haack shows you how to bind to collections. You'll need to use the javascript to create the new row with the correct names.
Probably this rows contains related values, so you can give the same name to all theses inputs in the html, and declare that you action receive an array of values.
Assume that you have this
<form method="post" action="/Controller/YourAction">
<input type="text" name="row" value="1" />
<input type="text" name="row" value="2" />
<input type="text" name="row" value="3" />
<input type="text" name="row" value="4" />
<input type="text" name="row" value="5" />
<input type="text" name="row" value="6" />
<input type="submit" />
</form>
all that you need to do is declare this inside your Controller
public ActionResult YourAction(int[] row)
{
//put your code here
}
and you will have all the values inside the row array
You may take a look at the following blog post which explains how to achieve exactly that. It uses a custom helper (Html.BeginCollectionItem) which allows to use non sequential as collection indexes instead of numbers which makes adding/deleting new items much easier.
I have two radio buttons:
<input type="radio" name="group1" />1
<input type="radio" name="group1" />2
How do I know which one is selected when the form is posted?
The inputs should have values:
<input type="radio" name="group1" value="1" />1
<input type="radio" name="group1" value="2" />2
Then, the value will be posted on the name group1. On Asp.net you can get it using:
Request.Form["group1"]
If you are using runat="server", it can happen that name attribute is changed at client, so you will get null as value.