在ASPX Design页面上创建控件作为数据表计数

时间:2016-04-17 15:04:48

标签: c# asp.net .net asp.net-mvc-4 razor

enter image description here

我想在每个类别的每一行显示4条最新记录。

我收到了结果,但我仍然坚持我显示数据的方式。

ASPX页面上的HTML

            <div class="row">
                <!--Start OF Live Feeds-->
                <div class="col-xs-6 col-sm-3 mix livefeeds">
                    <div class="work-item">
                        <div class="image-holder">
                            <a href="images/gallery/1.jpg" id="hrefLiveFeedsOne" runat="server" onserverclick="LiveFeedsOneEvent">
                                <img src="images/gallery/1.jpg" id="imgLiveFeedsOne" runat="server" />
                            <h4>
                     <asp:Label ID="lblLiveFeedsOne" runat="server" Text="Don’t quit your job if you work in"></asp:Label></h4>
                            </a>
                        </div>
                    </div> 
                </div>

                <div class="col-xs-6 col-sm-3 mix livefeeds">
                    <div class="work-item">
                        <div class="image-holder">
                            <a href="images/gallery/1.jpg" id="hrefLiveFeedsTwo" runat="server" onserverclick="LiveFeedsTwoEvent">
                                <img src="images/gallery/1.jpg" id="imgLiveFeedsTwo" runat="server" />
                                <h4>
                                    <asp:Label ID="lblLiveFeedsTwo" runat="server" Text="Live Feeds Two: Don’t quit your job if you work in 1 of these 5 industries"></asp:Label></h4>
                            </a>
                        </div>

                    </div>
                </div>

                <div class="col-xs-6 col-sm-3 mix livefeeds">
                    <div class="work-item">
                        <div class="image-holder">
                            <a href="images/gallery/1.jpg" id="hrefLiveFeedsThree" runat="server" onserverclick="LiveFeedsThreeEvent">
                                <img src="images/gallery/1.jpg" alt="" id="imgLiveFeedsThree" runat="server" />
                                <h4>
                                    <asp:Label ID="lblLiveFeedsThree" runat="server" Text="Live Feeds Three: Don’t quit your job if you work in 1 of these 5 industries"></asp:Label></h4>
                            </a>
                        </div>
                    </div>
                </div>

                <div class="col-xs-6 col-sm-3 mix livefeeds">
                    <div class="work-item">
                        <div class="image-holder">
                            <a href="images/gallery/1.jpg" id="hrefLiveFeedsFour" runat="server" onserverclick="LiveFeedsFourEvent">
                                <img src="images/gallery/1.jpg" alt="" id="imgLiveFeedsFour" runat="server" />
                                <h4>
                                    <asp:Label ID="lblLiveFeedsFour" runat="server" Text="Live Feeds Four: Don’t quit your job if you work in 1 of these 5 industries"></asp:Label></h4>
                            </a>
                        </div>
                    </div>
                </div>

                </div>

绑定数据

    private void BindData()
     {

        using (SqlConnection con = new SqlConnection(cn))
        {
            using (SqlCommand cmd = new SqlCommand("usp_NewsByCategories"))
            {

                using (SqlDataAdapter sda = new SqlDataAdapter())
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Connection = con;
                    sda.SelectCommand = cmd;

                    sda.Fill(dt);
                    ViewState["data"] = dt;                

                    imgLiveFeedsOne.Src = dt.Rows[55]         
                    imgPromotionFour.Src = dt.Rows[4]["ImagePath"].ToString();
                    hrefPromotionFour.HRef = dt.Rows[4]["Identity"].ToString();
                    lblPromotionFour.Text = dt.Rows[4]["Headline"].ToString();

                    imgPromotionFive.Src = dt.Rows[3]["ImagePath"].ToString();
                    hrefPromotionFive.HRef = dt.Rows[3]["Identity"].ToString();
                    lblPromotionFive.Text = dt.Rows[3]["Headline"].ToString();

                    imgPromotionSix.Src = dt.Rows[2]["ImagePath"].ToString();
                    hrefPromotionSix.HRef = dt.Rows[2]["Identity"].ToString();
                    lblPromotionSix.Text = dt.Rows[2]["Headline"].ToString();

                    imgPromotionSeven.Src = dt.Rows[1]["ImagePath"].ToString();
                    hrefPromotionSeven.HRef = dt.Rows[1]["Identity"].ToString();
                    lblPromotionSeven.Text = dt.Rows[1]["Headline"].ToString();

                    imgPromotionEight.Src = dt.Rows[0]["ImagePath"].ToString();
                    hrefPromotionEight.HRef = dt.Rows[0]["Identity"].ToString();
                    lblPromotionEight.Text = dt.Rows[0]["Headline"].ToString();


                   }
            }
        }
    }

这完全是错误的方式,因为在aspx页面上会有80个recods

问题是如何根据Datatable的数量编写代码来获取设计,并在foreach循环中绑定它并根据计数创建图像,动态标签,在MVC中轻松

喜欢

@{  
<div class="row">
@foreach (var item in Model)
{
 <div class="col-xs-6 col-sm-3 mix livefeeds">
                    <div class="work-item">
                        <div class="image-holder">
                            <a href="images/gallery/1.jpg" id="hrefLiveFeedsFive">
                                <img src="@item.imageID" alt=""/>
                                <h4>
                                    <span>@Html.Raw(@item.Title)</span>
                            </a>
                        </div>

                    </div>
                </div>

}
    </div>
  }

我如何在ASPX Design View上编写相同的东西,就像我们在MVC Razor上一样。 注意:由于服务器不支持MVC在Webform中执行此操作。

Compilation Error

1 个答案:

答案 0 :(得分:1)

绑定数据

     protected DataTable dt = new DataTable()
     private void BindData()
     {

        using (SqlConnection con = new SqlConnection(cn))
        {
            using (SqlCommand cmd = new SqlCommand("usp_NewsByCategories"))
            {

                using (SqlDataAdapter sda = new SqlDataAdapter())
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Connection = con;
                    sda.SelectCommand = cmd;

                    sda.Fill(dt);
                   }
            }
        }
    }

ASPX文件将

<div class="row">

<% foreach (DataRow item in dt.Rows)
    {
%>
     <div class="col-xs-6 col-sm-3 mix livefeeds">
                        <div class="work-item">
                            <div class="image-holder">
                                <a href="<%=item["Identity"].ToString()%>">
                                    <img src="<%=item["ImagePath"].ToString()%>" alt=""/>
                                    <h4>
                                        <span><%=item["Headline"].ToString()%></span>
                                </a>
                            </div>

                        </div>
             </div>
<%
}
%>
</div>