0

I am not able to find the right words to narrow this Google search, so here goes ...

Let's say inside an HTML Form there was the following question:

How Great is Stack Overflow?
<input type="radio" name="Q1" value="100.00">Excellent</input>
<input type="radio" name="Q1" value="75.00">Average</input>
<input type="radio" name="Q1" value="50.00">Poor</input>

If the User selected Excellent, Later in some type of Code Behind, I could do the following:

Request.Form["Q1"]

And it would give me the value 100.

Question 1: Can I get the actual text too? (Meaning, for this example, "Excellent")?

Question 2: How possible is it to get a list of all the text (Excellent, Average, Poor), the associated values (100, 75, 50), and which one was selected (Excellent - at a value of 100)?

Question 2a: And maybe even the actual question all that data is associated with (How Great Is Stack Overflow?)

JustLooking
  • 2,405
  • 4
  • 28
  • 38

3 Answers3

2

You can if you switch to a RadioButtonList.

<asp:RadioButtonList ID="Q1" runat="server">
    <asp:ListItem Text="Excellent" Value="100.00"></asp:ListItem>
    <asp:ListItem Text="Average" Value="75.00"></asp:ListItem>
    <asp:ListItem Text="Poor" Value="50.00"></asp:ListItem>
</asp:RadioButtonList>

And then in code behind

string question1 = Q1.SelectedValue + " (" + Q1.SelectedItem.Text + ")";

And if you want to get them all, you can loop the ListItems

foreach (ListItem item in Q1.Items)
{
    Response.Write(item.Value + " (" + item.Text + ")<br>");
}
VDWWD
  • 35,079
  • 22
  • 62
  • 79
1

No. The form will only post the input names and their values in key-value pairs, as in Q1=100.

If you do want to pass extra information in your form, you can put in hidden inputs. These are things the form will submit that the user can't see. It would be like this:

<input type="hidden" name="FirstFormText" value="Excellent" />
<input type="hidden" name="SecondFormText" value="Average" />

and so on. Then you could access them as:

Request.Form["FirstFormText"]

and so on.

jimboweb
  • 4,362
  • 3
  • 22
  • 45
  • Yeah, I figured as much (and I am familiar with hidden). Wanted to double-check. Though, I find that it doesn't come back as Q1="excellent", it comes back as Q1=100.00. Is there really something that gives me the actual text? Or just the value, as I see it when I'm debugging in Visual Studio? – JustLooking May 15 '17 at 19:34
  • 1
    Sorry, I didn't pay enough attention to what you wrote. Yes, that's correct, it will only post the value, not the text – jimboweb May 15 '17 at 19:48
  • 1
    I have updated my answer to better reflect your question. Also, if you feel I answered your question, could you please check my answer? Thanks. – jimboweb May 15 '17 at 19:50
  • I'll hook you up. You answered first, and correctly. I wish I could give more than one check-mark, as I like the examples in the others. All are worthy. – JustLooking May 17 '17 at 00:22
1

HTML is separated into static content and inputs. Only the inputs are presented to the server. So normally you can't get any of the static content on the server side. The idea here is that you want to keep the traffic at a minimum, and since the server rendered the page to begin with, it should already know what the static content is. But in some cases this doesn't work out, e.g. if the "static" content is generated via script running in the browser. I'll assume this is the case for your question.

To solve your problem, you need to copy the static text into hidden form variables, where they will be submitted along with all the other form variables. Below is an bit of jquery which iterates through all of the labels on the page and creates hidden inputs for them, which will be submitted to the server.

$(function() { 
    $('#Q1-Form').on('submit', function(e) {
        $("label").each(function() {
            $this = $(this);
            $('<input>').attr({
                                 type: 'hidden',
                                 name: $this.attr("for")
                             })
                        .val($this.text())
                        .appendTo("#Q1-Form");
        });
    });
});
<style> li { list-style: none; } </style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<label for="Q1-Form">Stackoverflow rules!</label>
<form id="Q1-Form">
<UL>
    <LI>
        <input id="Q1-100" type="radio" name="Q1" value="100.00">
        <label for="Q1-100">Excellent</label>
    </LI>
    <LI>
        <input id="Q1-75" type="radio" name="Q1" value="75.00">
        <label for="Q1-75">Average</label>
    </LI>
    <LI>
        <input id="Q1-50" type="radio" name="Q1" value="50.00">
        <label for="Q1-50">Poor</label>
    </LI>
</UL>
<input type="Submit">
</form>

Now when you submit the form, you'll see a whole bunch of additional tags added to the querystring (or to the form variables), one for each label. Hard to tell with the Stackoverflow code runner, but the URL will end up looking like this:

https://www.example.com/MyPage?Q1=100.00&Q1-Form=Stackoverflow+rules%21&Q1-100=Excellent&Q1-75=Average&Q1-50=Poor

(I recommend using method="POST" with this sort of content).

Note: A separate </input> close tag is not valid HTML5 (input is a self-closing tag). Instead you should provide the content in a separate label and link them with the for attribute, as shown in my example.

Community
  • 1
  • 1
John Wu
  • 50,556
  • 8
  • 44
  • 80