繁体   English   中英

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

[英]How to get Id at Server Side for controls inside PlaceHolder in Asp.Net Web Application?

我正在处理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"));

像这样更新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为“ SchemaEMITenure03MonthTextBox”

您可以通过以下方式在aspx上访问此文本:

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

如果文本框位于其他控件中,则必须使用findcontrol方法。只需使用父控件的ID,然后应用Findcontrol。如果您的控件中有多个父控件,则必须多次使用Findcontrol。

字符串值=(TopParent.FindControl(“ InnerParent”)。FindControl(“ TextBox”)as TextBox).Text;

在此示例中,文本框像这样在两个父对象的内部

TopParent InnerPArent文本框...

希望它将帮助

如果控件是文本框,则可以尝试浏览控件并获取值:

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

我很确定您可以在此代码中做一些改进。 但这是我想到解决您的问题的想法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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