0

enter image description here

I want to display 4 latest records in every row from each category.

I am getting the results but am stuck at the way I am displaying the data.

HTML on ASPX Page

            <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>

Binding data

    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();


                   }
            }
        }
    }

Its totally a wrong way to do it as there will be 80 recods on a aspx page

Question is how do I write a code to get the design as per the count of Datatable and in a foreach loop bind it and as per the count create the images,labels dynamically,in MVC its easy

Like

@{  
<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>
  }

How do i write the same thing on ASPX Design View like we do it on a MVC Razor. Note : As the server doesn't supports MVC am doing it in Webform.

Compilation Error

Dave
  • 263
  • 6
  • 23

1 Answers1

1

Binding data

     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 File will be

<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>
kroky
  • 474
  • 3
  • 11
  • Did it worked,am getting error CS0021: Cannot apply indexing with [] to an expression of type 'object' – Dave Apr 17 '16 at 16:07
  • in which line the exception occur? – kroky Apr 21 '16 at 09:07
  • Line number 45 and all red line on ASPX page <%=item["Identity"].ToString()%>.Have added a screen short – Dave Apr 22 '16 at 02:38
  • How Did you gave me the link i.e http://stackoverflow.com/questions/2325777/why-cant-i-do-foreach-var-item-in-datatable-rows/2325792#2325792 with the #id as it landed exactly on the same area of the page. Other wise the Url is http://stackoverflow.com/questions/2325777/why-cant-i-do-foreach-var-item-in-datatable-rows/ – Dave Apr 23 '16 at 16:05
  • there is share link under the answer if I clicked it the url for the exact place will appear. – kroky Apr 23 '16 at 18:20