Saving radiobutton value to Database - c#

Im trying to take some selected value to use it in an insert method I have to the database (this is working with given vaalues), now I want to give to take the value from a form. Everything's fine but the radio buttons... I do not know how to take the value :P.
Here it is:
<tr>
<td>Oral/Written Communications:</td>
<td>
<asp:RadioButton ID="form1_1" GroupName="form1" runat="server" Text="1" />
<asp:RadioButton ID="form1_2" GroupName="form1" runat="server" Text="2" />
<asp:RadioButton ID="form1_3" GroupName="form1" runat="server" Text="3" />
<asp:RadioButton ID="form1_4" GroupName="form1" runat="server" Text="4" />
<asp:RadioButton ID="form1_5" GroupName="form1" runat="server" Text="5" />
<br /></td>
</tr>
And in the backend...
int selected = form1.value
This is not working... Not even recognizing the form1. etc.... How can I handle this?
EB.

When you work with a RadioButton control, each one of them is a separate control and you need to check them like this:
string selectedValue;
if (form1_1.Checked)
selectedValue = form1_1.Text;
else if (form1_2.Checked)
selectedValue = form1_2.Text;
...
You can also go after the Request.Form collection, but I don't find it to be easy to use with RadioButton controls.
Maybe the easiest thing, if you can change the code slightly, is to use a RadioButtonList control:
<asp:RadioButtonList ID="form1list" runat="server" RepeatDirection="Horizontal" RepeatLayout="Flow">
<asp:ListItem Text="1" />
<asp:ListItem Text="2" />
<asp:ListItem Text="3" />
<asp:ListItem Text="4" />
<asp:ListItem Text="5" />
</asp:RadioButtonList>
Then you can simply refer to the control as a whole:
form1list.SelectedValue

Related

Prevent submission unless all fields are filled asp.net and c# (Multiple choice quiz)

