【问题标题】:Adding textbox to UserControl in asp.net在 asp.net 中将文本框添加到 UserControl
【发布时间】:2013-06-14 18:33:55
【问题描述】:

我有一个包含 UserControl(.ascx) 的简单网页。这是我的 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();
            }
        }
    }

usercontrol 后面的代码

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

如您所知,我是 asp.net 的新手,在 PageLoad 中,我正在读取 XMLfile 以确定是否将文本框添加到页面。代码正确添加了文本框,但文本框添加在页面末尾。我想在ucReportText 的部分而不是页面末尾添加文本框如何解决?

感谢您的帮助。

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    您可以将 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);
        }
    

    【讨论】:

      【解决方案2】:

      为了控制从代码隐藏中动态添加元素的位置,请将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);
      

      【讨论】:

        【解决方案3】:

        文本框被添加到页面的末尾,因为这是Page.Form.Controls.Add(txtBox); 放置它们的位置,您正在将文本框添加到包含您的 UC 的页面的末尾,

        Placeholder 控件添加到您的UC,然后将新文本框添加到占位符。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-01-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-07-18
          相关资源
          最近更新 更多