Building a query string based radiobutton values - asp.net

I'd like to build a query string based on values taken from 5 groups of radio buttons.
Selecting any of the groups is optional so you could pick set A or B or both. How would I build the querystring based on this? I'm using VB.NET 1.1
The asp:Radiobuttonlist control does not like null values so I'm resorting to normal html radio buttons. My question is how do I string up the selected values into a querystring
I have something like this right now:
HTML:
<input type="radio" name="apBoat" id="Apb1" value="1" /> detail1
<input type="radio" name="apBoat" id="Apb2" value="2" /> detail2
<input type="radio" name="cBoat" id="Cb1" value="1" /> detail1
<input type="radio" name="cBoat" id="Cb2" value="2" /> detail2
VB.NET
Public Sub btnSubmit_click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim queryString As String = "nextpage.aspx?"
Dim aBoat, bBoat, cBoat bas String
aBoat = "apb=" & Request("aBoat")
bBoat = "bBoat=" & Request("bBoat")
cBoat = "cBoat=" & Request("cBoat ")
queryString += aBoat & bBoat & cBoat
Response.Redirect(queryString)
End Sub
Is this the best way to build the query string or should I take a different approach altogether? Appreciate all the help I can get. Thanks much.

The easiest way would be to use a non-server-side <form> tag with the method="get" then when the form was submitted you would automatically get the querystring you are after (and don't forget to add <label> tags and associate them with your radio buttons):
<form action="..." method="get">
<input type="radio" name="apBoat" id="Apb1" value="1" /> <label for="Apb1">detail1</label>
<input type="radio" name="apBoat" id="Apb2" value="2" /> <label for="Apb2">detail2</label>
<input type="radio" name="cBoat" id="Cb1" value="1" /> <label for="Cb1">detail1</label>
<input type="radio" name="cBoat" id="Cb2" value="2" /> <label for="Cb2">detail2</label>
</form>

You could use StringBuilder instead of creating those three different strings. You can help it out by preallocating about how much memory you need to store your string. You could also use String.Format instead.
If this is all your submit button is doing why make it a .Net page at all and instead just have a GET form go to nextpage.aspx for processing?

Related

How to get which radio button is selected in symfony 3?

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];

How can I add additional fields to a form and still use MVC?

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.

HTML Radiobuton values in ASP.NET

I'm having some real problems with a group of HTML Radiobuttons when I try to pass the selected values back and forth between JavaScript and ASP.NET.
<input type="radio" id="radio1" name="markerSel" value="1" checked=true />
<input type="radio" id="radio2" name="markerSel" value="2" />
<input type="radio" id="radio3" name="markerSel" value="3" />
To begin with, the form loads with these radiobuttons and a user selects a value and submits the ASP.NET form. To get the value of the selected option on the server-side I have tried;
string selVal = Request.Form["markerSel"];
//always returns "1", regardless of the selection made.
And also tried ASP.NET's FindControl method (recursively too), and I just get null.
I want to be able to set the value of this radiobutton group via JavaScript and Asp.NET and be able to read from both environments.
Any help would be appreciated.
Thanks
Is there a reason why you don't use ASP.NET radio buttons instead of <input type=radio />?
If you use
<asp:RadioButton id="radio1" runat="server" Checked="true" />
<asp:RadioButton id="radio2" runat="server" />
<asp:RadioButton id="radio3" runat="server" />
You will be able to access them server-side by doing this.radio1.Checked
If you want to access your elements via javascript, you can do a document.getElementById('radio1') and you will get the DOM element that way.
If you want to use jQuery, you can access the radio buttons with
$('#radio1')
If you want to test if the radio button is checked, you can use
if ($("#radio1").is(':checked')) {//...
hmmm... I would try changing the line to:
string selVal = Request.Form("markerSel");

Variable Number of Input Fields in Classic-ASP form

I have a check out form where number of products can be "n". So how i can know how many input fields are in the form and take input from it?
Thanks
If it's a group of single controls - say a variable number of checkboxes representing items - the solution is pretty straightforward. For your checkboxes:
<input type="checkbox" name="ProductID" value="1" />Product #1<br />
<input type="checkbox" name="ProductID" value="2" />Product #2<br />
<input type="checkbox" name="ProductID" value="3" />Product #3
Then in your ASP, you could do this:
<%
Dim intID
For Each intID In Request.Form("ProductID")
' intID now represents a selected product ID. Insert into DB
' or whatever your process is. Note that only the "checked" values
' will be passed to the server.
Next
%>
In fact, this approach will work for any number of controls with the same name. If it were 1 - n number of textboxes with the name "FavoriteColor", you could For Each through each value in the same manner. Textboxes with no user input would not be passed.
Now, if your checkout form contains a group of input controls per item, you can build on that approach by carefully naming your other controls:
<div>
<input type="checkbox" name="ProductID" value="1" />Product #1<br />
<input type="textbox" name="Product1_Quantity">
<input type="textbox" name="Product1_Color">
</div>
<div>
<input type="checkbox" name="ProductID" value="2" />Product #2<br />
<input type="textbox" name="Product2_Quantity">
<input type="textbox" name="Product2_Color">
</div>
<div>
<input type="checkbox" name="ProductID" value="3" />Product #3
<input type="textbox" name="Product3_Quantity">
<input type="textbox" name="Product3_Color">
</div>
Now, again, on the server you could parse the data in this way:
<%
Dim intID
Dim intQuantity
Dim strColor
For Each intID In Request.Form("ProductID")
' this is a selected item
intQuantity = Request.Form("Product" & intID & "_Quantity")
strColor = Request.Form("Product" & intID & "_Color")
Next
%>
You'd be able to perform validation and other logic on each group of selected items in this manner.

Classic ASP checkbox question

I understand that if the all the inputs being entered as a, b, and c and all the checkbox are checked then the output would look like this.
response.write( request.form("a1") ) = a, b, c
response.write( request.form("chk") ) = 1, 1, 1
Is there a way to determined if the corresponding input text checkbox is checked if not all checkbox are checked?
ie: the input is being entered as a, b, and c then only the corresponding checkbox at "c" is checked.
The output of this will be:
response.write( request.form("a1") ) = a, b, c
response.write( request.form("chk") ) = 1
<form name="myForm">
<input type="text" name="a1" />
<input type="checkbox" name="chk" value="1" />
<input type="text" name="a1" />
<input type="checkbox" name="chk" value="1" />
<input type="text" name="a1" />
<input type="checkbox" name="chk" value="1" />
<input type"submit" value="submit" />
</form>
You're going to need to change the names of your inputs. The only type of input that is intended to have multiple instances sharing a name is a radio button. That is done so you can get the mutually-exclusive select behavior that radio buttons are designed for.
In this case, you're going to want to give each text and checkbox input a different name. So your HTML would look like:
<form name="myForm">
<input type="text" name="a1" />
<input type="checkbox" name="chk1" value="1" />
<input type="text" name="a2" />
<input type="checkbox" name="chk2" value="1" />
<input type="text" name="a3" />
<input type="checkbox" name="chk3" value="1" />
<input type"submit" value="submit" />
</form>
Then simply reference your third checkbox with:
response.write( request.form("chk3") )
It would require you to write a little code if you wanted the results to appear in a nice comma delimited list like you've shown, but I would argue that's appropriate.
If reason you want a comma-delimited string similar to "a, b, c" you could change the values of all your checkboxes to "1", "2", "3" instead of all being "1".

Resources