循环随机数量的文本框并获取值

时间:2016-05-30 09:43:22

标签: c# asp.net loops for-loop

在经典ASP中,我可以在循环未知输入字段时执行此操作:

<input id="textbox1" type="text">
<input id="textbox2" type="text">
<input id="textbox3" type="text">
<input id="textbox4" type="text">
<input id="textbox5" type="text">   

For i = 1 To 5  

   strTextbox = request.form("textbox" & i)

   If strTextbox <> "" Then 
    // Do the magic!
   End If

Next

有了这个,用户可以输入文本框1,3,4和5或者只有1和2的值,我可以收集For循环中的值输入。

我怎么能在C#中做到这一点?

我不能这样做,因为它像我在中间添加一个我的textbox.Text;

for (int i = 1; i < 6; i++)
{
   strTextbox = textbox[i].Text; 

   if (!string.IsNullOrEmpty(strTextbox)
   {
     // Do the magic!
   }
}

我现在有很多if:s检查循环中的每个文本框但是它必须是一个更简单的方法吗?

2 个答案:

答案 0 :(得分:2)

您可以在文本框的NamingContainer上使用FindControl

如果它们位于页面顶部而未嵌套在其他控件中,例如GridView

for (int i = 1; i < 6; i++)
{
   string strTextbox = "textbox" + i.ToString();
   TextBox txt = this.FindControl(strTextbox) as TextBox;
   if (txt != null && !string.IsNullOrEmpty(txt.Text))
   {
      // ... 
   }
}

但我会使用更有意义的名字。

  

我只想在一个button_click事件中访问文本框   实际页面。控件位于面板内。

然后我将使用这种LINQ方法:

List<TextBox> filledArticleTBS = txtPanel.Controls.OfType<TextBox>()
    .Where(txt => txt.ID.StartsWith("textbox") && !String.IsNullOrEmpty(txt.Text)) 
    .ToList();

答案 1 :(得分:0)

我确实设法通过一些额外的工作来实现这一点。

因此,最终的代码是首先在我的顶级母版中找到Contentplaceholder,然后在我的嵌套母版页中搜索Contentplaceholder并最终搜索文本框。它可以工作,但是在调试时我可以看到代码中还有其他一些代码,在某些情况下文本框没有找到。我将回到我的工作代码,我可以直接访问所有控件,而不是使用findcontrol。但是,如果有人感兴趣,这对我来说(差不多):

我的顶级母版页(Site.Master)

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="mysite.SiteMaster" %>
<html>
<head>
    // MasterPage head stuff
    // ...
</head>
<body>
<asp:ContentPlaceHolder ID="MainContent" runat="server">
    // My contentpages that use only the top masterpage
    // My contentpage contacts.aspx begin here, This is in a separate file called contacts.aspx. 
    // In code the contentpage is theoretically here, when the site runns it works in another way. Here things are explained; http://odetocode.com/articles/450.aspx
    <%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="contacts.aspx.cs" Inherits="mysite.contacts" %>
    <asp:Content ID="contactsContent" ContentPlaceHolderID="MainContent" runat="server">
        // Here is the content of a contentpage (contacts.aspx) that use the Site.Master
    </asp:Content>
    // contentpage contacts.aspx end here
</asp:ContentPlaceHolder>
</body>
</html>

我的嵌套母版页(XYZ.master)

<%@ Master Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="XYZMasterPage.master.cs" Inherits="mysite.XYZ.XYZMasterPage" %>
<asp:Content ID="NestedMasterPageContentPlaceHolder" ContentPlaceHolderID="MainContent" runat="server">
Nested MasterPage stuff
...
<asp:ContentPlaceHolder ID="NestedMainContent" runat="server">
    // Here is my contentpage where textbox1, 2, 3 etc. is
    // Here is the content of a contentpage (batch.aspx) that use the nested masterpage XYZMasterPage.master
    <%@ Page Title="" Language="C#" MasterPageFile="~/XYZMasterPage.master" AutoEventWireup="true" CodeBehind="batch.aspx.cs" Inherits="mysite.XYZ.batch" %>
    <asp:Content ID="batchInvContent" ContentPlaceHolderID="NestedMainContent" runat="server">
      // Here is the content of a contentpage (batch.aspx)
      <asp:Panel ID="PanelBatch" Runat="Server" >
        <asp:TextBox runat="server" ID="ArticleNr1" />
        <asp:TextBox runat="server" ID="ArticleNr2" />
        <asp:TextBox runat="server" ID="ArticleNr3" />
        <asp:TextBox runat="server" ID="ArticleNr4" />
        <asp:TextBox runat="server" ID="ArticleNr5" />  
        <asp:Button runat="server" ID="buttSubmit" OnClick="buttSubmit_Click" />    
      </asp:Panel>
    </asp:Content>      
    // contentpage batch.aspx end here
</asp:ContentPlaceHolder>

我的batch.aspx的代码隐藏文件

protected void buttSubmit_Click(object sender, EventArgs e)
{
    ContentPlaceHolder parentCP = this.Master.Master.FindControl("MainContent") as ContentPlaceHolder;
    ContentPlaceHolder childCP = parentCP.FindControl("NestedMainContent") as ContentPlaceHolder;

    string strTextbox = string.Empty;                

    for (int i = 1; i < 6; i++)
    {
       strTextbox = "ArticleNr" + i.ToString();
       TextBox txt = childCP.FindControl(strTextbox) as TextBox;
       if (txt != null && !string.IsNullOrEmpty(txt.Text))
       {
          // ... 
          // Insert to db
          // ...
       }
    }
}