开发者

How should I go about creating a user control with variable layout?

I have a user control which contains a number of child controls. I want to be able to change the layout of the control (i.e. change the markup surrounding the child controls) via a property of the user control, but I don't want to have to duplicate the child controls themselves. This precludes using, say, a MultiView with different versions of the layout in each view, since in that case I'd have to duplicate the controls. I'd also rather use markup than emit the code for the control directly (as you'd do in a server control).

To give a contrived example, say I have a label and a textbox. In one layout, I want the label and textbox to be contained by divs and laid out on top of each other. In an alternate layout, I want them开发者_开发问答 to be contained side by side in a table.

Should I just go ahead and duplicate the controls between each view? It seems like a violation of the DRY principle, but I'm not sure if I have much choice. A partial solution would be making the views of a multiview into naming containers so that I could at least save myself the time of having to prefix the duplicate control IDs with something to make them unique.

Thanks! Please feel free to ask if the above doesn't make sense.


You could use a templated user control to define header and footer templates for each section of the user control. From there you can use CSS and/or public properties on the user control to style the pre-defined (non-templated) components of the control.


It really sounds like maybe you should go ahead and create a server control instead of just a user control. You'd have to create the markup in code, but it would allow you the best flexibility.


An idea is to create a second user control, that is clone with the first on the design file but have the same code file.

FileLayout1.ascx
FileLayout2.ascx

both of this have
CodeFile="FileLayout1.ascs.cs" Inherits="FileLayout1"

FileLayout1.ascs.cs

You can load the 1 or the 2.

The limitations/problems are that both ascx must have the exactly same control names, and you have the same control 2 times (but the code is only one time)

From my point of view the good think is that you free to design the layout as you like on both way.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