动态添加用户控件
Adding User Control dynamically
我制作了一个可以在网站上使用的用户控件。我试图通过单击按钮添加此用户控件。我通过将它动态添加到页面来测试它,但没有显示任何内容。
我的 ascx 文件是
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="RoomsUserControl.ascx.cs" Inherits="RoomsUserControl" %>
<table>
<tr>
<td><asp:TextBox ID="txtsize" runat="server"></asp:TextBox></td>
<td>
<asp:DropDownList ID="ddlRoomType" runat="server">
<asp:ListItem>Master Bedroom</asp:ListItem>
<asp:ListItem>Bedroom</asp:ListItem>
<asp:ListItem>Bathroom</asp:ListItem>
<asp:ListItem>Kitchen</asp:ListItem>
<asp:ListItem>Powder Room</asp:ListItem>
<asp:ListItem>Dining Room</asp:ListItem>
<asp:ListItem>Store Room</asp:ListItem>
<asp:ListItem>Walkin Closet</asp:ListItem>
</asp:DropDownList>
</td>
<td>
<asp:DropDownList id="ddlLevel" runat="server">
<asp:ListItem>Basement</asp:ListItem>
<asp:ListItem>Ground Floor</asp:ListItem>
<asp:ListItem>First Floor</asp:ListItem>
</asp:DropDownList>
</td>
<td>
<asp:TextBox ID="txtFlooring" runat="server"></asp:TextBox>
</td>
<td>
<asp:TextBox ID="txtclosetType" runat="server"></asp:TextBox>
</td>
</tr>
</table>
与 RoomsUserControl.ascx.cs 文件
public partial class RoomsUserControl : System.Web.UI.UserControl
{
public string RoomSize
{
get
{
return txtsize.Text;
}
set
{
txtsize.Text = value;
}
}
public string RoomType
{
get
{
return ddlRoomType.SelectedItem.ToString();
}
set
{
ddlRoomType.SelectedItem.Text = value;
}
}
public string RoomLevel
{
get
{
return ddlLevel.SelectedItem.ToString();
}
set
{
ddlLevel.SelectedItem.Text = value;
}
}
public string RoomFlooring
{
get
{
return txtFlooring.Text;
}
set
{
txtFlooring.Text = value;
}
}
public string RoomCloset
{
get
{
return txtclosetType.Text;
}
set
{
txtclosetType.Text = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
}
}
测试页面代码为
protected void Page_Load(object sender, EventArgs e)
{
RoomsUserControl r1 = new RoomsUserControl();
r1.ID = "Room1";
pl1.Controls.Add(r1);
RoomsUserControl r2 = new RoomsUserControl();
r2.ID = "Room2";
pl1.Controls.Add(r2);
RoomsUserControl r3 = new RoomsUserControl();
r3.ID = "Room3";
pl1.Controls.Add(r3);
}
当我调试时,面板 pl1 包含三个用户控件,但它没有显示在表单上。
您需要更改实例化用户的方式control.Here您应该如何做:
protected void Page_Load(object sender, EventArgs e)
{
RoomsUserControl r1 = (RoomsUserControl)Page.LoadControl("~/RoomsUserControl.ascx");
r1.ID = "Room1";
pl1.Controls.Add(r1);
RoomsUserControl r2 = (RoomsUserControl)Page.LoadControl("~/RoomsUserControl.ascx");
r2.ID = "Room2";
pl1.Controls.Add(r2);
RoomsUserControl r3 = (RoomsUserControl)Page.LoadControl("~/RoomsUserControl.ascx");
r3.ID = "Room3";
pl1.Controls.Add(r3);
}
我制作了一个可以在网站上使用的用户控件。我试图通过单击按钮添加此用户控件。我通过将它动态添加到页面来测试它,但没有显示任何内容。
我的 ascx 文件是
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="RoomsUserControl.ascx.cs" Inherits="RoomsUserControl" %>
<table>
<tr>
<td><asp:TextBox ID="txtsize" runat="server"></asp:TextBox></td>
<td>
<asp:DropDownList ID="ddlRoomType" runat="server">
<asp:ListItem>Master Bedroom</asp:ListItem>
<asp:ListItem>Bedroom</asp:ListItem>
<asp:ListItem>Bathroom</asp:ListItem>
<asp:ListItem>Kitchen</asp:ListItem>
<asp:ListItem>Powder Room</asp:ListItem>
<asp:ListItem>Dining Room</asp:ListItem>
<asp:ListItem>Store Room</asp:ListItem>
<asp:ListItem>Walkin Closet</asp:ListItem>
</asp:DropDownList>
</td>
<td>
<asp:DropDownList id="ddlLevel" runat="server">
<asp:ListItem>Basement</asp:ListItem>
<asp:ListItem>Ground Floor</asp:ListItem>
<asp:ListItem>First Floor</asp:ListItem>
</asp:DropDownList>
</td>
<td>
<asp:TextBox ID="txtFlooring" runat="server"></asp:TextBox>
</td>
<td>
<asp:TextBox ID="txtclosetType" runat="server"></asp:TextBox>
</td>
</tr>
</table>
与 RoomsUserControl.ascx.cs 文件
public partial class RoomsUserControl : System.Web.UI.UserControl
{
public string RoomSize
{
get
{
return txtsize.Text;
}
set
{
txtsize.Text = value;
}
}
public string RoomType
{
get
{
return ddlRoomType.SelectedItem.ToString();
}
set
{
ddlRoomType.SelectedItem.Text = value;
}
}
public string RoomLevel
{
get
{
return ddlLevel.SelectedItem.ToString();
}
set
{
ddlLevel.SelectedItem.Text = value;
}
}
public string RoomFlooring
{
get
{
return txtFlooring.Text;
}
set
{
txtFlooring.Text = value;
}
}
public string RoomCloset
{
get
{
return txtclosetType.Text;
}
set
{
txtclosetType.Text = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
}
}
测试页面代码为
protected void Page_Load(object sender, EventArgs e)
{
RoomsUserControl r1 = new RoomsUserControl();
r1.ID = "Room1";
pl1.Controls.Add(r1);
RoomsUserControl r2 = new RoomsUserControl();
r2.ID = "Room2";
pl1.Controls.Add(r2);
RoomsUserControl r3 = new RoomsUserControl();
r3.ID = "Room3";
pl1.Controls.Add(r3);
}
当我调试时,面板 pl1 包含三个用户控件,但它没有显示在表单上。
您需要更改实例化用户的方式control.Here您应该如何做:
protected void Page_Load(object sender, EventArgs e)
{
RoomsUserControl r1 = (RoomsUserControl)Page.LoadControl("~/RoomsUserControl.ascx");
r1.ID = "Room1";
pl1.Controls.Add(r1);
RoomsUserControl r2 = (RoomsUserControl)Page.LoadControl("~/RoomsUserControl.ascx");
r2.ID = "Room2";
pl1.Controls.Add(r2);
RoomsUserControl r3 = (RoomsUserControl)Page.LoadControl("~/RoomsUserControl.ascx");
r3.ID = "Room3";
pl1.Controls.Add(r3);
}