简体   繁体   English

如何在ASP.NET中的运行时对控件进行排序

[英]How to sort controls at runtime in ASP.NET

How to order Controls at runtime in ASP.NET for instance I have three text boxes in my web page in the following order 例如,如何在ASP.NET中在运行时订购控件,我的网页中有三个文本框,其顺序如下

  • textbox1 文字框1
  • textbox2 textbox2
  • textbox3 文字框3

I want the order of these controls to be changed depending on the user preferences 我希望根据用户的喜好更改这些控件的顺序

  • textbox3 文字框3
  • textbox2 textbox2
  • textbox1 文字框1

that is just an example, any idea? 那只是一个例子,有什么主意吗?

Finally I have found a solution and I will explain it in detail 最后,我找到了一个解决方案,我将详细解释它

create a "panel control" and called it "myPanel" and put in this all controls you want to resort 创建一个“面板控件”并将其命名为“ myPanel”,然后将所有要使用的控件放入其中

<asp:Panel ID="myPanel" runat="server">
        <asp:Panel ID="dock1" runat="server">this is first module</asp:Panel>
        <asp:Panel ID="dock2" runat="server">this is second module</asp:Panel>
        <asp:Panel ID="dock3" runat="server">this is third module</asp:Panel>
</asp:Panel>

the output will be the following 输出将是以下

this is first module 这是第一个模块

this is second module 这是第二个模块

this is third module 这是第三个模块

so what should I do to change their order, lets see I have created more three panels to act like a containers for the docks my code will be like this 所以我该怎么做才能更改其顺序,让我看看我创建了更多的三个面板,就像码头的容器一样,我的代码将像这样

<asp:Panel ID="myPanel" runat="server">
        <asp:Panel ID="container1" runat="server"></asp:Panel>
        <asp:Panel ID="container2" runat="server"></asp:Panel>
        <asp:Panel ID="container3" runat="server"></asp:Panel>
        <asp:Panel ID="dock1" runat="server">this is first module</asp:Panel>
        <asp:Panel ID="dock2" runat="server">this is second module</asp:Panel>
        <asp:Panel ID="dock3" runat="server">this is third module</asp:Panel>
</asp:Panel>

in the code behind I have something like this 在后面的代码中,我有这样的东西

this will remove dock1 control from mypanel and put it in container3 这将从mypanel中删除dock1控件并将其放入container3

    Control myCtrl1 = myPanel.FindControl("dock1");
    Control containerCtrl1 = myPanel.FindControl("container3");
    myCtrl1.Visible = true;
    myPanel.Controls.Remove(myCtrl1);
    containerCtrl1 .Controls.Add(myCtrl1);

and you can manage these thing depending on user preferences from a database or a cookies 您可以根据数据库或cookie中的用户偏好来管理这些事情

best regards 最好的祝福

Had a similar issue and resolved by using ".Controls.AddAt" and specifying an index for the order you want it to appear. 发生了类似的问题,并通过使用“ .Controls.AddAt”并为希望它显示的顺序指定了索引来解决。

so to make my chart appear first in the panel i change: 为了使我的图表首先出现在面板中,我进行了更改:

GraphPanel.Controls.Add(GE.GetChart(t13));

to: 至:

GraphPanel.Controls.AddAt(0, (GE.GetChart(t13)));

and your example to order textboxes in order of 3, 2 then 1 becomes: 您的示例按3、2、1的顺序对文本框进行排序:

Panel1.Controls.AddAt(0,(TextBox3));

Panel1.Controls.AddAt(1,(TextBox2));

Panel1.Controls.AddAt(2,(TextBox1));

The jQuery UI framework can do that for you. jQuery UI框架可以为您做到这一点。 Check out this demo and the docs for the same. 查看此演示文档

The easiest way is to use third-party controls like Telerik . 最简单的方法是使用第三方控件(例如Telerik) More precisely DockZones. 更准确地说是DockZones。
Otherwise you'd have to create your own pagebuilder with this functionality. 否则,您将不得不使用此功能创建自己的Pagebuilder。

For a very simple proof-of-concept, you would need some kind of backing store like a database table that stores UserID, Ordinal, and AscxFileName. 对于非常简单的概念验证,您将需要某种后备存储,例如存储UserID,Ordinal和AscxFileName的数据库表。 In SQL you could run a query such as: 在SQL中,您可以运行查询,例如:

SELECT AscxFileName FROM tableControls WHERE UserID=@UserID ORDER BY Ordinal

Then with the result set: 然后设置结果:

while(IDataReader.Read())
{
    Control myControl = Page.LoadControl(IDataReader["AscxFileName"].ToString());
    myPanel.Controls.Add(myControl);
}

Grab all the IDs of the textbox controls and store them in an array. 获取文本框控件的所有ID,并将它们存储在数组中。 Then just do a reverse sort: 然后只需做一个反向排序:

Array.Reverse( mytextboxArray );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM