尝试创建自定义ASP.NET控件

时间:2022-09-02 13:34:29

I am trying to make my first custom control in WebForms.

我想在WebForms中创建我的第一个自定义控件。

I defined my class like so: (DeviceRow.ascx.cs)

我这样定义了我的类:(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;
    }
}

I marked it up like this: (DeviceRow.ascx)

我把它标记为:(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>

And I am attempting to use it like this: (Default.aspx)

我试图像这样使用它:(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>

The 'Click Me' button appears on the page, but my DeviceRow panel doesn't. (no errors)

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

What did I miss?

我错过了什么?

2 个解决方案

#1


1  

To use the control, like you mentioned ,

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

Please change the below line

请更改以下行

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

to

public partial class DeviceRow  : System.Web.UI.UserControl

#2


0  

When you create a user control, you are responsible to create properties and maintain states for that control. Here you need a BorderStyle Property for your control. I would do like this:

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

DeviceRow.ascx

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

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;
            }
        }

    }
}

And when I add the usercontrol, it's BorderStyle property is available in markup as well as in the code:

当我添加usercontrol时,它的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;
}

You should go ahead and play with different properties and even see how to manage events for user controls.

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

#1


1  

To use the control, like you mentioned ,

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

Please change the below line

请更改以下行

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

to

public partial class DeviceRow  : System.Web.UI.UserControl

#2


0  

When you create a user control, you are responsible to create properties and maintain states for that control. Here you need a BorderStyle Property for your control. I would do like this:

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

DeviceRow.ascx

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

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;
            }
        }

    }
}

And when I add the usercontrol, it's BorderStyle property is available in markup as well as in the code:

当我添加usercontrol时,它的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;
}

You should go ahead and play with different properties and even see how to manage events for user controls.

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