如何在单个aspx页面中使用列表框和下拉列表,并在aspx页面中访问它

本文关键字:aspx 下拉列表 访问 并在 单个 列表 | 更新日期: 2023-09-27 18:02:31

我想在单个ascx页面中使用多个控件,如下拉列表,列表框,单选按钮列表,而不是使用多个ascx页面,并希望在aspx页面中访问它

如何在单个aspx页面中使用列表框和下拉列表,并在aspx页面中访问它

假设你有一个用户控件MyControl。

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="MyControl.ascx.cs" Inherits="WebApplication1.MyControl" %>
<asp:LinkButton ID="lbtn" runat="server">Link Button</asp:LinkButton>
<asp:RadioButton ID="rbtn" runat="server" Text="Radio Button" />
<asp:DropDownList ID="ddl" runat="server">
    <asp:ListItem Text="Dropdown"></asp:ListItem>
</asp:DropDownList>

现在mycontrol . asx .cs页面代码:为每个控件创建公共属性并设置它们

public DropDownList dropdown {get;设置;}

public RadioButton radioButton { get; set; }
public LinkButton linkbutton { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
    SetCtrlProperties();
}
private void SetCtrlProperties(){
    dropdown = ddl;
    radioButton = rbtn;
    linkbutton = lbtn;
}

现在如果你把它放在一个名为default的页面上。想要隐藏所有按钮的控制从。Aspx页面点击,然后。Aspx页面代码:

<body>
    <form id="form1" runat="server">
    <div>
    </div>
    <uc1:MyControl ID="MyControl1" runat="server" />
    <asp:Button ID="Button1" runat="server" Text="Hide" onclick="Button1_Click" />
    </form>
</body>

和Default.aspx.cs页面代码:

protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            MyControl1.dropdown.Visible = false;
            MyControl1.radioButton.Visible = false;
            MyControl1.linkbutton.Visible = false;
        }

创建一个名为它的用户控件,并添加您想要显示的所有控件,并且在某些页面中是常见的

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="CustomControl.ascx.cs" Inherits="ControlsDemo.CustomControl" %>
<asp:LinkButton ID="lnkBtn" runat="server">Link Button</asp:LinkButton>
<asp:RadioButton ID="radioBtn" runat="server" Text="Radio Button" />
<asp:DropDownList ID="ddl" runat="server"></asp:DropDownList>

现在你需要在aspx页面中声明用户控件

 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ControlsDemo.aspx.cs" Inherits="ControlsDemo.ControlsDemo"%>
    <%@ Register Src="~/CustomControl.ascx" TagName="CControl" TagPrefix="ucCtrl" %>

现在你可以在aspx页面的任何地方使用

<ucCtrl="CControl" runat="server" />