简体   繁体   中英

I need to loop through TextBox to get the text values in asp.net c# using code behind?

<asp:Panel runat="server" ID="pnlDocUpload">
<div class="row">
    <div class="col-md-4">
    <div class="column-pad-bottom column-pad-top">Document Title:</div>
        <div class="column-pad-bottom2"><asp:TextBox runat="server" ID="fuTitle1" CssClass="form-control"/></div>
        <div class="column-pad-bottom2"><asp:TextBox runat="server" ID="fuTitle2" CssClass="form-control"/></div>
        <div class="column-pad-bottom2"><asp:TextBox runat="server" ID="fuTitle3" CssClass="form-control"/></div>
        <div class="column-pad-bottom2"><asp:TextBox runat="server" ID="fuTitle4" CssClass="form-control"/></div>
        <div class="column-pad-bottom2"><asp:TextBox runat="server" ID="fuTitle5" CssClass="form-control"/></div>
        <div class="column-pad-bottom2"><asp:TextBox runat="server" ID="fuTitle6" CssClass="form-control"/></div>
        <div class="column-pad-bottom2"><asp:TextBox runat="server" ID="fuTitle7" CssClass="form-control"/></div>
        <div class="column-pad-bottom2"><asp:TextBox runat="server" ID="fuTitle8" CssClass="form-control"/></div>
        <div class="column-pad-bottom2"><asp:TextBox runat="server" ID="fuTitle9" CssClass="form-control"/></div>
        <div class="column-pad-bottom2"><asp:TextBox runat="server" ID="fuTitle10" CssClass="form-control"/></div>
        <div class="column-pad-bottom2"><asp:TextBox runat="server" ID="fuTitle11" CssClass="form-control"/></div>
        <div class="column-pad-bottom2"><asp:TextBox runat="server" ID="fuTitle12" CssClass="form-control"/></div>
    </div>

<H3> I tried using find controls it is always returning null </H3>



protected void btAddSection_Click(object sender, EventArgs e)
{
    string path = "C:/Users/emahou1/Desktop/testfolder";
    if (cbIsLink.Checked == true)
    {
        if (fuTitleLink.HasFile)
        {
            // it is other section has nothing to do with the 12 input tags
        }
        else
        {
            // get the link path from the text box and save it ??   
        }
    }
    else
    {
        for (int i = 0; i < Request.Files.Count; i++  )
        {
            string text = "";
            for (int t = 1; t <= 5; t++)
            {
                TextBox tb = FindControl("fuTitle" + t.ToString()) as TextBox;
                if (tb != null) text += tb.Text;
            }
            if (Request.Files[i].FileName.ToString() != "")
            {

               // here i will collect the files and the textbox values to submit them to database
            }
        }
    }
}

I have 12 input tabs that I will use to upload files to the server, I am already looping throgh the input controls to get the files, but these text boxes i want to loop through them so I can capture the text entered by the user and use that text to rename each file being uploaded ....

How about using FindControl inside the panel. Something like this

for (int t = 1; t <= 5; t++)
{
    TextBox tb = pnlDocUpload.FindControl("fuTitle" + t.ToString()) as TextBox;
    if (tb != null) text += tb.Text + "<br>";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM