English 中文(简体)
需要从数据库生成ASP.NET控件
原标题:Need to generate ASP.NET controls from a database
  • 时间:2011-02-16 12:13:25
  •  标签:
  • c#
  • asp.net

我正在编写一个应用程序,其中页面的控件需要根据SqlServer表中的数据来确定。

For example: I have a PropertyGroups with Properties(Join Table) I then have categories to Propertygroups, the Properties have a values table, blah blah. Not to get too much in to the DB schema, but I need to assign controls to properties.

比如说,属性Color需要是一个列表框,项目集合是由数据库中的数据等预先确定的。。。

What is the most efficient way to render this before the page is loaded? A handler maybe? I am using Master Pages as well.

最佳回答

看起来,使用asp.net mvc(只需定义视图和模型),甚至动态数据

问题回答
  1. Retrieve all data from database.
  2. Construct control tree in memory.
  3. Add this tree to Page s Controls property.

或者,如果你只需要渲染html,你可以使用一些模板引擎,比如NVelocity。

尽管您可以动态加载控件并将它们添加到Page类controls集合中,但在我看来,解决问题的最佳方案是利用某种绑定到模板的控件来控制数据模板。您可以使用要添加到页面中的控件来构建模板,这些控件将根据绑定到的数据进行重复。

Repeater控件的一个简单示例可能如下所示:

<asp:Repeater id="dataRepeater" runat="server">
  <ItemTemplate>
     <asp:ListBox id="myList" runat="server" />
     <asp:TextBox id="myText" runat="server" Text= <%#Eval("MyDataField")%>  />
     <div> Plain Html </div>
  </ItemTemplate>
</asp:Repeater>

您可以使用交替的项目模板。此外,对于分层数据,您可以在Repeater绑定到的Item上进行几次传递。例如,假设我想将ListBox绑定到它自己的集合,我可以绑定Repeater,然后在Items集合上进行另一次传递,使用FindControl方法查找我的ListBox,然后对它自己的DataSource执行操作。

        foreach (var item in dataRepeater.Items) 
        {
            var listBox = item.FindControl("myList") as ListBox;
            listBox.DataSource = moreData;
            listBox.DataTextField = "Text";
            listBox.DataValueField = "Value";
            listBox.DataBind();
        }

Repeater、DataList和其他绑定到模板的控件也支持事件,比如ItemDataBound,您也可以使用它们来操作模板。如果您希望根据数据库中的内容控制每个模板项中的内容,则可以执行诸如切换模板中已有控件的可见性之类的操作,或者插入子控件。

如上所述,您可以使用其他一些模板引擎,但其概念相当相似。这个想法是允许您的模板驱动所呈现的HTML,而不是尝试自己操作和管理控制树。

如果在.aspx文件中添加占位符控件,则有点像。。。

<body>
    <form id="form1" runat="server">
    <div>
        <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
    </div>
    </form>
</body>

…然后您将能够在内存中创建控件,并将它们添加到占位符的控件集合中,一个简单的示例可能是。。。

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.PlaceHolder1.Controls.Add(new Button(){
            Text = "Added"}
        );

    }
}

…尽管这只是添加了一个按钮,但没有理由不能添加任何类型的控件,只从数据库中检索其值。

丹。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签