Add 1 if checked correct answer - c#

For example i have two radio button list:
<asp:RadioButtonList ID="rbl1" runat="server" OnSelectedIndexChanged="rbl1_SelectedIndexChanged" AutoPostBack="True" >
<asp:ListItem Value="1"> 1</asp:ListItem>
<asp:ListItem Value="2"> 2</asp:ListItem>
<asp:ListItem Value="3"> 3</asp:ListItem>
<asp:ListItem Value="4"> 4</asp:ListItem>
</asp:RadioButtonList>
<asp:RadioButtonList ID="rbl2" runat="server" AutoPostBack="True" OnSelectedIndexChanged="rbl2_SelectedIndexChanged" >
<asp:ListItem Value="1"> 1</asp:ListItem>
<asp:ListItem Value="2"> 2</asp:ListItem>
<asp:ListItem Value="3"> 3</asp:ListItem>
<asp:ListItem Value="4"> 4</asp:ListItem>
</asp:RadioButtonList>
protected void rbl1_SelectedIndexChanged(object sender, EventArgs e)
{
if (rbl1.SelectedIndex == 1)
{
Label1.Text = "Correct Answer.";
}
else
{
Label1.Text = "Incorect";
}
}
protected void rbl2_SelectedIndexChanged(object sender, EventArgs e)
{
if (rbl2.SelectedIndex == 0)
{
Label2.Text = "Correct Answer";
}
else
{
Label2.Text = "Incorect";
}
and I want to if rbl1 selectedindex =1 add 1 value to a label (like Lable3) to show also if rbl2 selectedindex = 0, again sum 1 with previous score in label3.
Please help me, Thanks in advance.

Related

When a certain item on dropdownlist1 is selected, select automatically a certain item on dropdownlist2

I want to when the item x5 is selected on dropdownlist1 the item y0 is automatically selected on dropdownlist2
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_Itemchanged">
<asp:ListItem Value='5'>x5</asp:ListItem>
<asp:ListItem Value="4">x4</asp:ListItem>
<asp:ListItem Value="3">x3</asp:ListItem>
<asp:ListItem Value="2">x2</asp:ListItem>
<asp:ListItem Value="1">x1</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server">
<asp:ListItem Value="0.75">y0.75</asp:ListItem>
<asp:ListItem Value="0.50">y0.50</asp:ListItem>
<asp:ListItem Value="0.25">y0.25</asp:ListItem>
<asp:ListItem Value="0">y0</asp:ListItem>
</asp:DropDownList>
protected void DropDownList1_Itemchanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedItem.Value == "5")
{
DropDownList2.Items.FindByValue("0").Selected = true;
DropDownList2.Items.FindByValue("0.75").Attributes.Add("Disabled", "Disabled");
DropDownList2.Items.FindByValue("0.50").Attributes.Add("Disabled", "Disabled");
DropDownList2.Items.FindByValue("0.25").Attributes.Add("Disabled", "Disabled");
}
}
When I select the item x4 on dropdownlist1 and select the item y0.25 on dropdownlist2, and after that when I select x5 on the dropdownlist1 it gives me "Cannot have multiple items selected in a DropDownList"
Use the SelectedValue property on the list:
protected void DropDownList1_Itemchanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedItem.Value == "5")
{
DropDownList2.SelectedValue = "0";
}
}

Select multiple value in ListBox using ASP.NET and C#

