1

我正在尝试在 WebForms 中制作我的第一个自定义控件。

我这样定义我的类:(DeviceRow.ascx.cs)

public partial class DeviceRow : System.Web.UI.WebControls.Panel
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.BorderStyle = System.Web.UI.WebControls.BorderStyle.Solid;
    }
}

我这样标记它:(DeviceRow.ascx)

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="DeviceRow.ascx.cs" Inherits="VMS_Calc.DeviceRow" %>
<asp:Button ID="Button1" runat="server" Text="X" Width="28px" />

<asp:DropDownList ID="ddl_Type" runat="server"></asp:DropDownList>

<asp:DropDownList ID="ddl_Mfg" runat="server"></asp:DropDownList>

<asp:TextBox ID="tb_FPS" runat="server" Width="40px">15</asp:TextBox>

我正在尝试像这样使用它:(Default.aspx)

<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
    <section style="vertical-align: middle">
        <asp:UpdatePanel ID="UpdatePanel1" runat="server"> 
            <ContentTemplate>
                <vmsc:DeviceRow ID="DeviceRow11" runat="server"> </vmsc:DeviceRow>
                 <br />
                <br />
                 <asp:Button ID="Button1" runat="server"
                 Text="Click Me" OnClick="Button1_Click" />
            </ContentTemplate>
        </asp:UpdatePanel>
    </section>
</asp:Content>

“单击我”按钮出现在页面上,但我的 DeviceRow 面板没有。(没有错误)

我错过了什么?

4

2 回答 2

1

要使用控件,就像您提到的那样,

请更改以下行

public partial class DeviceRow : System.Web.UI.WebControls.Panel

public partial class DeviceRow  : System.Web.UI.UserControl
于 2013-09-03T13:39:45.173 回答
0

创建用户控件时,您负责创建属性​​并维护该控件的状态。在这里,您需要一个 BorderStyle 属性用于您的控件。我会这样做:

DeviceRow.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="DeviceRow.ascx.cs" Inherits="VMS_Calc.DeviceRow" %>

<asp:Panel ID="pnlContainer" runat="server" >
    <asp:Button ID="Button1" runat="server" Text="X" Width="28px" />
    <asp:DropDownList ID="ddl_Type" runat="server"></asp:DropDownList>
    <asp:DropDownList ID="ddl_Mfg" runat="server"></asp:DropDownList>
    <asp:TextBox ID="tb_FPS" runat="server" Width="40px">15</asp:TextBox>
</asp:Panel>

DeviceRow.ascx.cs

using System;
using System.Web.UI.WebControls;

namespace VMS_Calc
{
    public partial class DeviceRow : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Page_PreLoad(object sender, EventArgs e)
        {
            pnlContainer.BorderStyle = this.BorderStyle;

        }

        public BorderStyle BorderStyle 
        {
            get {
                    if (ViewState["MyBorderStyle"] != null)
                    {
                        return(BorderStyle)ViewState["MyBorderStyle"];
                    }
                    return System.Web.UI.WebControls.BorderStyle.None;
                }
            set
            {
                ViewState["MyBorderStyle"] = value;
                pnlContainer.BorderStyle = value;
            }
        }

    }
}

当我添加用户控件时,它的 BorderStyle 属性在标记和代码中都可用:

<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
     <section style="vertical-align: middle">
        <asp:UpdatePanel ID="UpdatePanel1" runat="server"> 
            <ContentTemplate>
                <vmsc:DeviceRow ID="DeviceRow11" BorderStyle="Solid" runat="server"> </vmsc:DeviceRow>
                 <br />
                <br />
                 <asp:Button ID="Button1" runat="server"
                 Text="Click Me" OnClick="Button1_Click" />
            </ContentTemplate>
        </asp:UpdatePanel>
    </section>    
</asp:Content>



protected void Button1_Click(object sender, EventArgs e)
{
    DeviceRow11.BorderStyle = BorderStyle.None;
}

您应该继续使用不同的属性,甚至了解如何管理用户控件的事件。

于 2013-09-03T15:06:53.127 回答