Click here to Skip to main content
15,894,313 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i also tried OrderByDescending().ToList() method to fetch all the records, but the problem is when i fetch all the records im not able to bind all the records in div tag so that the div tag get incremented according the records.

Also i want to know how to hide some text of blog description with read more link

Please Help me!

Thanks !

What I have tried:

public string author="";
public string blogTitile = "";
public string blogDescription = "";

using(database db = new database())
          {
              var query = (from c in db.table

                           select new
                           {
                               c.id,
                               c.Author,
                               c.BlogTitle,
                               c.BlogDescription
                           }).OrderByDescending(d => d.id).FirstOrDefault();



              author = query.Author;
              blogTitle = query.BlogTitle;
              blogDescription = query.BlogDescription;


                  divImage ="<div class='col-md-6 nopadding hidden-xs'><img src='../images/blog/1.jpg' alt='News Image'><span class='img-overflow'></span><div class='arrow-left'></div></div>";
                  divDetails ="<p class='date'>"+ author+ "</p>"+
                              "<h2>"+ blogTitle+"</h2>"+
                                "<p> "+blogDescription+"</p>"+
                                 "<a href='artikel' class='btn btn-primary btn-xs'>Read More</a>"+
                                "<span class='comments'>0 "></span>";


this is aspx page:
 <div class="row ">
     <div class="col-xs-12">
          <div class="item item-left">
                    <%= divImage %>

         <div class="col-xs-6 ">
              <div class="content">
                        <%=divDetails %>
              </div>
        </div>
    </div>
  </div>
</div>
</div>
Posted
Updated 4-Oct-17 21:22pm
v2

for read more logic you can search on google. however i am posting answer for this question where you want to loop through records and form the HTML.
Basically in c# code on page_Load method, you will fire query the linq which you have written.
after taking the record you will form the HTML and bind this HTML to a placeholder which is on aspx.
so your aspx code will be
<div id="blogData" runat="server"></div>

and in your pageload you will write below code
var query = (from c in db.table
                            
                            select new
                            {
                                c.id,
                                c.Author,
                                c.BlogTitle,
                                c.BlogDescription
                            }).OrderByDescending(d => d.id)
// got the records. loop through it
System.Text.StringBuilder sb = new System.Text.StringBuilder();
                query.ForEach(x => { sb.Append(string.Format("<div class='row '><div class='col-xs-12'><div class='item item-left'><div class='col-md-6 nopadding hidden-xs'><img src='../images/blog/1.jpg' alt='News Image'><span class='img-overflow'></span><div class='arrow-left'></div></div><div class='col-xs-6 '><div class='content'><p class='date'>{0}</p>'+'<h2>{1}</h2>'+'<p>{2}</p>'+'<a href='artikel' class='btn btn-primary btn-xs'>Read More</a>'+'<span class='comments'>0 '></span></div></div></div></div></div>", x.Author, x.BlogTitle,x.BlogDescription)); });

//sb will have your formed HTML.
// now you will put this html to placeholder div
blogData.InnerHtml = sb.ToString();


note: this is not a tested code you may need to alter it. Best of luck
 
Share this answer
 
Comments
Umair Nafis 4-Oct-17 13:58pm    
Thanx for the reply sir,
I tried your code but im getting error in ForEach()..are you missing a using directive or assembly reference.
Umair Nafis 4-Oct-17 14:14pm    
Thank You so much sir, it works I just used
query.ToList().ForEach(...),,...
Thanks Alott sir.
sachin.vishwa90 4-Oct-17 14:18pm    
lol, i was about to reply the same, but do you know why you have to use .ToList there?
Umair Nafis 4-Oct-17 16:27pm    
Actually i really dnt knw why i used ToList() here as i dnt what should be the best option to retrieve all the data from database. please correct me if i have better option than ToList(). I just want bind all the data..

Also sir i want to know if have any option to display only limited text in blog description with a read more link..hope u understand.
sachin.vishwa90 5-Oct-17 2:38am    
you have used linq statement to select the records from database. so how this linq works is it, whatever you write here in c# code it internally creates a sql statement and when you write .ToList() then it actually fires the sql statement. in earlier case this statement was created but not executed. as soon as you wrote .ToList() it executed the query and brought you data.
for readmore i will post soon.
there is a jquery plugin available for it. http://www.jqueryscript.net/text/jQuery-Plugin-For-Truncating-Text-with-Read-More-Links-jReadMore.html
you have to alter your html text as mentioned in the blog.
the key part is activate your plugin code. for this you will have to write javascript in you aspx page.
JavaScript
<script type='text/javascript'>
function activateReadmore(){
$('.read-more').readMore();
}
<script>

you have to call this function after you bind your html in your c# code
blogData.InnerHtml = sb.ToString();
// after this you will activate your function
Page.ClientScript.RegisterStartupScript(this.GetType(),"CallMyFunction","activateReadmore()",true);

hope that helps
 
Share this answer
 
Comments
Umair Nafis 4-Nov-17 13:49pm    
sir i used freetextbox for inserting blog description into database, now when i bind that data on webpage it display all text. i want to show only limited text from blog description, I used x.blogDescription.ToString().SubString(200) , because of this method it shows html tag as well in the web page so is there any alternate solution of binding limited text of freetextbox/RichtextBox ? Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900