I am trying to create a multiple choice quiz whereby users will answer the questions and will be presented with a score upon submission, I am using required field validators to present a message if they do not answer one of the questions, however the system will still calculate their score even if I have not completed all the questions when hitting submit.
I need to be able to stop submission until all radio button lists are completed but i'm not sure how to do this.
My code is here:
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<asp:Label ID="QLabel1" runat="server" Text="Question 1"></asp:Label>
<asp:RadioButtonList ID="RadioButtonList1" runat="server"
RepeatDirection="Horizontal" RepeatLayout="Table">
<asp:ListItem Text="*Answer 1*" Value="Incorrect 1" />
<asp:ListItem Text="*Answer 2*" Value="Correct" />
<asp:ListItem Text="*Answer 3*" Value="I" />
<asp:ListItem Text="*Answer 4*" Value="4" />
</asp:RadioButtonList>
<div>
<asp:RequiredFieldValidator ID="Q1requiredvalidator" runat="server" Display="Dynamic" ControlToValidate="RadioButtonList1" CssClass="text-danger" ErrorMessage="Ensure question 1 is completed"></asp:RequiredFieldValidator>
</div>
<asp:Label ID="QLabel2" runat="server" Text="Question 2"></asp:Label>
<asp:RadioButtonList ID="RadioButtonList2" runat="server"
RepeatDirection="Horizontal" RepeatLayout="Table">
<asp:ListItem Text="*Answer 1*" Value="Incorrect 1" />
<asp:ListItem Text="*Answer 2*" Value="Correct" />
<asp:ListItem Text="*Answer 3*" Value="I" />
<asp:ListItem Text="*Answer 4*" Value="4" />
</asp:RadioButtonList>
<div>
<asp:RequiredFieldValidator ID="Q2requiredvalidator" runat="server" Display="Dynamic" ControlToValidate="RadioButtonList2" CssClass="text-danger" ErrorMessage="Ensure question 2 is completed"></asp:RequiredFieldValidator>
</div>
<asp:Label ID="QLabel3" runat="server" Text="Question 3"></asp:Label>
<asp:RadioButtonList ID="RadioButtonList3" runat="server"
RepeatDirection="Horizontal" RepeatLayout="Table">
<asp:ListItem Text="*Answer 1*" Value="Incorrect 1" />
<asp:ListItem Text="*Answer 2*" Value="Correct" />
<asp:ListItem Text="*Answer 3*" Value="I" />
<asp:ListItem Text="*Answer 4*" Value="4" />
</asp:RadioButtonList>
<div>
<asp:RequiredFieldValidator ID="Q3requiredvalidator" runat="server" Display="Dynamic" ControlToValidate="RadioButtonList3" CssClass="text-danger" ErrorMessage="Ensure question 3 is completed"></asp:RequiredFieldValidator>
</div>
<asp:Label ID="QLabel4" runat="server" Text="Question 4"></asp:Label>
<asp:RadioButtonList ID="RadioButtonList4" runat="server"
RepeatDirection="Horizontal" RepeatLayout="Table">
<asp:ListItem Text="*Answer 1*" Value="Incorrect 1" />
<asp:ListItem Text="*Answer 2*" Value="Correct" />
<asp:ListItem Text="*Answer 3*" Value="I" />
<asp:ListItem Text="*Answer 4*" Value="4" />
</asp:RadioButtonList>
<div>
<asp:RequiredFieldValidator ID="Q4requiredvalidator" runat="server" Display="Dynamic" ControlToValidate="RadioButtonList4" CssClass="text-danger" ErrorMessage="Ensure question 4 is completed"></asp:RequiredFieldValidator>
</div>
<asp:Label ID="QLabel5" runat="server" Text="Question 5"></asp:Label>
<asp:RadioButtonList ID="RadioButtonList5" runat="server"
RepeatDirection="Horizontal" RepeatLayout="Table">
<asp:ListItem Text="*Answer 1*" Value="Incorrect 1" />
<asp:ListItem Text="*Answer 2*" Value="Correct" />
<asp:ListItem Text="*Answer 3*" Value="I" />
<asp:ListItem Text="*Answer 4*" Value="4" />
</asp:RadioButtonList>
<div>
<asp:RequiredFieldValidator ID="Q5requiredvalidator" runat="server" Display="Dynamic" ControlToValidate="RadioButtonList5" CssClass="text-danger" ErrorMessage="Ensure question 5 is completed"></asp:RequiredFieldValidator>
</div>
<asp:Label ID="QLabel6" runat="server" Text="Question 6"></asp:Label>
<asp:RadioButtonList ID="RadioButtonList6" runat="server"
RepeatDirection="Horizontal" RepeatLayout="Table">
<asp:ListItem Text="*Answer 1*" Value="Incorrect 1" />
<asp:ListItem Text="*Answer 2*" Value="Correct" />
<asp:ListItem Text="*Answer 3*" Value="I" />
<asp:ListItem Text="*Answer 4*" Value="4" />
</asp:RadioButtonList>
<div>
<asp:RequiredFieldValidator ID="Q6requiredvalidator" runat="server" Display="Dynamic" ControlToValidate="RadioButtonList6" CssClass="text-danger" ErrorMessage="Ensure question 6 is completed"></asp:RequiredFieldValidator>
</div>
<asp:Label ID="QLabel7" runat="server" Text="Question 7"></asp:Label>
<asp:RadioButtonList ID="RadioButtonList7" runat="server"
RepeatDirection="Horizontal" RepeatLayout="Table">
<asp:ListItem Text="*Answer 1*" Value="Incorrect 1" />
<asp:ListItem Text="*Answer 2*" Value="Correct" />
<asp:ListItem Text="*Answer 3*" Value="I" />
<asp:ListItem Text="*Answer 4*" Value="4" />
</asp:RadioButtonList>
<div>
<asp:RequiredFieldValidator ID="Q7requiredvalidator" runat="server" Display="Dynamic" ControlToValidate="RadioButtonList7" CssClass="text-danger" ErrorMessage="Ensure question 7 is completed"></asp:RequiredFieldValidator>
</div>
<asp:Label ID="QLabel8" runat="server" Text="Question 8"></asp:Label>
<asp:RadioButtonList ID="RadioButtonList8" runat="server"
RepeatDirection="Horizontal" RepeatLayout="Table">
<asp:ListItem Text="*Answer 1*" Value="Incorrect 1" />
<asp:ListItem Text="*Answer 2*" Value="Correct" />
<asp:ListItem Text="*Answer 3*" Value="I" />
<asp:ListItem Text="*Answer 4*" Value="4" />
</asp:RadioButtonList>
<div>
<asp:RequiredFieldValidator ID="Q8requiredvalidator" runat="server" Display="Dynamic" ControlToValidate="RadioButtonList8" CssClass="text-danger" ErrorMessage="Ensure question 8 is completed"></asp:RequiredFieldValidator>
</div>
<asp:Label ID="QLabel9" runat="server" Text="Question 9"></asp:Label>
<asp:RadioButtonList ID="RadioButtonList9" runat="server"
RepeatDirection="Horizontal" RepeatLayout="Table">
<asp:ListItem Text="*Answer 1*" Value="Incorrect 1" />
<asp:ListItem Text="*Answer 2*" Value="Correct" />
<asp:ListItem Text="*Answer 3*" Value="I" />
<asp:ListItem Text="*Answer 4*" Value="4" />
</asp:RadioButtonList>
<div>
<asp:RequiredFieldValidator ID="Q9requiredvalidator" runat="server" Display="Dynamic" ControlToValidate="RadioButtonList9" CssClass="text-danger" ErrorMessage="Ensure question 9 is completed"></asp:RequiredFieldValidator>
</div>
<asp:Label ID="QLabel10" runat="server" Text="Question 10"></asp:Label>
<asp:RadioButtonList ID="RadioButtonList10" runat="server"
RepeatDirection="Horizontal" RepeatLayout="Table">
<asp:ListItem Text="*Answer 1*" Value="Incorrect 1" />
<asp:ListItem Text="*Answer 2*" Value="Correct" />
<asp:ListItem Text="*Answer 3*" Value="I" />
<asp:ListItem Text="*Answer 4*" Value="4" />
</asp:RadioButtonList>
<div>
<asp:RequiredFieldValidator ID="Q10requiredvalidator" runat="server" Display="Dynamic" ControlToValidate="RadioButtonList10" CssClass="text-danger" ErrorMessage="Ensure question 10 is completed"></asp:RequiredFieldValidator>
</div>
<asp:Button ID="Button1" runat="server" Text="Submit Final Answers" OnClick="Submit_Click" Visible="true" />
<script runat="server">
protected void Submit_Click(object sender, EventArgs e)
{
int score = 0;
List<RadioButtonList> list = new List<RadioButtonList>() { RadioButtonList1, RadioButtonList2, RadioButtonList3, RadioButtonList4, RadioButtonList5, RadioButtonList6, RadioButtonList7, RadioButtonList8, RadioButtonList9, RadioButtonList10 };
foreach (var element in list)
{
if (element.SelectedValue == "Correct")
{
score++;
}
}
Response.Write("you scored: " + score);
Button1.Visible = false;
}
</script>
</asp:Content>
Before hitting submit:
After Hitting submit when no answers are filled:
My submit button dissapears like it is supposed to but i want to be able to stop it from being submitted and the score output until all questions are answered.
New to all these so sorry if this seems easy or stupid.
Thanks

