如何在服务器端获取Asp.Net Web应用程序中PlaceHolder内控件的Id

本文关键字:PlaceHolder 控件 Id 应用程序 Web 服务器端 获取 Net Asp | 更新日期: 2023-09-27 18:19:39

我正在开发asp.net web应用程序。我使用占位符和用户控件在一个页面中动态创建控件。占位符中有多个文本框。

我正在for循环中运行此代码。

phSchemaEMITenureDetails.Controls.Add(LoadControl("~/UserControl/SchemaEMITenureDetails.ascx"));

意思是,我想创建多组这样的文本框。

一切都很好,但现在我想在服务器端的这些文本框中获取用户输入的文本数据。所以我需要服务器端这些控件的Id。

我不明白。请帮我解决

用户控制:

    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="SchemaEMITenureDetails.ascx.cs" Inherits="PaymentControllerGUI.SchemaEMITenureDetails" %>
<asp:Label runat="server" ID="SchemaRowName"></asp:Label>
<asp:Table runat="server">
    <asp:TableRow runat="server" ID="SchemaEMITenure03MonthRow" Style="display: none;">
        <asp:TableCell>
            <asp:Label runat="server" Text="EMI 03 Months" ID="SchemaEMITenure03MonthLabel">
            </asp:Label>
        </asp:TableCell>
        <asp:TableCell>
<asp:TextBox runat="server" ID="SchemaEMITenure03MonthTextBox">
</asp:TextBox>
        </asp:TableCell>
    </asp:TableRow>
    <asp:TableRow runat="server" ID="SchemaEMITenure06MonthRow" Style="display: none;">
        <asp:TableCell>
            <asp:Label runat="server" Text="EMI 06 Months" ID="SchemaEMITenure06MonthLabel">
            </asp:Label>
        </asp:TableCell>
        <asp:TableCell>
            <asp:TextBox ID="SchemaEMITenure06MonthTextBox" runat="server">
            </asp:TextBox>
        </asp:TableCell>
    </asp:TableRow>
    <asp:TableRow runat="server" ID="SchemaEMITenure09MonthRow" Style="display: none;">
        <asp:TableCell>
            <asp:Label runat="server" Text="EMI 09 Months" ID="SchemaEMITenure09MonthLabel">
            </asp:Label>
        </asp:TableCell>
        <asp:TableCell>
            <asp:TextBox ID="SchemaEMITenure09MonthTextBox" runat="server">
            </asp:TextBox>
        </asp:TableCell>
    </asp:TableRow>
    <asp:TableRow runat="server" ID="SchemaEMITenure12MonthRow" Style="display: none;">
        <asp:TableCell>
            <asp:Label runat="server" Text="EMI 12 Months" ID="SchemaEMITenure12MonthLabel">
            </asp:Label>
        </asp:TableCell>
        <asp:TableCell>
            <asp:TextBox ID="SchemaEMITenure12MonthTextBox" runat="server">
            </asp:TextBox>
        </asp:TableCell>
    </asp:TableRow>
</asp:Table>

页码Aspx:

<asp:PlaceHolder ID="phSchemaEMITenureDetails" runat="server"></asp:PlaceHolder>

页面Aspx.cs:phSchemaEMITenureDetails.Controls.Add(LoadControl("~/UserControl/SchemaEMITenureDetails.ascx"));

如何在服务器端获取Asp.Net Web应用程序中PlaceHolder内控件的Id

像这样更新ypur ascx文件(只需为表添加ID)

  <asp:Table runat="server" ID="tbl">
        <asp:TableRow runat="server" ID="SchemaEMITenure03MonthRow" Style="display: none;">
            <asp:TableCell>
                <asp:Label runat="server" Text="EMI 03 Months" ID="SchemaEMITenure03MonthLabel">
                </asp:Label>
            </asp:TableCell>
            <asp:TableCell>
    <asp:TextBox runat="server" ID="SchemaEMITenure03MonthTextBox">
    </asp:TextBox>.... 

现在您的文本框id是"SchemaEMITtenure03MonthTextBox"

您可以在aspx上访问以下文本:

string value = (tbl.FindControl("SchemaEMITenure03MonthTextBox") as TextBox).Text;

如果您的文本框位于其他文本框中,则必须使用findcontrol方法控制只需使用父控件的id,然后应用Findcontrol。如果你的内部有多个父控件,那么你必须像这个一样多次使用Findcontrol

字符串值=(TopParent.FindControl("InnerParent").FindControl"("TextBox")作为TextBox).Text;

在本例中,文本框位于两个类似的父对象内部

TopParentInnerPArent文本框。。。。

希望它能帮助

如果控件是一个文本框,您可以尝试遍历控件并获取值:

System.Text.StringBuilder result = new System.Text.StringBuilder();
protected void cmdOK_Click(object sender, EventArgs e)
{
    this.GetValues(this.phSchemaEMITenureDetails, this.result);
    this.litResult.Text = this.result.ToString();
}
private void GetValues(Control control, System.Text.StringBuilder strBuilder)
{         
    if(control.HasControls())
    {
        foreach (Control item in control.Controls)
        {
            if (item is TextBox)
            {
                TextBox cntrl = (TextBox)item;
                strBuilder.AppendLine(cntrl.ID.ToString() + " = " + cntrl.Text + "<br/>");
            }
            if (item.HasControls())
            {
                this.GetValues(item, strBuilder);
            }
        }  
    }
}

我非常确信你可以在这个代码中做一些改进。但这是我想到的解决你问题的想法。