我需要循环遍历TextBox以使用代码隐藏在asp.net c#中获取文本值?

时间:2017-12-05 17:52:26

标签: c# asp.net

<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
            }
        }
    }
}

我有12个输入选项卡,我将用于将文件上传到服务器,我已经循环输入控件来获取文件,但这些文本框我想循环遍历它们所以我可以捕获用户输入的文本,并使用该文本重命名正在上传的每个文件....

1 个答案:

答案 0 :(得分:2)

如何在面板中使用FindControl。像这样的东西

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