Ajaxtoolkit combobox in gridview

I'm having UI problems with ajaxtoolkit's combo box inside gridview.
<asp:Gridview Id="grv" runat="server" AutoGenerateColumns="False" CellPadding="4">
<Columns>
<asp:TempalteField>
<ItemTemplate>
<cc1:ComboBox ID="cmbGrdOrigin" runat="server" AutoCompleteMode="Suggest" Visible="false" DropDownStyle="DropDown"
AutoPostBack="true">
<asp:ListItem Text="" Value="" />
<asp:ListItem Text="Domestic" Value="Domestic" />
<asp:ListItem Text="Foreign" Value="Foreign" />
</cc1:ComboBox>
</ItemTemplate>
<asp:TempalteField>
</Columns>
</asp:Gridview>
The problem is the list of items are not showing. The combobox item list does not overlap with the cell below it.
Added photo for reference. all the combobox on the grid does not work https://drive.google.com/open?id=0B4JBZR0AZcx9WjNkbjM5UkZ1Y2c

ASPX event not firing

I just want to ask what's the problem in my codes here? The OnSelectedIndexChanged event in #cbList is working fine but the ondrop event in #sortable2 is not working. I tried placing it inside and outside the update panel. The code that is being called in the ondrop is in code behind. It just change the dummyText's text.
Here's my code:
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="true">
<ContentTemplate>
<input type="hidden" runat="server" id="hdnSelectedValue" />
<asp:CheckBoxList ID="cbList" runat="server" AutoPostBack="true" OnSelectedIndexChanged="cbList_SelectedIndexChanged">
<asp:ListItem Text="One" Value="1">
</asp:ListItem>
<asp:ListItem Text="Two" Value="2">
</asp:ListItem>
<asp:ListItem Text="Three" Value="3">
</asp:ListItem>
<asp:ListItem Text="Four" Value="4">
</asp:ListItem>
<asp:ListItem Text="Five" Value="5">
</asp:ListItem>
</asp:CheckBoxList>
<br />
<asp:Label ID="lblSelection" runat="server"></asp:Label>
<asp:Label ID="dummyText" runat="server" Text="Here"></asp:Label>
<ul id="sortable2" runat="server" class="connectedSortable" ondrop="Change_Text"></ul>
</ContentTemplate>
</asp:UpdatePanel>
</form>
ondrop="" is client side event. You cannot wired up this event in code behind.
Check this link for more information on OnDrop event

