I'm working on a project and I'm a beginner and I'm having a little trouble with this. I'm trying to check if a textbox is empty and if it is to change the value to N/A so that I can input n/a into a database instead of it not working.
我正在做一个项目,我是初学者,我遇到了一些麻烦。我正在尝试检查文本框是否为空以及是否要将值更改为N / A以便我可以将n / a输入到数据库中而不是它不起作用。
Here is the code that I thought would work but didn't because the .Text property isn't near the ID anymore:
这是我认为可以工作的代码,但没有因为.Text属性不再靠近ID:
for(int i = 1; i<=17; i++)
{
if(!("tb" + i).Text)
"tb" + i.Text = "n/a";
}
I wasn't sure if the true/false would work but I never got to find out because it doesn't compile to begin with. I have 17 textboxes on my design page all with ID 'tb + i' e.g tb1, tb2
我不确定真/假是否会起作用,但我从来没有找到,因为它没有编译开始。我的设计页面上有17个文本框,ID为'tb + i',例如tb1,tb2
thx
2 个解决方案
#1
3
If you want to loop through the textbox ids, you have to find the control on the page first using the FindControl
method.
如果要遍历文本框ID,则必须首先使用FindControl方法在页面上找到控件。
Then you can create your loop like this:
然后你可以像这样创建你的循环:
TextBox txt;
for(int i = 1; i<=17; i++)
{
txt = (TextBox)Page.FindControl("tb" + i);
if(string.IsNullOrEmpty(txt.Text))
txt.Text = "n/a";
}
#2
2
You can use FindControl
method
您可以使用FindControl方法
for(int i = 1; i<=17; i++)
{
var textBox = Page.FindControl("tb" + i) as TextBox;
if(textBox != null && textBox.Text == "") { ... }
}
#1
3
If you want to loop through the textbox ids, you have to find the control on the page first using the FindControl
method.
如果要遍历文本框ID,则必须首先使用FindControl方法在页面上找到控件。
Then you can create your loop like this:
然后你可以像这样创建你的循环:
TextBox txt;
for(int i = 1; i<=17; i++)
{
txt = (TextBox)Page.FindControl("tb" + i);
if(string.IsNullOrEmpty(txt.Text))
txt.Text = "n/a";
}
#2
2
You can use FindControl
method
您可以使用FindControl方法
for(int i = 1; i<=17; i++)
{
var textBox = Page.FindControl("tb" + i) as TextBox;
if(textBox != null && textBox.Text == "") { ... }
}