C# 在Asp.NETWeb应用程序中,如何在服务器端获取占位符内控件的Id?
我正在开发asp.net web应用程序。我使用占位符ans用户控件在一个页面中动态创建控件。占位符中有多个文本框 我在for循环中运行这段代码C# 在Asp.NETWeb应用程序中,如何在服务器端获取占位符内控件的Id?,c#,asp.net,user-controls,placeholder,C#,Asp.net,User Controls,Placeholder,我正在开发asp.net web应用程序。我使用占位符ans用户控件在一个页面中动态创建控件。占位符中有多个文本框 我在for循环中运行这段代码 phSchemaEMITenureDetails.Controls.Add(LoadControl("~/UserControl/SchemaEMITenureDetails.ascx")); 也就是说,我想创建多组这样的文本框 一切正常,但现在我想获取用户在服务器端的这些文本框中输入的文本数据。所以我需要服务器端这些控件的Id 我没办法弄到。请帮帮
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>
Page Aspx.cs:phSchemaEMITenureDetails.Controls.AddLoadControl~/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>....
如果文本框位于其他文本框中,则必须使用findcontrol方法
控制
只需使用父控件的id,然后应用Findcontrol。如果您的内部有多个父控件,则必须像这样多次使用Findcontrol
字符串值=TopParent.FindControlInnerParent.FindControlTextBox作为TextBox.Text
在本例中,文本框位于两个父对象的内部,如下所示
至高无上的
内亲
文本框
如果控件是文本框,请尝试查看控件并获取值:
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);
}
}
}
}
我很确定您可以在这段代码中做一些改进。但这是我想到的解决你问题的办法
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);
}
}
}
}