How can I change the output of a label, based on what's selected from a dropdown list (C# in ASP.NET)

What I want to happen is to create a statement that describes the type of work the user selects in the second dropdown list (titled workList). When the user selects one of the options from the dropdown list, I want a short text description of that work to appear where the label below it is (titled lblWork). I only have one option in the event (workListChanged) right now. Once I figure out how to make THAT display, I should be able to finish the rest. But I can't figure out how to get the label to display something based on the selection. The error I'm currently receiving is "cannot implicity convert type 'string' to 'bool' in the "if" statement in the workListChanged event.
<%# Page Language="C#" Debug="true" %>
<!DOCTYPE html>
<script runat="server">
protected void workListChanged(object sender, EventArgs e)
{
if (workList.SelectedItem.Text = "Office Work")
lblWork.Text = "You prefer to stay inside and code your life away.";
}
</script>
<html>
<head id="Head1" runat="server">
<title>Personality Test</title>
<style>
ul {
list-style-type: none;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label id="lblName"
Text="Name"
AssociatedControlID="txtName"
runat="server" />
<asp:TextBox
id="txtname"
AutoPostBack="true"
runat="server" />
<br /><br />
<asp:TextBox
id="textComments"
Text="Tell me a little about yourself"
TextMode="MultiLine"
Columns="30"
rows="10"
runat="server" />
<br /><br />
Select a gender:
<asp:RadioButton
id="rd1Male"
Text="Male"
GroupName="rgGender"
runat="server" />
<asp:RadioButton
id="rd1Female"
Text="Female"
GroupName="rgGender"
runat="server" />
<br /><br />
<strong>Favorite Season:</strong>
<br />
<asp:DropDownList
id="DropDownList1"
Runat="server"
AutoPostBack="true"
>
<asp:ListItem Text="Spring" />
<asp:ListItem Text="Summer" />
<asp:ListItem Text="Autumn" />
<asp:ListItem Text="Winter" />
</asp:DropDownList>
<br /><br />
<strong>Which of the following colors are your favorite?</strong>
<ul>
<li>
<asp:RadioButton
id="rd1Red"
Text="Red"
GroupName="colors"
runat="server" />
</li>
<li>
<asp:RadioButton
id="rd1Blue"
Text="Blue"
GroupName="colors"
runat="server" />
</li>
<li>
<asp:RadioButton
id="rd1Purple"
Text="Purple"
GroupName="colors"
runat="server" />
</li>
<li>
<asp:RadioButton
id="rd1Yellow"
Text="Yellow"
GroupName="colors"
runat="server" />
</li>
<li>
<asp:RadioButton
id="rd1Green"
Text="Green"
GroupName="colors"
runat="server" />
</li>
<li>
<asp:RadioButton
id="rd1Orange"
Text="Orange"
GroupName="colors"
runat="server" />
</li>
<li>
<asp:RadioButton
id="rd1Violet"
Text="Violet"
GroupName="colors"
runat="server" />
</li>
<li>
<asp:RadioButton
id="rd1Pink"
Text="Pink"
GroupName="colors"
runat="server" />
</li>
<li>
<asp:RadioButton
id="rd1Brown"
Text="Brown"
GroupName="colors"
runat="server" />
</li>
<li>
<asp:RadioButton
id="d1Grey"
Text="Grey"
GroupName="colors"
runat="server" />
</li>
</ul>
<br /><br />
<strong>Which type of work do you prefer?</strong>
<br />
<asp:DropDownList
id="workList"
Runat="server"
AutoPostBack="true"
OnSelectedIndexChanged="workListChanged">
<asp:ListItem Text="Office Work" />
<asp:ListItem Text="Outdoor Work" />
<asp:ListItem Text="Investigative Work" />
<asp:ListItem Text="Working With People" />
<asp:ListItem Text="Work Requiring Travel" />
<asp:ListItem Text="Helping People" />
</asp:DropDownList>
<br />
<asp:Label
id="lblWork"
runat ="server" />
</div>
</form>
</body>
</html>
Try changing your if statement as below. You are using a = which means you are trying to assign value, rather use == for string comparison. You cannot do if (stringExpression), since an if statement only works on a boolean.
protected void workListChanged(object sender, EventArgs e)
{
if (workList.SelectedItem.Text == "Office Work")
lblWork.Text = "You prefer to stay inside and code your life away.";
}

how to check form after postback

Please cosider this scenario:
I have a form with 3 drop down list.I place these controls in an update panel.when my users select an Item with value greater that 2 in first drop down list I disable second and third drop down list with jQuery. My problem is after any post back all drop down list are enable.I know this is normal but how I can check the form again and disable controls that should be disable?
thanks
Edit 1)
this is my code:
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<table border="1" cellpadding="0" cellspacing="0">
<tr>
<td>
<asp:DropDownList ID="DropDownList1" runat="server" Width="100px">
<asp:ListItem Value="1" Text="1" />
<asp:ListItem Value="2" Text="2" />
<asp:ListItem Value="3" Text="3" />
</asp:DropDownList>
</td>
</tr>
<tr>
<td>
<asp:DropDownList ID="DropDownList2" runat="server" Width="100px">
<asp:ListItem Value="1" Text="1" />
<asp:ListItem Value="2" Text="2" />
<asp:ListItem Value="3" Text="3" />
</asp:DropDownList>
</td>
</tr>
<tr>
<td>
<asp:DropDownList ID="DropDownList3" runat="server" Width="100px">
<asp:ListItem Value="1" Text="1" />
<asp:ListItem Value="2" Text="2" />
<asp:ListItem Value="3" Text="3" />
</asp:DropDownList>
<br />
</td>
</tr>
</table>
<div>
<asp:Button ID="Button1" runat="server" Text="Cause Post Back" Width="200px"
onclick="Button1_Click"/>
</div>
</ContentTemplate>
</asp:UpdatePanel>
and javascript:
$(document).ready(function () {
function Disable(item) {
item.attr('disabled', 'disabled');
}
$('#DropDownList1').change(function () {
if ($(this).val() > 2) {
Disable($('#DropDownList2'));
Disable($('#DropDownList3'));
}
else {
Enable($('#DropDownList2'));
Enable($('#DropDownList3'));
}
}).change();
function Enable(item) {
item.removeAttr('disabled');
}
});
Am I missing something?
$('#dropdown1').change(function()
{
if ( $(this).val() > 2 )
{
$('#dropdown2, #dropdown3').prop('disabled', true);
}
else
{
$('#dropdown2, #dropdown3').prop('disabled', false);
}
})
.change();
The problem is due to updatepanel, because it is partialy post back your page.
Please put your Jquery inside a function pageLoad()
function pageLoad() {
// Put your code here...
}
Hope it will solve your issue.
try this
AutoPostBack="true"
as
<asp:DropDownList ID="DropDownList1" runat="server" Width="100px"
AutoPostBack="true">
<asp:ListItem Value="1" Text="1" />
<asp:ListItem Value="2" Text="2" />
<asp:ListItem Value="3" Text="3" />
</asp:DropDownList>
Add a hidden field, which stores the state of the dropdowns..for example 0=disabled, 1=enabled...when you disable the dropdown set the value of the hidden field as 0..when enabled set it as 1. Then when the form is posted and reloaded, read the value of the hidden fields and enable/disable the dropdowns accordingly.
$(function(){
if($("#hiddenFieldID").val()=="0")
//disable
else
//enable
});

Categories

Resources