0

UserControl(.ascx) を持つ単純な Web ページがあります。これが私のUserControlソースです

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ucReportTextBox.ascx.cs"
Inherits="Intranet.UserControl.ucReport" %>
<table width="100%">
<tr>
      <td>
         <asp:TextBox ID="txtQuery" runat="server" Width="250px" />
      </td>
</tr>
</table>

そして私のウェブページのソース

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage/DefaultMasterPage.Master"
AutoEventWireup="true" CodeBehind="ComposeReport.aspx.cs" Inherits="Intranet.MasterPage.WebForm4" %>
<%@ Register Src="~/UserControl/UcReportTextBox.ascx" TagName="UcReportTextBox" TagPrefix="uc1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="contentHead" runat="server"/>
<asp:Content ID="Content2" ContentPlaceHolderID="contentBody" runat="server">
<table>
   <tr>
       <td>
           <uc1:UcReportTextBox ID="ucReporttxt" runat="server" />
       </td>
   </tr>
//includes so many tags
</table>
</asp:Content>

私のウェブページの背後にあるコード

    protected void Page_Load(object sender, EventArgs e)
    {
        InitComponent();
    }

    private void InitComponent()
    {
        XDocument document = XDocument.Load("ReportXML.xml");
        var parameterTypes = document.Descendants("Type");

        foreach (var parType in parameterTypes)
        {
            if (parType.Value == "string") //TODO Enumerate it !!
            {
                ucReporttxt.addTextBox();
            }
        }
    }

ユーザーコントロールのコードビハインド

public void addTextBox() 
    {
        TextBox txtBox = new TextBox();
        txtBox.ID = "txtBox";
        txtBox.Width = 170;
        Page.Form.Controls.Add(txtBox);
    }

あなたが理解しているように、私は asp.net に慣れていません。コードはテキストボックスを正しく追加しますが、テキストボックスはページの最後に追加されます。ページの最後ではない部分にテキストボックスを追加したいのですがucReportText、どうすれば修正できますか?

ご協力いただきありがとうございます。

4

3 に答える 3

0

asp パネルをユーザー コントロールに追加し、テキスト ボックスをページ コントロールではなくパネル コントロールに追加できます。次に、テキストボックスを表示したい場所にパネルを配置します。

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage/DefaultMasterPage.Master"
AutoEventWireup="true" CodeBehind="ComposeReport.aspx.cs" Inherits="Intranet.MasterPage.WebForm4" %>
<%@ Register Src="~/UserControl/UcReportTextBox.ascx" TagName="UcReportTextBox" TagPrefix="uc1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="contentHead" runat="server"/>
<asp:Content ID="Content2" ContentPlaceHolderID="contentBody" runat="server">
<table>
   <tr>
       <td>
           <uc1:UcReportTextBox ID="ucReporttxt" runat="server" />
           <asp:panel runat="server" id="pnlContainer"></asp:panel>
       </td>
   </tr>
//includes so many tags
</table>
</asp:Content>



public void addTextBox() 
    {
        TextBox txtBox = new TextBox();
        txtBox.ID = "txtBox";
        txtBox.Width = 170;
        pnlContainer.Controls.Add(txtBox);
    }
于 2013-06-14T18:37:32.877 に答える
0

コード ビハインドから要素を動的に追加する場所を制御するには、PlaceHolderコントロールをページに追加してそこに追加します。このようなもの:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ucReportTextBox.ascx.cs" Inherits="Intranet.UserControl.ucReport" %>
<table width="100%">
    <tr>
        <td>
            <asp:TextBox ID="txtQuery" runat="server" Width="250px" />
            <asp:PlaceHolder runat="server" ID="phAdditionalTextBoxes" />
        </td>
    </tr>
</table>

(注: 動的テキスト ボックスを追加する場所が完全に明確ではないため、PlaceHolderそれに応じて を移動します。)

そしてコードで:

TextBox txtBox = new TextBox();
txtBox.ID = "txtBox";
txtBox.Width = 170;
phAdditionalTextBoxes.Controls.Add(txtBox);
于 2013-06-14T18:39:13.773 に答える