Indresh says:
Hi , firoz1986
Please share full code with html.
Below is full code:
html
<div>
<asp:ListView ID="Listv1" runat="server" GroupPlaceholderID="groupPlaceHolder1"
ItemPlaceholderID="itemPlaceHolder1">
<LayoutTemplate>
<div class="news_container">
<asp:PlaceHolder runat="server" ID="groupPlaceHolder1"></asp:PlaceHolder>
<asp:DataPager ID="DataPager1" runat="server" PagedControlID="Listv1" PageSize="6"></asp:DataPager>
</div>
</LayoutTemplate>
<GroupTemplate>
<asp:PlaceHolder runat="server" ID="itemPlaceHolder1"></asp:PlaceHolder>
</GroupTemplate>
<ItemTemplate>
<div> <h2> <%--<a href="Details.aspx?id=<%# Eval("Id")%>"><%# Eval("Title")%> </a>--%>
<asp:HyperLink ID="HyperLink1" runat="server" href='<%# GetRouteUrl("RouteForCat", new {id = Eval("id"), Title= GetTitle(Eval("Title"))})%>'
Text='<%# Eval("Title") %>'>
</asp:HyperLink>
</h2></div>
<div class="news_tag">
<label class=" margin_right_10"> <label class="posted_date pull-left"> </label>
<asp:Label CssClass="pull-left" ID="date" Text='<%# Eval("Posted", "{0:dd, MMM yyyy}") %>' runat="server" /> </label>
<label class=" margin_right_10"> <label class="post_comments pull-left"> </label> <label class="pull-left"> <%# Eval("cnt")%></label> </label>
<label class=" margin_right_10"><label class="posted_under pull-left"></label><label class="pull-left"> <%# Eval("Category")%>, <%# Eval("Tag")%>, <%# Eval("Tag2")%>, <%# Eval("Tag3")%> </label> </label>
<div class="clearfix"> </div>
</div>
<div> <img class="" src='<%# Eval("Image") %>'></div>
<div class="news_content">
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
<%# Eval("Description").ToString().PadRight(186).Substring(0, 186).TrimEnd()%>... </div>
<div class="readfullstory"> <a href="Details.aspx?id=<%# Eval("Id")%>">Read full story ❯</a> </div>
</ItemTemplate>
</asp:ListView>
</div>
C#
protected string GetTitle(object obj)
{
return obj.ToString().Replace(' ', '-');
}
//binding list view
private void BindListView()
{
string constr = ConfigurationManager.ConnectionStrings["ConnectionString2"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand())
{
//cmd.CommandText = "select * from entry e left join category c on c.id = e.catid";
cmd.CommandText = "select image,title,et.id,description,catid,posted,rt.category,tag,tag2,tag3,rt.id,count(blogid) as cnt from Entry et left join comment c on c.blogid = et.id RIGHT OUTER JOIN category rt on rt.id =catid group by image,c.blogid,title,description,catid,posted,et.id,rt.category,tag,tag2,tag3,rt.id";
cmd.Connection = con;
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
sda.Fill(dt);
Listv1.DataSource = dt;
Listv1.DataBind();
}
}
}
}
Global file
<%@ Application Language="C#" %>
<%@ Import Namespace="System.Web.Routing" %>
<script runat="server">
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
RegisterCustomRoutes(RouteTable.Routes);
}
void RegisterCustomRoutes(RouteCollection routes) {
routes.MapPageRoute("RouteForCat", "{id}/Article/{title}", "~/Details.aspx");
}
void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown
}
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
}
void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
}
void Session_End(object sender, EventArgs e)
{
// Code that runs when a session ends.
// Note: The Session_End event is raised only when the sessionstate mode
// is set to InProc in the Web.config file. If session mode is set to StateServer
// or SQLServer, the event is not raised.
}
</script>
Details.aspx
protected void Page_Load(object sender, EventArgs e)
{
BindRepeaterData(); //binding repeater control for comments
this.BindListView(); //binding list view for news details
if (Page.RouteData.Values["id"].ToString() != null)
{
string strId = Page.RouteData.Values["id"].ToString();
//passing id for blog_id
Blog_id.Value = Page.RouteData.Values["id"].ToString();
}
}
//binding list view for news details
private void BindListView()
{
string constr = ConfigurationManager.ConnectionStrings["ConnectionString2"].ConnectionString;
//string id = Request.QueryString["id"].ToString();
string id = Page.RouteData.Values["id"].ToString();
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand())
{
//SqlCommand cmd = new SqlCommand("select image,title,description,et.id,posted,rt.category,tag,tag2,tag3,rt.id, count(blogid) as cnt from Entry et left join comment c on c.blogid = et.id RIGHT OUTER JOIN category rt on rt.id =catid where et.id =" + Request.QueryString["id"] + " group by image,title,description,et.id,posted,rt.category,tag,tag2,tag3,rt.id ", con);
cmd.CommandText = "select image,title,description,et.id,posted,rt.category,tag,tag2,tag3,rt.id, count(blogid) as cnt from Entry et left join comment c on c.blogid = et.id RIGHT OUTER JOIN category rt on rt.id =catid where et.id =" + id + "group by image,title,description,et.id,posted,rt.category,tag,tag2,tag3,rt.id";
cmd.Connection = con;
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
sda.Fill(dt);
Listv1.DataSource = dt;
Listv1.DataBind();
}
}
}
}
Html
<asp:ListView ID="Listv1" runat="server" GroupPlaceholderID="groupPlaceHolder1"
ItemPlaceholderID="itemPlaceHolder1">
<LayoutTemplate>
<div class="news_container">
<asp:PlaceHolder runat="server" ID="groupPlaceHolder1"></asp:PlaceHolder>
<asp:DataPager ID="DataPager1" runat="server" PagedControlID="Listv1" PageSize="6"> </asp:DataPager>
</div>
</LayoutTemplate>
<GroupTemplate>
<asp:PlaceHolder runat="server" ID="itemPlaceHolder1"></asp:PlaceHolder>
</GroupTemplate>
<ItemTemplate>
<div> <h2> <%# Eval("Title")%></h2></div>
<div class="news_tag">
<label class=" margin_right_10"> <label class="posted_date pull-left"> </label><asp:Label CssClass="pull-left" ID="date" Text='<%# Eval("Posted", "{0:dd, MMM yyyy}") %>' runat="server" /> </label>
<label class=" margin_right_10"> <label class="post_comments pull-left"> </label> <label class="pull-left"> <%# Eval("cnt")%></label> </label>
<label class=" margin_right_10"><label class="posted_under pull-left"></label><label class="pull-left"> <%# Eval("Category")%>, <%# Eval("Tag")%>, <%# Eval("Tag2")%>, <%# Eval("Tag3")%> </label> </label>
<div class="clearfix"> </div>
</div>
<div> <img class="" src='<%# Eval("Image") %>'></div>
<div class="news_content"> <%# Eval("Description") %> </div>
</ItemTemplate>
</asp:ListView>