.NET和SQL:我是否需要构建自定义控件?

时间:2014-07-30 18:57:42

标签: html sql .net visual-studio-2013

我正在使用VS 2013中的默认Web窗体模板,其中一行有3个div:

    <div class="col-md-4">
        <h2>Get Paid</h2>
        <img src="Images/adp_logo.gif" class="img-responsive" alt="ADP" />
        <p>
        Everybody wants to get paid! Just click on the "ADP" link to monitor or change your timesheet, benefits
        information, 401K contributions, etc...
        </p>

        <p>
            <a class="btn btn-default" href="http://go.microsoft.com/fwlink/?LinkId=301948">Go! &raquo;</a>
        </p>
    </div>

    <div class="col-md-4">
        <h2>Get Help</h2>
        <asp:ImageButton ID="imgBtn2" runat="server" ImageUrl="~/Images/user_help.png" Height="100" />
        <p>
            Don't know how to do something? We can help! Click the image above to find answers about how to get a badge,
            share your Outlook calendar, or make a great cup of coffee (okay, maybe not the last one, but you get the idea...)
        </p>
        <p>
            <a class="btn btn-default" href="http://go.microsoft.com/fwlink/?LinkId=301949">Go! &raquo;</a>
        </p>
    </div>

    <div class="col-md-4">
        <h2>Find the People You Need</h2>
        <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/Images/find_icon.png" Height="100" />
        <p>
            You can easily find information about anybody that works at ERC. Just click and go!
        </p>
        <p>
            <a class="btn btn-default" href="http://go.microsoft.com/fwlink/?LinkId=301950">Go! &raquo;</a>
        </p>
    </div>

我希望能够使用数据库行中的信息填充这些div。例如,在我的数据库中,我有一个包含标题,图像,描述,链接和类别列的表。如果某一行的类别为“#34;特色&#34;”,则会使用它的标题值,带有图像的标记等填充第一个元素。

我是否需要构建自定义控件才能实现此目标,或者我只是忽略了实现此目标的明显方法?

顺便说一句,Google Play商店就是我之后建模的,如果有帮助的话......

...谢谢

1 个答案:

答案 0 :(得分:0)

就像大卫所说的那样,.NET Repeater控件非常适合你想要实现的目标。您可以添加一次html模板,然后根据您的数据库查询和它将提取的条目数重复它。

HTML。请注意,将数据绑定到'src','href'等属性时,单引号是必需的:

<asp:Repeater ID="myRepeater" runat="server">
    <ItemTemplate>
        <div class="col-md-4">
            <h2><%# Eval("Title") %></h2>
            <img src='<%# Eval("Image") %>' class="img-responsive" alt="ADP" />
            <p>
                <%# Eval("Description") %>
            </p>
            <p>
                <a class="btn btn-default" href='<%# Eval("Link") %>'>Go! &raquo;</a>
            </p>
        </div>
    </ItemTemplate>
</asp:Repeater>

代码隐藏C#:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack) {
        bindRepeater();
    }
}

protected void bindRepeater() {
    DatabaseEntities db = new DatabaseEntities();

    var query = (from q in db.myTableName
                         where q.Category == "Featured"
                         select q);

    if (query.Count() > 0)
    {
        myRepeater.DataSource = query.ToArray();
        myRepeater.DataBind();
    }
}