Getting Value from HTML Radio Button - in Aspx-C#

Getting value from html radio button - in aspx-c#

place your code like this:

 if (Request.Form["Gender"] != null)
{
string selectedGender = Request.Form["Gender"].ToString();
}

Note that Request.Form["Gender"] will be null if none of the RadioButtons are selected.

see the markup below

<form id="form1" runat="server" method="post">
<input type="radio" name="Gender" value="male" id="test" checked="checked" />
male
<input type="radio" name="Gender" value="female" />female
<input type="submit" value="test" />
<asp:Button ID="btn" runat="server" Text="value" />
</form>

for both the buttons i.e input type="submit" and usual asp:button, Request.Form["Gender"] is going to have some value upon PostBack, provided, either of the RadioButtons is selected.

And yes, upon PostBack only, i.e. when you hit either of the buttons and not on first load.

Issue in getting value of input type = radio from asp.net childf page of a master page to c#

Since all of HTML radio buttons have runat="server" attribute which means they're accessible from code behind, put different control IDs and use if condition to check which radio button is selected.

Markup

<input id="RadioButton1" type="radio" runat="server" name="MouvementYesNo" value="Yes" checked> Yes
<input id="RadioButton2" type="radio" runat="server" name="MouvementYesNo" value="No"> No

Code behind

protected void Button1_Click(object sender, EventArgs e)
{
if (RadioButton1.Checked == true)
{
// do something
}
else if (RadioButton2.Checked == true)
{
// do something else
}

// other stuff
}

Note that if you want to use Request.Form, the HTML server control attribute (i.e. runat="server") should be removed, also Request.Form contains null value if no radio button input elements are selected.

Side note:

Better to use RadioButtonList server control like this:

<asp:RadioButtonList ID="MouvementYesNo" runat="server">
<asp:ListItem value="Yes">Yes</asp:ListItem>
<asp:ListItem value="No">No</asp:ListItem>
</asp:RadioButtonList>

And you can get currently selected radio button value later:

var selected = MouvementYesNo.SelectedValue;

Similar issue:

Getting value from html radio button - in aspx-c#

How to get value of Radio Buttons?

For Win Forms :

To get the value (assuming that you want the value, not the text) out of a radio button, you get the Checked property:

string value = "";
bool isChecked = radioButton1.Checked;
if(isChecked )
value=radioButton1.Text;
else
value=radioButton2.Text;

For Web Forms :

<asp:RadioButtonList ID="rdoPriceRange" runat="server" RepeatLayout="Flow">
<asp:ListItem Value="Male">Male</asp:ListItem>
<asp:ListItem Value="Female">Female</asp:ListItem>
</asp:RadioButtonList>

And CS-in some button click

string value=rdoPriceRange.SelectedItem.Value.ToString();

Get value from html label assiociated with radiobutton using FOR attribute

Try this Script:

    window.onload = function () {
var btnSubmit = document.getElementById('btn-submit');
btnSubmit.onclick = function (e) {
e.preventDefault();
var radioInputs = document.querySelectorAll('#myform input[type="radio"][name="sex"]');
for (var i = 0; i < radioInputs.length; i++) {
if (radioInputs[i].checked)
{
var value = getlabelforValue(radioInputs[i]);
alert(value);
}
}
document.getElementById("myform").submit();
};
};

function getlabelforValue(inputname) {
var labelElements = document.querySelectorAll('#myform label');
for (var i = 0; labelElements[i]; i++) {
console.log(labelElements[i]);
if (labelElements[i].getAttribute('for') == inputname.getAttribute('id')) {
return labelElements[i].innerText || labelElements[i].textContent;
}
}
return null;
}

Html:

<form id="myform" action="demo_form.aspx">
<label for="male">Male</label>
<input type="radio" name="sex" id="male" value="male" /><br />
<label for="female">Female</label>
<input type="radio" name="sex" id="female" value="female" /><br />
<input type="submit" id="btn-submit" value="Submit">
</form>

Here is the Demo

Or

In code behind you can do this to get value.

string radioValue = String.Format("{0}", Request.Form['sex']);

How to determine which radio button was selected in C# ASP.NET CORE MVC 5.0

Give the radio buttons a common name and assign the label text to the value of the radio buttons.

change Index.cshmtl to :

<form method="post" action="/DemSecondo/ValidateAddress">
<input type="radio" value="Valid" name="myRadio" /><label>Valid</label>
<input type="radio" value="Wrong" name="myRadio" /><label>Wrong</label>
<input type="radio" value="InValid" name="myRadio" /><label>InValid</label>
<input type="submit" value="Address Validation" />
</form>

and change ValidateAddress action to :

[HttpPost]
public IActionResult ValidateAddress(string myRadio)
{
switch (myRadio)
{
case "Valid":
var request = new AddressRequest
{
StatusCode = 1,
Address = "2018 Main St New York City, NY, 10001"
};
break;
case "Wrong":
var req = new AddressRequest
{
StatusCode = 2,
Address = "91 Apple City Galveston, TX, 77550"
};
break;
case "Invalid":
var addressRequest = new AddressRequest
{
StatusCode = 3,
Address = "00000 Tree Ln, Miami FL 91041"
};
break;
}

return RedirectToAction("SecIndex");
}

How to display results from selected radio buttons into one textbox using a for loop?

First: It's because you've not incremented the q value,

Incorrect because q is always 1:

txtResults.Text = "Course Results";
if (results[i] == 1)
{
txtResults.Text += "Q" + q + ": Strongly Disagree";
}

Second, you've allowed the program to write for cases where i=0 and i=11

Full answer:

public void DisplayResults(double[] results)
{
try
{
for (int i = 0, q = 1; i <= 19; i++, q++)
{
if (i == 0)
{
txtResults.Text += "Course Results";
}
if (i == 11)
{
txtResults.Text += "Professor Results";
}
txtResults.Text += "Q" + q ": ";
if (results[i] == 1)
{
txtResults.Text +="Strongly Disagree";
}
else if (results[i] == 2)
{
txtResults.Text +="Disagree";
}
else if (results[i] == 3)
{
txtResults.Text +=": Neutral";
}
else if (results[i] == 4)
{
txtResults.Text +="Agree";
}
else if (results[i] == 5)
{
if(i == 0 )
txtResults.Text +="Strongly Agree ZERO";
else if(i == 11 )
txtResults.Text +="Strongly Agree ELEVEN";
else
txtResults.Text +="Strongly Agree";
}
}
}
catch (Exception e)
{
Console.Write(e.Message, "Error");
}
}


Related Topics



Leave a reply



Submit