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
- textbox2
- textbox3
I want the order of these controls to be changed depending on the user preferences
我希望根据用户首选项更改这些控件的顺序
- textbox3
- textbox2
- textbox1
that is just an example, any idea?
这只是一个例子,任何想法?
6 个解决方案
#1
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
#2
#3
The easiest way is to use third-party controls like Telerik. More precisely DockZones.
Otherwise you'd have to create your own pagebuilder with this functionality.
最简单的方法是使用Telerik之类的第三方控件。更确切地说是DockZones。否则,您必须使用此功能创建自己的页面构建器。
#4
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));
#5
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. In SQL you could run a query such as:
对于一个非常简单的概念验证,您需要某种类型的后备存储,如存储UserID,Ordinal和AscxFileName的数据库表。在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);
}
#6
Grab all the IDs of the textbox controls and store them in an array. Then just do a reverse sort:
获取文本框控件的所有ID并将它们存储在一个数组中。然后只做反向排序:
Array.Reverse( mytextboxArray );
#1
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
#2
The jQuery UI framework can do that for you. Check out this demo and the docs for the same.
jQuery UI框架可以为您做到这一点。查看此演示和相同的文档。
#3
The easiest way is to use third-party controls like Telerik. More precisely DockZones.
Otherwise you'd have to create your own pagebuilder with this functionality.
最简单的方法是使用Telerik之类的第三方控件。更确切地说是DockZones。否则,您必须使用此功能创建自己的页面构建器。
#4
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));
#5
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. In SQL you could run a query such as:
对于一个非常简单的概念验证,您需要某种类型的后备存储,如存储UserID,Ordinal和AscxFileName的数据库表。在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);
}
#6
Grab all the IDs of the textbox controls and store them in an array. Then just do a reverse sort:
获取文本框控件的所有ID并将它们存储在一个数组中。然后只做反向排序:
Array.Reverse( mytextboxArray );