Why "SelectedValue" selects only the first value in my multiple selection mode ?
<dt>Tags:</dt><dd>
<asp:ListBox ID="ListTag" runat="server" SelectionMode="Multiple"
DataSourceID="SqlDataSourceTag" DataTextField="tag_name"
DataValueField="tag_id">
</asp:ListBox>
<asp:SqlDataSource ID="SqlDataSourceTag" runat="server"
ConnectionString="<%$ ConnectionStrings:db_cc %>">
</asp:SqlDataSource>
</dd>
ListTag.SelectedValue = "Tag1"; <-- only this is selects
ListTag.SelectedValue = "Tag2";
in your asp page you have this.
<asp:ListBox ID="ListTag" runat="server" AutoPostBack="True" SelectionMode="Multiple" Width="250px">
<asp:ListItem Selected="False" Text="Tag-1" Value="1">Tag - 1</asp:ListItem>
<asp:ListItem Selected="False" Text="Tag-2" Value="2">Tag - 2</asp:ListItem>
<asp:ListItem Selected="False" Text="Tag-3" Value="3">Tag - 3</asp:ListItem>
<asp:ListItem Selected="False" Text="Tag-4" Value="4">Tag - 4</asp:ListItem>
<asp:ListItem Selected="False" Text="Tag-5" Value="5">Tag - 5</asp:ListItem>
<asp:ListItem Selected="False" Text="Tag-6" Value="6">Tag - 6</asp:ListItem>
<asp:ListItem Selected="False" Text="Tag-7" Value="7">Tag - 7</asp:ListItem>
<asp:ListItem Selected="False" Text="Tag-8" Value="8">Tag - 8</asp:ListItem>
<asp:ListItem Selected="False" Text="Tag-9" Value="9">Tag - 9</asp:ListItem>
<asp:ListItem Selected="False" Text="Tag-10" Value="10">Tag - 10</asp:ListItem>
</asp:ListBox>
then in your cs(Code Behind) in the event that you want you have this.
for (int i = 0; i < lsBox.Items.Count; i++)
{
var item = lsBox.Items[i];
string[] selectecvalues = new string[] { "1", "3", "6", "9" };
if (selectecvalues.Contains(item.Value)) // or item.Text if you like
{
lsBox.Items[i].Selected = true;
}
}
that will result in Tags 1,3,6,9 selectd

Asp.net Dropdownlist selected item value

I have a problem with the SelectedItem in the DropDownList
<asp:DropDownList ID="Etkin_Drop" runat="server" OnSelectedIndexChanged="Etkin_Drop_SelectedIndexChanged">
<asp:ListItem Text="Seç" Value="-1" Selected="True"></asp:ListItem>
<asp:ListItem Text="Aktif" Value="1"></asp:ListItem>
<asp:ListItem Text="Deaktif" Value="0"></asp:ListItem>
</asp:DropDownList>
First list item value is -1 but when I want to check in the if statement its not working
protected void Etkin_Drop_SelectedIndexChanged(object sender, EventArgs e)
{
if (Convert.ToInt32(Etkin_Drop.SelectedItem.Value) == -1)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "popup", "alert('Lütfen Bir Seçim Yapınız');", true);
}
else
{
Label4.Text = Etkin_Drop.SelectedItem.Value;
}
}
I could not define the problem
Add AutoPostBack Property to your DropDownList and set this property to True. Like this:
<asp:DropDownList ID="Etkin_Drop" runat="server"
OnSelectedIndexChanged="Etkin_Drop_SelectedIndexChanged" AutoPostBack="True">

Step by Step dropdown selection

Please help me if you have an idea on autopostback in asp.net using C#
i have 4 dropdown lists in one page
if i selects 1st dropdownlist then it enables 2nd dropdownlist
and after selection an option in 2nd dropdownlist 3rd dropdownlist enables
and after selection an option in 3rd drop down list 4th dropdown list enables
please let me know to do this task...
please help me
Try this:-
ASPX
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Enabled="true" Text="Select Value" Value="-1"></asp:ListItem>
<asp:ListItem Text="Value1" Value="1"></asp:ListItem>
<asp:ListItem Text="Value2" Value="2"></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged">
<asp:ListItem Enabled="true" Text="Select Value" Value="-1"></asp:ListItem>
<asp:ListItem Text="Value1" Value="1"></asp:ListItem>
<asp:ListItem Text="Value2" Value="2"></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="DropDownList3" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList3_SelectedIndexChanged">
<asp:ListItem Enabled="true" Text="Select Value" Value="-1"></asp:ListItem>
<asp:ListItem Text="Value1" Value="1"></asp:ListItem>
<asp:ListItem Text="Value2" Value="2"></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="DropDownList4" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList4_SelectedIndexChanged">
<asp:ListItem Enabled="true" Text="Select Value" Value="-1"></asp:ListItem>
<asp:ListItem Text="Value1" Value="1"></asp:ListItem>
<asp:ListItem Text="Value2" Value="2"></asp:ListItem>
</asp:DropDownList>
ASPX CS
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DropDownList1.Enabled = true;
DropDownList2.Enabled = false;
DropDownList3.Enabled = false;
DropDownList4.Enabled = false;
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList2.Enabled = true;
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList3.Enabled = true;
}
protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList4.Enabled = true;
}
you can use SelectedIndexChanged event of drop down list to do the server side functionality and set AutoPostBack = true so that on selection change your page will get re-loaded.
Check out this Fiddle .
Important part is to handle the change event of dropdown list and to show another dropdownlist.
$("#one").change(function()
{
$("#two").toggleClass("show",true);
});

ASP.Net JQuery customvalidator on multiple fields

