簡體   English   中英

在ASPX Design頁面上創建控件作為數據表計數

[英]Create controls as the datatable count on ASPX Design page

在此處輸入圖片說明

我想在每個類別的每一行中顯示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();


                   }
            }
        }
    }

這完全是一種錯誤的方法,因為apx頁上將有80個Recod

問題是如何編寫代碼以根據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>
  }

我如何像在MVC Razor上一樣在ASPX Design視圖上編寫相同的內容。 注意:由於服務器不支持MVC,因此請在Webform中進行。

編譯錯誤

綁定數據

     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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM