2016-05-30 42 views
1

在傳統的ASP循環THROU一個未知inputfields當我可以這樣做:循環THROU文本框的隨機量和得到的值

<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#中做到這一點?

我不能這樣做,因爲它的劑量就像我在中間添加一個我om textbox.Text;

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

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

我現在有很多如果:檢查循環內的每個文本框,但它必須是一個easyer方式?

回答

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

也許在if子句中加入'&& txt.Text!= String.Empty'。雖然這當然可以作爲OP的練習... – user1429080

+0

@ user1429080:完成 –

+0

好吧,看起來像它會工作。我試圖實現這一點,但我無法從文本框中獲取值,我想這是由於我嵌套的主頁!..?! 我使用asp:textboxes並試圖使用像這樣的findcontrol: TextBox txt = this.Master.Master.FindControl(strTextbox)as TextBox; txt apears在調試時甚至包含文本。我也試着用這個:TextBox txt = this。FindControl(strTextbox)作爲TextBox;並finaly也試過這個:strTextbox =「MainContent_MainContent_textbox」+ i.ToString();沒有運氣。 :( – Slint

0

我還是設法得到了一些額外的行這方面的工作。

因此,最終的代碼是首先在我的頂級主文件中查找Contentplaceholder,然後在嵌套Masterpage中搜索Contentplaceholder並最終搜索文本框。它的工作原理,但在調試時,我可以看到有一些其他的代碼,在某些情況下,文本框沒有找到。我要回到以前的工作代碼,我直接訪問所有的控件,而不是findcontrol。但是,如果有人intrested這個工作(幾乎)對我來說:

我頂母版(的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> 

我codebehindfile爲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 
      // ... 
     } 
    } 
} 
+0

太複雜且容易出錯;-)如果您想訪問面板中的文本框,則不需要「FindControl」方法從'buttSubmit_Click',只是'PanelBatch.Controls.OfType ()' –