I am trying to do validation on multiple fields with the custom validator in ASP.net using JQuery but I can't get it to work.
There is a drop down box which contains two values, based on these values other items appear.
So if the user selects option 1, then a text box appears, if they select option 2 the text box disappears and two drop down boxes appear.
Depending on what option they choose I want to validate their input.
What I have so far that is not working is
.ASPX
<asp:DropDownList ID="drpHeight" runat="server">
<asp:ListItem Value="CMs">CMs</asp:ListItem>
<asp:ListItem Value="Feet">Feet</asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="txtCm" runat="server" Width="100px"></asp:TextBox>
<asp:CustomValidator ID="CustomValidator9" runat="server" ErrorMessage="Please select you height"
ClientValidationFunction = "ValidateHeight" Display="Dynamic" ControlToValidate="txtCm" ValidateEmptyText="true"></asp:CustomValidator>
<asp:RegularExpressionValidator ID="revCm" runat="server"
ControlToValidate="txtCm" Display="Dynamic"
ErrorMessage="Please enter a valid number"
ValidationExpression="^([0-9]*|\d*\.\d{1}?\d*)$" SetFocusOnError="True"></asp:RegularExpressionValidator>
<br />
<br />
<asp:DropDownList ID="drpFeet" runat="server">
<asp:ListItem Value="-1">Select...</asp:ListItem>
<asp:ListItem Value="1"></asp:ListItem>
<asp:ListItem Value="2"></asp:ListItem>
<asp:ListItem Value="3"></asp:ListItem>
<asp:ListItem Value="4"></asp:ListItem>
<asp:ListItem Value="5"></asp:ListItem>
<asp:ListItem Value="6"></asp:ListItem>
<asp:ListItem Value="7"></asp:ListItem>
<asp:ListItem Value="8"></asp:ListItem>
</asp:DropDownList>
<asp:Label ID="lblFeet" runat="server" Text="Ft"></asp:Label>
<asp:DropDownList ID="drpInches" runat="server">
<asp:ListItem Value="-1">Select...</asp:ListItem>
<asp:ListItem Value="0"></asp:ListItem>
<asp:ListItem Value="1"></asp:ListItem>
<asp:ListItem Value="2"></asp:ListItem>
<asp:ListItem Value="3"></asp:ListItem>
<asp:ListItem Value="4"></asp:ListItem>
<asp:ListItem Value="5"></asp:ListItem>
<asp:ListItem Value="6"></asp:ListItem>
<asp:ListItem Value="7"></asp:ListItem>
<asp:ListItem Value="8"></asp:ListItem>
<asp:ListItem Value="9"></asp:ListItem>
<asp:ListItem Value="10"></asp:ListItem>
<asp:ListItem Value="11"></asp:ListItem>
</asp:DropDownList>
<asp:Label ID="lblInches" runat="server" Text="Inches"></asp:Label>
<br />
<br />
JQuery
function ValidateHeight(sender, args) {
if ($('#<%=drpHeight.ClientID%>').val = "CMs") {
if ($('#<%=txtCm.ClientID%>').val().length > 0) {
args.IsValid = true;
}
else {
args.IsValid = false;
return;
}
}
else if ($('#<%=drpHeight.ClientID%>').val = "Feet") {
args.IsValid = true;
}
}
</script>
At the moment it is not working, I did have it validating the CM b but then it stopped working and now I can't figure out why.
What is the best way to approach this? I've got another one for Weight to do as well, that one has 3 input possibilities.
I came across the answer finally, I will provide the answer for anyone else who wants to know how to do this.
Changed my customer validator to:
<asp:CustomValidator ID="CustomValidator9" runat="server" ErrorMessage="Please select you height"
ClientValidationFunction = "ValidateHeight" Display="Dynamic" ValidateEmptyText="true"></asp:CustomValidator>
Then my JQuery became
<script type="text/javascript">
function ValidateHeight(sender, args) {
var drpHeight = $('#<%=drpHeight.ClientID%>').val();
if (drpHeight == "CMs") {
if ($('#<%=txtCm.ClientID%>').val().length > 0) {
args.IsValid = true;
return;
}
else {
args.IsValid = false;
return;
}
}
else if (drpHeight == "Feet") {
var drpFeet = $('#<%=drpFeet.ClientID%>').val();
var drpInches = $('#<%=drpInches.ClientID%>').val();
if (drpFeet == -1 || drpInches == -1) {
args.IsValid = false;
return;
}
else {
args.IsValid = true;
return;
}
}
}
</script>

Categories

Resources