2014-08-28 61 views
0

我正在研究asp.net web應用程序。我使用佔位符和用戶控件在一個頁面中動態創建控件。裏面的佔位符有多個文本框。如何獲取Asp.Net Web應用程序中PlaceHolder中控件的服務器端Id?

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

回答

0

更新這樣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.If你裏面不止一個家長控制,那麼你必須使用的FindControl多時間像這樣

string value =(TopParent.FindControl(「InnerParent」)。FindControl(「TextBox」)as TextBox).Text;

在這個例子中的文本框是兩家母公司這樣

TopParent InnerPArent 文本框的裏面....

希望它會heplp

0

您可以嘗試通過控制去,獲取值如果控件是一個文本框:

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

我很確定你可以做此代碼的一些改進。但這是我想到解決您的問題的一個想法。

相關問題