动态添加用户控件

时间:2016-05-29 04:48:42

标签: c# asp.net user-controls dynamic-controls asp.net-controls

我已经在网站上使用了usercontrol。我试图通过单击按钮添加此用户控件。我通过动态添加到页面来测试它,但没有显示任何内容。

我的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包含三个用户控件,但它没有在表单上显示。

1 个答案:

答案 0 :(得分:0)

您需要更改实例化用户控件的方式。以下是您应该如何操作的:

while (GetMessage(&msg, NULL, 0, 0))
{
    TranslateMessage(&msg);

    int id = (int)GetProp(msg.hwnd, "PROP_ID");
    int translatedMsg = translateMessage(id, msg.message);

    // but with this technique I miss WM_CREATE, WM_DESTROY, WM_CLOSE
    evtCallbacks[translatedMsg](hinstance, msg.hwnd, msg.wParam, msg.lParam);

    DispatchMessage(&msg);
}