你如何移动ASP.Net控件在运行时在Web表单上不同的地方? [英] How can you move ASP.Net controls to different places on the Web form at runtime?

查看:99
本文介绍了你如何移动ASP.Net控件在运行时在Web表单上不同的地方?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有动控制一个公认的方法。

Is there an accepted way to "move" a control.

我的客户希望放置标记(再presenting一些视觉元素)的某块中的页面在几个不同的地方之一。该位置是我不能沿着对CSS的变化(由浮动,或某物)的点不同。

My client wants to place a certain chunk of markup (representing some visual element) in one of several different places on the page. The locations are different to the point that I can't effect the change on CSS along (by floating it or something).

我认为只是把在可见集多点控制假,则显示一个在他们想对特定页面的地方。

I considered just putting the control in multiple spots with Visible set to "false," then displaying the one in the place they wanted for that particular page.

不过,code这个控制并不是微不足道的 - 有一对夫妇的模板部分,例如。有在多个地方欺骗,这将得到笨拙。另外,我不希望有这种严格的控制从code-工作背后出于同样的原因。

However, the code for this control is not trivial -- there's a couple template sections, for instance. Having to dupe this in multiple places would get unwieldy. Also, I don't want to have to work with this control strictly from the code-behind for the same reason.

所以,我希望把它放在一个地方,在Web表单上,它周围的基础上,我想它的举动。我可以把占位符在不同的地方,都控制在一个地方,然后取出并将其添加到正确的位置?我怀疑这是可行的。

So, I'd like to put it in one place on the Web form, the move it around based on where I want it. Could I put Placeholders in different spots, have the control in one spot, then remove and add it to the right spot? I suspect this would work.

有人是否有一个更好的主意吗?是否有一个最佳实践呢?

Does someone have a better idea? Is there a best practice for this?

推荐答案

我推荐使用的占位符的控制,移动你的标记到一个单独的用户控件,然后在运行时加载这个并将其添加到相关的占位符。

I'd recommend using a placeholder control, moving your markup into a separate user control, then loading this at runtime and adding it to the relevant placeholder.

例如:

// Load a user control
MyControl userCtrl = (MyControl) LoadControl("~/Controls/MyControl.ascx");

// Or create an instance of your control
SubclassedControl subclassedCtrl = new SubclassedControl();

// Do stuff with controls here
userCtrl.LoadData();
subclassedCtrl.Text = "Hello World";

// Check which placeholder to add controls to
PlaceHolder placeHolder = (foo=="bar") ? placeHolder1 : placeHolder2;

// Add the controls
placeHolder.Controls.Add(userCtrl);
placeHolder.Controls.Add(subclassedCtrl);

这将避免塞满了不必要的标记您的网页,并在运行时加载它也将避免不必要的混乱后,当其他开发人员着眼于code并不能立即看到为什么控制是在同一个地方标记,但呈现在页面的一个完全不同的部分。

This will avoid cluttering up your page with unnecessary markup, and loading it at runtime will also avoid unnecessary confusion later, when another developer looks at the code and can't immediately see why a control is in one place in the markup, but renders on a completely different part of the page.

这篇关于你如何移动ASP.Net控件在运行时在Web表单上不同的地方?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