我有一个简单的网页,它有一个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();
}
}
}
在用户控制后面的代码中
public void addTextBox()
{
TextBox txtBox = new TextBox();
txtBox.ID = "txtBox";
txtBox.Width = 170;
Page.Form.Controls.Add(txtBox);
}
正如您所理解的,我是asp.net的新成员,在PageLoad中,我正在阅读XMLfile是否将文本框添加到页面中。代码正确地添加文本框,但文本框将添加到页面的末尾。我想在ucReportText
的部分添加文本框,而不是页面的末尾,怎么才能修复呢?
谢谢你的帮助。
发布于 2013-06-14 10:37:32
可以将asp面板添加到用户控件中,并将textbox添加到面板控件,而不是页控件。然后,只需将面板放在您希望文本框出现的任何位置即可。
<%@ 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-14 10:39:13
为了控制从代码背后动态添加元素的位置,在页面中添加一个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-14 10:46:16
文本框被添加到页面的末尾,因为这是Page.Form.Controls.Add(txtBox);
放置它们的地方,your正在将文本框添加到保持UC的页面末尾,
向UC添加一个Placeholder
控件,然后将新的文本框添加到占位符中。
https://stackoverflow.com/questions/17115126
复制