Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET C#4.0
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
 

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 3-Feb-13 20:20pm
shamjid1.4K
Edited 3-Feb-13 22:02pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
 
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++;
        }
    }
}
  Permalink  
Comments
shamjid at 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 at 4-Feb-13 5:24am
   
<form id="form1" runat="server">
<asp:TextBox ID="TextBox1" runat="server" valuetocheck="4">
</form>
shamjid at 4-Feb-13 5:44am
   
@Irbaz:-i cant get into if Statement
shamjid at 4-Feb-13 5:53am
   
@Irbaz:-its working fine...Thanks Alot..
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

ASPX Page
<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
 
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();
        }
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 335
1 Sergey Alexandrovich Kryukov 150
2 ChauhanAjay 120
3 BotCar 105
4 Dave Kreskowiak 100
0 Sergey Alexandrovich Kryukov 9,056
1 OriginalGriff 7,981
2 CPallini 2,613
3 Richard MacCutchan 2,121
4 Abhinav S 1,928


Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 4 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100