开发者

Alternatives to ASCX User Control without a server-side form?

I've got an ASP.NET 3.5 Web Forms application in which a large chunk of code needs to be duplicated between a few different pages. Sounds like the ideal candidate for a user-control right? Problem is, this cannot be contained within a <form runat="server"> because it contains a client-side form of it's own.

There are no runat=server controls or postbacks or anything that really need that webform - think of it just as a chunk开发者_StackOverflow of HTML with a few basic <% %> tags. I'd just want to set a property on the control when it's loaded, so that it knows what to output. This is purely an exercise to make the code easier to maintain.

Before I resort to using an oldskool <!--#include-->, is there some better way of doing this?


You can still use a normal user control. Just don't rely on viewstate and postbacks and you shouldn't have any problems.


<%=Response.Write(File.ReadAllText(Server.MapPath("~/includes/filename.ext")))%>

Something along those lines, anyway.

Edit: Same functionality as a server side include, but if I'm not mistaken, enabling the SSI syntax requires an IIS change, where as this wouldn't.

Edit 2: I didn't see the note that your include contains asp.net code. This would obviously only work for client side code only. My mistake.


You can have as many form controls as you want but only one can have runat="server".

Some other techniques:

http://webproject.scottgu.com/CSharp/UserControls/UserControls.aspx

http://weblogs.asp.net/scottgu/archive/2005/08/28/423888.aspx


I'd still make it a control. The <% %> stuff could be labels/literals for more flexibility, and as soon as you get done saying there are no postbacks needed, you'll need them. Best to set up the other pages to include it as a control now for easier changes later. Heck - you could even take advantage control-level caching!

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