Slint
Slint

Reputation: 355

Looping throu random amount of textboxes and get values

In classic ASP i could do this when looping throu a unknown inputfields:

<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

With this the user could input values to textbox 1, 3, 4 and 5 or maybe only 1 and 2 and i could collect the values inputs in the For loop.

How could i do this in C#?

I cant do this because it dosent like that i add a i in the middle om my textbox.Text;

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

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

I now have a lot of if:s checking every textbox inside the loop but it got to be a easyer way?

Upvotes: 1

Views: 1088

Answers (2)

Slint
Slint

Reputation: 355

I did manage to get this working with some extra lines.

So the final code was to first find Contentplaceholder in my top master, then search for the Contentplaceholder in my nested Masterpage and finaly search for the textbox. It works, but when debugging i can see that there are some other isues with the code where in some cases the textbox is'nt found. I'm going back to my previusly working code where i access all the controls directly and not with findcontrol. But if someone is intrested this worked (almost) for me:

My Top Masterpage (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>

My Nested Masterpage (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>

My codebehindfile for 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
          // ...
       }
    }
}

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460108

You can use FindControl on the NamingContainer of your textboxes.

If they're are on top of the page and not nested in other controls like 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))
   {
      // ... 
   }
}

But i would use more meaningful names instead.


I want access to the textboxes from a button_click event only on the actual page. The controls are inside a panel.

Then i would use this LINQ approach:

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

Upvotes: 2

Related Questions