Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
3.33/5 (3 votes)
See more:
in my web page there r more-than 50 text-boxes. Each textbox is used to fill the answer of paricular sums. These Texboxes are divided into 5 rows.That means 10 textbox in a row. Each Row Texbox has a single value. Each row contains 10 sums. For Example first row sum textbox value is 2 . ie from texbox1 .....textbox10 the answer is 2. If we type 2 in textbox1 then our correct answer is 1. If we type 2 in textbox1 to textbox10 then our correct answer is 10.if we type any wrong answer then it will b counted as wrong answer( for example if i type 3 for textbox5 ans. then my overall calculation for texbox1 to texbox10 ll b "CORRECT ANSWER =9 , WRONG ANSWER=1 ). This what i have to do for my whole Rows.


Now what i have to do in this page is when i finished my sums i have to get total "CORRECT ANSWER and WRONG ANSWER" for 5 row sums. That means 50 text box values.. I have to perform this in a button click. For 10 texboxes wat i did was


C#
int _correct = 0;
int _wrong = 10;

if (txtbox1.Text == "5")
       {
           _correct++;

       }

       if (txtbox1.Text == "3")
       {
           _correct++;
       }
.
.
.
.
.
.
int _wrongfinal = _wrong - _correct;
        lblCorrextAnswer.Text = _correct.ToString();
        lblWrongAnswer.Text = _wrongfinal.ToString();


For 50 texbox i think this is not a good method. Can i do this with loop method?. How can i do that? Should i use Tabindex for looping?
Posted
Updated 3-Feb-13 22:02pm
v2

On your HTML add text box with correct value to check

<asp:textbox id="TextBox1" runat="server" valuetocheck="4" xmlns:asp="#unknown"></asp:textbox> 


This is how to do it through loop.
form1.Controls here form1 is the name of control that contains the textbox controls. You can adjust the conditions accordingly.

C#
int _correct = 0;
int _wrong = 0;


foreach (Control cntrl in form1.Controls)
{
    if (cntrl.GetType().Name == "TextBox")
    {
        TextBox txt = (TextBox)cntrl;
        string Valuetocheck = txt.Attributes["valuetocheck"];
        string GetInputValue = txt.Text;

        if (Valuetocheck == GetInputValue)
        {
            _correct++;
        }
        else
        {
            _wrong++;
        }
    }
}
 
Share this answer
 
Comments
shamjid 4-Feb-13 5:21am    
@Irbaz:- Could u plz explain form1 in form1.control and xmlns:asp="#unknown" and
also i'm using master page in my web page..Thanks for ur reply..
Irbaz Haider Hashmi 4-Feb-13 5:24am    
<form id="form1" runat="server">
<asp:TextBox ID="TextBox1" runat="server" valuetocheck="4">
</form>
shamjid 4-Feb-13 5:44am    
@Irbaz:-i cant get into if Statement
shamjid 4-Feb-13 5:53am    
@Irbaz:-its working fine...Thanks Alot..
ASPX Page
XML
<head id="Head1" runat="server">
    <title>Title</title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:Label ID="lblCorrect" runat="server" Text="Label"></asp:Label>
    <asp:Label ID="lblWrong" runat="server" Text="Label"></asp:Label>
    <asp:TextBox ID="TextBox1" runat="server" valuetocheck="4"></asp:TextBox>
    <asp:TextBox ID="TextBox2" runat="server" valuetocheck="4"></asp:TextBox>
    <asp:TextBox ID="TextBox3" runat="server" valuetocheck="3"></asp:TextBox>
    <asp:TextBox ID="TextBox4" runat="server" valuetocheck="3"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
    </form>
</body>
</html>




CS COde file

C#
protected void Button1_Click(object sender, EventArgs e)
        {
            int _correct = 0;
            int _wrong = 0;


            foreach (Control cntrl in form1.Controls)
            {
                if (cntrl.GetType().Name == "TextBox")
                {
                    TextBox txt = (TextBox)cntrl;
                    string Valuetocheck = txt.Attributes["valuetocheck"];
                    string GetInputValue = txt.Text;

                    if (Valuetocheck == GetInputValue)
                    {
                        _correct++;
                    }
                    else
                    {
                        _wrong++;
                    }
                }
            }

            lblCorrect.Text = _correct.ToString();
            lblWrong.Text = _wrong.ToString();
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900