只需单击一下按钮,即可获取超过3个文本框值 [英] fetching morethan 3 Textbox Values in a single button click
问题描述
目前在我的网页上,我有一个按钮和近20个文本框。每个文本框都有其默认值(例如Textbox1 =1和Textbox2 =2.... Textbox20 =20)。如果我在Textbox1中键入1(即默认值),那么我必须显示我的正确答案= 1和错误答案= 0。如果我输入所有答案,那么我的正确答案= 20错误的答案= 0..如果我输入5,而不是在Texbox1中输入1,那么这被认为是错误的答案。当我完成所有这20个文本框的输入时,我必须单击一个按钮,我必须显示正确答案和错误答案的状态..我已经通过使用if else来做到这一点..我在下面粘贴我的代码..我想知道有没有其他办法让这成为可能....
我的按钮点击活动
Currently in my web page I have one button and nearly 20 text boxes. Each text box has its default value(say Textbox1="1" and Textbox2="2"....Textbox20="20"). if i type 1 in Textbox1(ie.the default value),then i have to show that my "Correct answer =1" and "wrong Answer =0). If i typed all answer right then my "Correct Answer=20" and Wrong Answer=0"..Instead of typing 1 in Texbox1 if i type 5,then that is considered as Wrong answer. when i finish typing in all these 20 textboxes i have to click a button where i have to show the status of correct answer and wrong answer.. i already did this by using "if else"..i''m pasting my code below.. i want to know is there any other way to make this possible ....
in my button click event
int _correct = 0;
int _wrong = 0;
if (text2.Text == "2")
{
_correct++;
}
else
{
_wrong++;
}
if (text3.Text == "5")
{
_correct++;
}
else
{
_wrong++;
}
.
.
.
.
.
lblCorrect.Visible = true;
lblCorrect.Text = _correct.ToString();
lblWrong.Visible = true;
lblWrong.Text = _wrong.ToString();
推荐答案
您可以使用以下方法避免使用else块。
you can avoid the else block by using the following approach.
int _correct = 0;
int _wrong = 20;
if (text2.Text == "2")
{
_correct++;
_wrong--;
}
if (text3.Text == "5")
{
_correct++;
_wrong--;
}
这篇关于只需单击一下按钮,即可获取超过3个文本框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!