【问题标题】:Adding DropDownLists to a user control C#将 DropDownLists 添加到用户控件 C#
【发布时间】:2016-06-21 14:20:14
【问题描述】:

我从未在 C# .NET 中使用过用户控件,并且正在从事一个有 3 个下拉列表的项目,并且需要将它们放入用户控件中。我想知道我将如何去做这件事。我将发布我的下拉列表的代码,以及它们当前的外观以及它们应该是什么样的屏幕截图。谢谢。

 public partial class _default2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DropDownList1.DataBind();
                DropDownList1_SelectedIndexChanged(sender, e);
                DropDownList2.DataBind();
                DropDownList2_SelectedIndexChanged(sender, e);
                DropDownList3.DataBind();
            }
        }

        // Drop Down List 1
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            XmlDataSource2.XPath = String.Format("mmsdata/mill[@n='{0}']/mach", DropDownList1.SelectedValue);
        }
        // Drop Down List 2
        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            XmlDataSource3.XPath = String.Format("mmsdata/mill[@n='{0}']/mach[@n='{1}']/srn", DropDownList1.SelectedValue, DropDownList2.SelectedValue);
            BindSensorList();
        }

        protected void BindSensorList()
        {
            //sender.GetType();
            XmlDocument xdoc = XmlDataSource3.GetXmlDocument();
            XmlNodeList nodes = xdoc.SelectNodes(XmlDataSource3.XPath);
            var sensors = new List<Sensor>();
            foreach (XmlNode node in nodes)
            {
                sensors.Add(new Sensor { id = node.Attributes["n"].Value, name = node.InnerText });
            }

            DropDownList3.DataSource = sensors;
            DropDownList3.DataValueField = "id";
            DropDownList3.DataTextField = "name";
            DropDownList3.DataBind();
        }

我希望它们看起来像什么

他们的样子

【问题讨论】:

    标签: c# .net controls


    【解决方案1】:

    将下拉列表添加到用户控件的最简单方法是在项目解决方案中创建一个用户控件,然后将 asp.net webform 代码放入 ui 中,然后您可以用与您相同的方式为其编写代码创建 Web 表单。

    用户控制示例

    <%@ Control Language="C#" ClassName="SampleUserControl" %>
    <h3> <u>User Control</u> </h3>
    <script runat=server>
    </script>
    Drop down 1:    <asp:DropDownList id="ColorList"
                    AutoPostBack="True"
                    OnSelectedIndexChanged="Selection_Change"
                    runat="server">
    
                  <asp:ListItem Selected="True" Value="White"> White </asp:ListItem>
                  <asp:ListItem Value="Silver"> Silver </asp:ListItem>
                  <asp:ListItem Value="DarkGray"> Dark Gray </asp:ListItem>
                  <asp:ListItem Value="Khaki"> Khaki </asp:ListItem>
                  <asp:ListItem Value="DarkKhaki"> Dark Khaki </asp:ListItem>
               </asp:DropDownList>
    <asp:label id="Label1" runat=server/>
    

    您可以在页面上使用用户控件并动态加载其内容,只需在您希望使用它的页面上对其进行初始化并以与加载类/页面相同的方式传递值

    【讨论】:

    • “他们看起来像什么”和“我希望他们看起来像什么”是倒退的,我需要它们是相反的,我修复了图片上的评论。现在是正确的
    • @JordanDarland 不只是样式问题吗?如果您在动态创建下拉控件时将类或 ID 应用于下拉控件,则可以在 css 中随意设置样式。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多