如何在asp.net应用程序中使用循环方法? [英] How to use loop method in asp .net application ?

查看:70
本文介绍了如何在asp.net应用程序中使用循环方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的网页上有超过50个文本框。每个文本框用于填充特定总和的答案。这些Texbox分为5行。这意味着连续10个文本框。每个行Texbox都有一个值。每行包含10个总和。例如,第一行总和文本框值为2。即从texbox1 ..... textbox10答案是2.如果我们在textbox1中输入2,那么我们的正确答案是1.如果我们在textbox1中输入2到textbox10那么我们的正确答案是10.如果我们输入任何错误的答案然后它将b计为错误的答案(例如,如果我输入3为textbox5 ans。那么我对texbox1到texbox10的整体计算ll b正确的答案= 9,错误的答案= 1)。这就是我必须为我的整行做的事情。





现在我在这个页面要做的就是当我完成我的总和时,我必须得到全面的正确答案和错误的答案 对于5行总和。这意味着50个文本框值。我必须按一下按钮执行此操作。对于10个texbox,我做的是





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();





对于50 texbox我认为这不是一个好方法。我可以用循环方法吗?我怎样才能做到这一点?我应该使用Tabindex进行循环吗?



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?

推荐答案

在你的HTML添加文本框中输入正确值



On your HTML add text box with correct value to check

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





这是通过循环来完成的。

form1.Controls这里form1是控件的名称包含文本框控件。你可以相应地调整条件。





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++;
        }
    }
}


ASPX页面

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文件







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


这篇关于如何在asp.net应用程序中使用循环方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