现在我正在做asp.net,我有30个文本框,我使用数字将它们区别开来。
实施例: txtC1Name txtC2Name txtC3Name .... txtC30Name
我计划使用for循环并测试它是否为空但我无法获得该值。
int count = 30;
string x = "asd";
for (int i = 0; i < count; i++)
{
string test = "txtC" + i + "Name.text";
if (validate.isEmpty(test) == true)
{
x = "LOL";
}
blC9Type.Text = test;
}
lblC10Type.Text = x;
我得到的是txtC1Name而不是框中的值。
任何人都知道其他解决方案可以做什么或我的代码出错了什么?
这是我在UI中的asp.net代码
<table class="rightT">
<tr>
<td><asp:Label ID="lblTaskName" runat="server" Text="Task Name :"></asp:Label> </td>
<td><asp:TextBox ID="txtTaskName" runat="server"></asp:TextBox></td>
<td><asp:Label ID="lblCreateOnM" runat="server" Text="Created On :"></asp:Label></td>
<td><asp:Label ID="lblCreateOn" runat="server" Text="Label"></asp:Label></td>
</tr>
<tr>
<td><asp:Label ID="lblSubsystem" runat="server" Text="Subsystem :"></asp:Label></td>
<td><asp:TextBox ID="txtSubsystem" runat="server"></asp:TextBox></td>
<td><asp:Label ID="lblUpdateOnM" runat="server" Text="Updated On :"></asp:Label></td>
<td><asp:Label ID="lblUpdateOn" runat="server" Text="Label"></asp:Label></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><asp:Label ID="lblUpdateByM" runat="server" Text="Updated By :"></asp:Label></td>
<td><asp:Label ID="lblUpdateBy" runat="server" Text="Label"></asp:Label></td>
</tr>
<tr>
<td> </td>
<td>
<asp:Label ID="lblError" runat="server"></asp:Label>
</td>
<td><asp:Label ID="lblStatusM" runat="server" Text="Status :"></asp:Label></td>
<td><asp:Label ID="lblStatus" runat="server" Text="Label"></asp:Label></td>
</tr>
<tr>
<td><hr /></td>
<td><hr /></td>
<td><hr /></td>
<td><hr /></td>
</tr>
<tr>
<td><asp:Label ID="lblC1Name" runat="server" Text="Column Name 1:"></asp:Label></td>
<td><asp:TextBox ID="txtC1Name" runat="server" ></asp:TextBox></td>
<td><asp:Label ID="lblC1Type" runat="server" Text="Column Type 1:"></asp:Label></td>
<td>
<asp:RadioButton ID="RadioC11" runat="server" Text="text" GroupName="C1" />
<asp:RadioButton ID="RadioC12" runat="server" Text="y/n" GroupName="C1"/>
<asp:RadioButton ID="RadioC13" runat="server" Text="low/med/high" GroupName="C1"/>
</td>
</tr>
<tr>
<td><asp:Label ID="lblC2Name" runat="server" Text="Column Name 2:"></asp:Label></td>
<td><asp:TextBox ID="txtC2Name" runat="server"></asp:TextBox></td>
<td><asp:Label ID="lblC2Type" runat="server" Text="Column Type 2:"></asp:Label></td>
<td>
<asp:RadioButton ID="RadioC21" runat="server" Text="text" GroupName="C2"/>
<asp:RadioButton ID="RadioC22" runat="server" Text="y/n" GroupName="C2"/>
<asp:RadioButton ID="RadioC23" runat="server" Text="low/med/high" GroupName="C2"/>
</td>
</tr>
<tr>
<td><asp:Label ID="lblC3Name" runat="server" Text="Column Name 3:"></asp:Label></td>
<td><asp:TextBox ID="txtC3Name" runat="server"></asp:TextBox></td>
<td><asp:Label ID="lblC3Type" runat="server" Text="Column Type 3:"></asp:Label></td>
<td>
<asp:RadioButton ID="RadioC31" runat="server" Text="text" GroupName="C3"/>
<asp:RadioButton ID="RadioC32" runat="server" Text="y/n" GroupName="C3"/>
<asp:RadioButton ID="RadioC33" runat="server" Text="low/med/high" GroupName="C3"/>
</td>
</tr>
<tr>
<td><asp:Label ID="lblC4Name" runat="server" Text="Column Name 4:"></asp:Label></td>
<td><asp:TextBox ID="txtC4Name" runat="server"></asp:TextBox></td>
<td><asp:Label ID="lblC4Type" runat="server" Text="Column Type 4:"></asp:Label></td>
<td>
<asp:RadioButton ID="RadioC41" runat="server" Text="text" GroupName="C4"/>
<asp:RadioButton ID="RadioC42" runat="server" Text="y/n" GroupName="C4"/>
<asp:RadioButton ID="RadioC43" runat="server" Text="low/med/high" GroupName="C4"/>
</td>
</tr>
<tr>
<td><asp:Label ID="lblC5Name" runat="server" Text="Column Name 5:"></asp:Label></td>
<td><asp:TextBox ID="txtC5Name" runat="server"></asp:TextBox></td>
<td><asp:Label ID="lblC5Type" runat="server" Text="Column Type 5:"></asp:Label></td>
<td>
<asp:RadioButton ID="RadioC51" runat="server" Text="text" GroupName="C5"/>
<asp:RadioButton ID="RadioC52" runat="server" Text="y/n" GroupName="C5"/>
<asp:RadioButton ID="RadioC53" runat="server" Text="low/med/high" GroupName="C5"/>
</td>
</tr>
<tr>
<td><asp:Label ID="lblC6Name" runat="server" Text="Column Name 6:"></asp:Label></td>
<td><asp:TextBox ID="txtC6Name" runat="server"></asp:TextBox></td>
<td><asp:Label ID="lblC6Type" runat="server" Text="Column Type 6:"></asp:Label></td>
<td>
<asp:RadioButton ID="RadioC61" runat="server" Text="text" GroupName="C6"/>
<asp:RadioButton ID="RadioC62" runat="server" Text="y/n" GroupName="C6"/>
<asp:RadioButton ID="RadioC63" runat="server" Text="low/med/high" GroupName="C6"/>
</td>
</tr>
<tr>
<td><asp:Label ID="lblC7Name" runat="server" Text="Column Name 7:"></asp:Label></td>
<td><asp:TextBox ID="txtC7Name" runat="server"></asp:TextBox></td>
<td><asp:Label ID="lblC7Type" runat="server" Text="Column Type 7:"></asp:Label></td>
<td>
<asp:RadioButton ID="RadioC71" runat="server" Text="text" GroupName="C7"/>
<asp:RadioButton ID="RadioC72" runat="server" Text="y/n" GroupName="C7"/>
<asp:RadioButton ID="RadioC73" runat="server" Text="low/med/high" GroupName="C7"/>
</td>
</tr>
<tr>
<td><asp:Label ID="lblC8Name" runat="server" Text="Column Name 8:"></asp:Label></td>
<td><asp:TextBox ID="txtC8Name" runat="server"></asp:TextBox></td>
<td><asp:Label ID="lblC8Type" runat="server" Text="Column Type 8:"></asp:Label></td>
<td>
<asp:RadioButton ID="RadioC81" runat="server" Text="text" GroupName="C8"/>
<asp:RadioButton ID="RadioC82" runat="server" Text="y/n" GroupName="C8"/>
<asp:RadioButton ID="RadioC83" runat="server" Text="low/med/high" GroupName="C8"/>
</td>
</tr>
<tr>
<td><asp:Label ID="lblC9Name" runat="server" Text="Column Name 9:"></asp:Label></td>
<td><asp:TextBox ID="txtC9Name" runat="server"></asp:TextBox></td>
<td><asp:Label ID="lblC9Type" runat="server" Text="Column Type 9:"></asp:Label></td>
<td>
<asp:RadioButton ID="RadioC91" runat="server" Text="text" GroupName="C9"/>
<asp:RadioButton ID="RadioC92" runat="server" Text="y/n" GroupName="C9"/>
<asp:RadioButton ID="RadioC93" runat="server" Text="low/med/high" GroupName="C9"/>
</td>
</tr>
<tr>
<td><asp:Label ID="lblC10Name" runat="server" Text="Column Name 10:"></asp:Label></td>
<td><asp:TextBox ID="txtC10Name" runat="server"></asp:TextBox></td>
<td><asp:Label ID="lblC10Type" runat="server" Text="Column Type 10:"></asp:Label></td>
<td>
<asp:RadioButton ID="RadioC101" runat="server" Text="text" GroupName="C10"/>
<asp:RadioButton ID="RadioC102" runat="server" Text="y/n" GroupName="C10"/>
<asp:RadioButton ID="RadioC103" runat="server" Text="low/med/high" GroupName="C10"/>
</td>
</tr>
<tr>
<td><asp:Label ID="lblC11Name" runat="server" Text="Column Name 11:"></asp:Label></td>
<td><asp:TextBox ID="txtC11Name" runat="server"></asp:TextBox></td>
<td><asp:Label ID="lblC11Type" runat="server" Text="Column Type 11:"></asp:Label></td>
<td>
<asp:RadioButton ID="RadioC111" runat="server" Text="text" GroupName="C11"/>
<asp:RadioButton ID="RadioC112" runat="server" Text="y/n" GroupName="C11"/>
<asp:RadioButton ID="RadioC113" runat="server" Text="low/med/high" GroupName="C11"/>
</td>
</tr>
<tr>
<td><asp:Label ID="lblC12Name" runat="server" Text="Column Name 12:"></asp:Label></td>
<td><asp:TextBox ID="txtC12Name" runat="server"></asp:TextBox></td>
<td><asp:Label ID="lblC12Type" runat="server" Text="Column Type 12:"></asp:Label></td>
<td>
<asp:RadioButton ID="RadioC121" runat="server" Text="text" GroupName="C12"/>
<asp:RadioButton ID="RadioC122" runat="server" Text="y/n" GroupName="C12"/>
<asp:RadioButton ID="RadioC123" runat="server" Text="low/med/high" GroupName="C12"/>
</td>
</tr>
<tr>
<td><asp:Label ID="lblC13Name" runat="server" Text="Column Name 13:"></asp:Label></td>
<td><asp:TextBox ID="txtC13Name" runat="server"></asp:TextBox></td>
<td><asp:Label ID="lblC13Type" runat="server" Text="Column Type 13:"></asp:Label></td>
<td>
<asp:RadioButton ID="RadioC131" runat="server" Text="text" GroupName="C13"/>
<asp:RadioButton ID="RadioC132" runat="server" Text="y/n" GroupName="C13"/>
<asp:RadioButton ID="RadioC133" runat="server" Text="low/med/high" GroupName="C13"/>
</td>
</tr>
<tr>
<td><asp:Label ID="lblC14Name" runat="server" Text="Column Name 14:"></asp:Label></td>
<td><asp:TextBox ID="txtC14Name" runat="server"></asp:TextBox></td>
<td><asp:Label ID="lblC14Type" runat="server" Text="Column Type 14:"></asp:Label></td>
<td>
<asp:RadioButton ID="RadioC141" runat="server" Text="text" GroupName="C14"/>
<asp:RadioButton ID="RadioC142" runat="server" Text="y/n" GroupName="C14"/>
<asp:RadioButton ID="RadioC143" runat="server" Text="low/med/high" GroupName="C14"/>
</td>
</tr>
<tr>
<td><asp:Label ID="lblC15Name" runat="server" Text="Column Name 12:"></asp:Label></td>
<td><asp:TextBox ID="txtC15Name" runat="server"></asp:TextBox></td>
<td><asp:Label ID="lblC15Type" runat="server" Text="Column Type 12:"></asp:Label></td>
<td>
<asp:RadioButton ID="RadioC151" runat="server" Text="text" GroupName="C15"/>
<asp:RadioButton ID="RadioC152" runat="server" Text="y/n" GroupName="C15"/>
<asp:RadioButton ID="RadioC153" runat="server" Text="low/med/high" GroupName="C15"/>
</td>
</tr>
<tr>
<td><asp:Label ID="lblC16Name" runat="server" Text="Column Name 16:"></asp:Label></td>
<td><asp:TextBox ID="txtC16Name" runat="server"></asp:TextBox></td>
<td><asp:Label ID="lblC16Type" runat="server" Text="Column Type 16:"></asp:Label></td>
<td>
<asp:RadioButton ID="RadioC161" runat="server" Text="text" GroupName="C16"/>
<asp:RadioButton ID="RadioC162" runat="server" Text="y/n" GroupName="C16"/>
<asp:RadioButton ID="RadioC163" runat="server" Text="low/med/high" GroupName="C16"/>
</td>
</tr>
<tr>
<td><asp:Label ID="lblC17Name" runat="server" Text="Column Name 17:"></asp:Label></td>
<td><asp:TextBox ID="txtC17Name" runat="server"></asp:TextBox></td>
<td><asp:Label ID="lblC17Type" runat="server" Text="Column Type 17:"></asp:Label></td>
<td>
<asp:RadioButton ID="RadioC171" runat="server" Text="text" GroupName="C17"/>
<asp:RadioButton ID="RadioC172" runat="server" Text="y/n" GroupName="C17"/>
<asp:RadioButton ID="RadioC173" runat="server" Text="low/med/high" GroupName="C17"/>
</td>
</tr>
<tr>
<td><asp:Label ID="lblC18Name" runat="server" Text="Column Name 18:"></asp:Label></td>
<td><asp:TextBox ID="txtC18Name" runat="server"></asp:TextBox></td>
<td><asp:Label ID="lblC18Type" runat="server" Text="Column Type 18:"></asp:Label></td>
<td>
<asp:RadioButton ID="RadioC181" runat="server" Text="text" GroupName="C18"/>
<asp:RadioButton ID="RadioC182" runat="server" Text="y/n" GroupName="C18"/>
<asp:RadioButton ID="RadioC183" runat="server" Text="low/med/high" GroupName="C18"/>
</td>
</tr>
<tr>
<td><asp:Label ID="lblC19Name" runat="server" Text="Column Name 19:"></asp:Label></td>
<td><asp:TextBox ID="txtC19Name" runat="server"></asp:TextBox></td>
<td><asp:Label ID="lblC19Type" runat="server" Text="Column Type 19:"></asp:Label></td>
<td>
<asp:RadioButton ID="RadioC191" runat="server" Text="text" GroupName="C19"/>
<asp:RadioButton ID="RadioC192" runat="server" Text="y/n" GroupName="C19"/>
<asp:RadioButton ID="RadioC193" runat="server" Text="low/med/high" GroupName="C19"/>
</td>
</tr>
<tr>
<td><asp:Label ID="lblC20Name" runat="server" Text="Column Name 20:"></asp:Label></td>
<td><asp:TextBox ID="txtC20Name" runat="server"></asp:TextBox></td>
<td><asp:Label ID="lblC20Type" runat="server" Text="Column Type 20:"></asp:Label></td>
<td>
<asp:RadioButton ID="RadioC201" runat="server" Text="text" GroupName="C20"/>
<asp:RadioButton ID="RadioC202" runat="server" Text="y/n" GroupName="C20"/>
<asp:RadioButton ID="RadioC203" runat="server" Text="low/med/high" GroupName="C20"/>
</td>
</tr>
<tr>
<td><asp:Label ID="lblC21Name" runat="server" Text="Column Name 21:"></asp:Label></td>
<td><asp:TextBox ID="txtC21Name" runat="server"></asp:TextBox></td>
<td><asp:Label ID="lblC21Type" runat="server" Text="Column Type 21:"></asp:Label></td>
<td>
<asp:RadioButton ID="RadioC211" runat="server" Text="text" GroupName="C21"/>
<asp:RadioButton ID="RadioC212" runat="server" Text="y/n" GroupName="C21"/>
<asp:RadioButton ID="RadioC213" runat="server" Text="low/med/high" GroupName="C21"/>
</td>
</tr>
<tr>
<td><asp:Label ID="lblC22Name" runat="server" Text="Column Name 22:"></asp:Label></td>
<td><asp:TextBox ID="txtC22Name" runat="server"></asp:TextBox></td>
<td><asp:Label ID="lblC22Type" runat="server" Text="Column Type 22:"></asp:Label></td>
<td>
<asp:RadioButton ID="RadioC221" runat="server" Text="text" GroupName="C22"/>
<asp:RadioButton ID="RadioC222" runat="server" Text="y/n" GroupName="C22"/>
<asp:RadioButton ID="RadioC223" runat="server" Text="low/med/high" GroupName="C22"/>
</td>
</tr>
<tr>
<td><asp:Label ID="lblC23Name" runat="server" Text="Column Name 23:"></asp:Label></td>
<td><asp:TextBox ID="txtC23Name" runat="server"></asp:TextBox></td>
<td><asp:Label ID="lblC23Type" runat="server" Text="Column Type 23:"></asp:Label></td>
<td>
<asp:RadioButton ID="RadioC231" runat="server" Text="text" GroupName="C23"/>
<asp:RadioButton ID="RadioC232" runat="server" Text="y/n" GroupName="C23"/>
<asp:RadioButton ID="RadioC233" runat="server" Text="low/med/high" GroupName="C23"/>
</td>
</tr>
<tr>
<td><asp:Label ID="lblC24Name" runat="server" Text="Column Name 24:"></asp:Label></td>
<td><asp:TextBox ID="txtC24Name" runat="server"></asp:TextBox></td>
<td><asp:Label ID="lblC24Type" runat="server" Text="Column Type 24:"></asp:Label></td>
<td>
<asp:RadioButton ID="RadioC241" runat="server" Text="text" GroupName="C24"/>
<asp:RadioButton ID="RadioC242" runat="server" Text="y/n" GroupName="C24"/>
<asp:RadioButton ID="RadioC243" runat="server" Text="low/med/high" GroupName="C24"/>
</td>
</tr>
<tr>
<td><asp:Label ID="lblC25Name" runat="server" Text="Column Name 25:"></asp:Label></td>
<td><asp:TextBox ID="txtC25Name" runat="server"></asp:TextBox></td>
<td><asp:Label ID="lblC25Type" runat="server" Text="Column Type 25:"></asp:Label></td>
<td>
<asp:RadioButton ID="RadioC251" runat="server" Text="text" GroupName="C25"/>
<asp:RadioButton ID="RadioC252" runat="server" Text="y/n" GroupName="C25"/>
<asp:RadioButton ID="RadioC253" runat="server" Text="low/med/high" GroupName="C25"/>
</td>
</tr>
<tr>
<td><asp:Label ID="lblC26Name" runat="server" Text="Column Name 26:"></asp:Label></td>
<td><asp:TextBox ID="txtC26Name" runat="server"></asp:TextBox></td>
<td><asp:Label ID="lblC26Type" runat="server" Text="Column Type 26:"></asp:Label></td>
<td>
<asp:RadioButton ID="RadioC261" runat="server" Text="text" GroupName="C26"/>
<asp:RadioButton ID="RadioC262" runat="server" Text="y/n" GroupName="C26"/>
<asp:RadioButton ID="RadioC263" runat="server" Text="low/med/high" GroupName="C26"/>
</td>
</tr>
<tr>
<td><asp:Label ID="lblC27Name" runat="server" Text="Column Name 27:"></asp:Label></td>
<td><asp:TextBox ID="txtC27Name" runat="server"></asp:TextBox></td>
<td><asp:Label ID="lblC27Type" runat="server" Text="Column Type 27:"></asp:Label></td>
<td>
<asp:RadioButton ID="RadioC271" runat="server" Text="text" GroupName="C27"/>
<asp:RadioButton ID="RadioC272" runat="server" Text="y/n" GroupName="C27"/>
<asp:RadioButton ID="RadioC273" runat="server" Text="low/med/high" GroupName="C27"/>
</td>
</tr>
<tr>
<td><asp:Label ID="lblC28Name" runat="server" Text="Column Name 28:"></asp:Label></td>
<td><asp:TextBox ID="txtC28Name" runat="server"></asp:TextBox></td>
<td><asp:Label ID="lblC28Type" runat="server" Text="Column Type 28:"></asp:Label></td>
<td>
<asp:RadioButton ID="RadioC281" runat="server" Text="text" GroupName="C28"/>
<asp:RadioButton ID="RadioC282" runat="server" Text="y/n" GroupName="C28"/>
<asp:RadioButton ID="RadioC283" runat="server" Text="low/med/high" GroupName="C28"/>
</td>
</tr>
<tr>
<td><asp:Label ID="lblC29Name" runat="server" Text="Column Name 29:"></asp:Label></td>
<td><asp:TextBox ID="txtC29Name" runat="server"></asp:TextBox></td>
<td><asp:Label ID="lblC29Type" runat="server" Text="Column Type 29:"></asp:Label></td>
<td>
<asp:RadioButton ID="RadioC291" runat="server" Text="text" GroupName="C29"/>
<asp:RadioButton ID="RadioC292" runat="server" Text="y/n" GroupName="C29"/>
<asp:RadioButton ID="RadioC293" runat="server" Text="low/med/high" GroupName="C29"/>
</td>
</tr>
<tr>
<td><asp:Label ID="lblC30Name" runat="server" Text="Column Name 30:"></asp:Label></td>
<td><asp:TextBox ID="txtC30Name" runat="server"></asp:TextBox></td>
<td><asp:Label ID="lblC30Type" runat="server" Text="Column Type 30:"></asp:Label></td>
<td>
<asp:RadioButton ID="RadioC301" runat="server" Text="text" GroupName="C30"/>
<asp:RadioButton ID="RadioC302" runat="server" Text="y/n" GroupName="C30"/>
<asp:RadioButton ID="RadioC303" runat="server" Text="low/med/high" GroupName="C30"/>
</td>
</tr>
<tr>
<td>
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/Image/back.button.jpg" Height="30px" Width="30px" OnClick="ImageButton1_Click" />
<asp:ImageButton ID="ImageButton2" runat="server" ImageUrl="~/Image/save-btn.png" Height="30px" Width="30px" OnClick="ImageButton2_Click" />
</td>
<td>
<asp:ImageButton ID="ImageButton3" runat="server" ImageUrl="~/Image/active.png" Height="30px" Width="30px" OnClick="ImageButton3_Click" />
<asp:ImageButton ID="ImageButton4" runat="server" ImageUrl="~/Image/inactive.png" Height="30px" Width="30px" OnClick="ImageButton4_Click" />
<asp:ImageButton ID="ImageButton5" runat="server" ImageUrl="~/Image/Delete.jpg" Height="30px" Width="30px" OnClick="OnConfirm" OnClientClick = "Confirm()"/>
</td>
<td> </td>
<td>
</td>
</tr>
</table>
答案 0 :(得分:2)
您可以使用查找控制方法....
例如:
for (int i = 0; i < count; i++)
{
string test = "txtC" + i + "Name";
Control myControl = this.FindControl(test);
TextBox txt = (TextBox)myControl;
if(myControl !=null)
{
if (validate.isEmpty(txt.Text) == true)
{
x = "LOL";
}
}
}