The Default.aspx page looks like this
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master"
AutoEventWireup="true" CodeBehind="Default.aspx.cs"
Inherits="ReadMoreReadLess_GridView._Default" %> <asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent"> </asp:Content> <asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent"> <center> <asp:GridView ID="GridView1" runat="server" Width="70%" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound"> <Columns> <asp:TemplateField HeaderText="Sno" ControlStyle-Width="10px"> <ItemTemplate> <asp:Label ID="lblSno" Text='<%# Eval("Sno") %>'
runat="server"></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Article Name" ControlStyle-Width="100px"> <ItemTemplate> <asp:Label ID="lblName" Text='<%# Eval("Article Name") %>'
runat="server"></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Description"> <ItemTemplate> <asp:Label ID="lblDesc" runat="server"
Text='<%# Eval("Description").ToString().Length <= 50 ? Eval("Description") :
Eval("Description").ToString().Substring(0, 50) %>' ToolTip='<%# Eval("Description") %>'></asp:Label>
<asp:LinkButton ID="btnreadMore" runat="server"
Text="Read More" Visible='<%# SetVisibility(Eval("Description"), 50) %>' OnClick="btnreadMore_Click" CssClass="NoUnderLine"></asp:LinkButton>
<asp:Label ID="lblExtra" runat="server"
ToolTip='<%# Eval("Description").ToString().Length <= 50 ? Eval("Description") :
Eval("Description").ToString().Substring(0, 50) %>' Text='<%# Eval("Description") %>' Visible="false"></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Price" ControlStyle-Width="100px"> <ItemTemplate> <asp:Label ID="lblPrice" Text='<%# Eval("Price") %>'
runat="server"></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </center> </asp:Content>
and CodeBehind page looks like below
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Text; namespace ReadMoreReadLess_GridView { public partial class _Default : System.Web.UI.Page { DataRow dr; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { DataTableGrid(); } } private void DataTableGrid() { StringBuilder sb1 = new StringBuilder(); sb1.Append("The following articles on motivation are part of articles I wrote for my weekly motivational "); sb1.Append("column in the Post Newspaper of Zambia."); sb1.Append("\n It is proving a challenge to write an article every week, but one which I am enjoying a lot."); sb1.Append(" After all, if you are too comfortable and not being challenged then something is wrong."); sb1.Append(" At times you need to get out of your comfort zone and do something that requires more from you."); sb1.Append("\n That is the only way we grow and growth is not an option for dreamers - it's a necessity."); sb1.Append("I have received a lot of positive feedback on these motivation articles and most of it has been exteremely encouraging and positive."); sb1.Append("I am sure they will be a great help to you as you reach out to your dreams. "); StringBuilder sb2 = new StringBuilder(); sb2.Append("I have received a lot of positive feedback on these motivation articles and most of it has been exteremely encouraging and positive."); sb2.Append("I am sure they will be a great help to you as you reach out to your dreams. "); sb2.Append("The following articles on motivation are part of articles I wrote for my weekly motivational "); sb2.Append("column in the Post Newspaper of Zambia."); sb2.Append("\n It is proving a challenge to write an article every week, but one which I am enjoying a lot."); sb2.Append(" After all, if you are too comfortable and not being challenged then something is wrong."); StringBuilder sb3 = new StringBuilder(); sb3.Append(" At times you need to get out of your comfort zone and do something that requires more from you."); sb3.Append("\n That is the only way we grow and growth is not an option for dreamers - it's a necessity."); sb3.Append("I have received a lot of positive feedback on these motivation articles and most of it has been exteremely encouraging and positive."); sb3.Append("I am sure they will be a great help to you as you reach out to your dreams. "); sb3.Append("\nThe following articles on motivation are part of articles I wrote for my weekly motivational "); sb3.Append("column in the Post Newspaper of Zambia."); sb3.Append("\n It is proving a challenge to write an article every week, but one which I am enjoying a lot."); sb3.Append(" After all, if you are too comfortable and not being challenged then something is wrong."); StringBuilder sb4 = new StringBuilder(); sb4.Append("\n It is proving a challenge to write an article every week, but one which I am enjoying a lot."); sb4.Append(" After all, if you are too comfortable and not being challenged then something is wrong."); sb4.Append(" At times you need to get out of your comfort zone and do something that requires more from you."); sb4.Append("\n That is the only way we grow and growth is not an option for dreamers - it's a necessity."); sb4.Append("I have received a lot of positive feedback on these motivation articles and most of it has been exteremely encouraging and positive."); sb4.Append("I am sure they will be a great help to you as you reach out to your dreams. "); DataTable dt = new DataTable(); dt.Columns.Add("Sno"); dt.Columns.Add("Article Name"); dt.Columns.Add("Description"); dt.Columns.Add("Price"); for (int i = 0; i < 4; i++) { dr = dt.NewRow(); dr["Sno"] = i + 1; dr["Article Name"] = "Article " + (i + 1); if (i == 0) dr["Description"] = sb1.ToString(); if (i == 1) dr["Description"] = sb2.ToString(); if (i == 2) dr["Description"] = sb3.ToString(); if (i == 3) dr["Description"] = sb4.ToString(); dr["Price"] = "Rs. 2000" + (i + 1) + "/-"; dt.Rows.Add(dr); } GridView1.DataSource = dt; GridView1.DataBind(); } protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { try { if (e.Row.RowType == DataControlRowType.DataRow) { string ID = ((Label)e.Row.FindControl("lblSno")).Text; int length = ((Label)e.Row.FindControl("lblDesc")).Text.Length; if (length == 50) ((Label)e.Row.FindControl("lblDesc")).Text += "..."; ((Label)e.Row.FindControl("lblExtra")).ToolTip += "..."; } } catch (Exception ex) { string ErrMsg = ex.Message; } } protected bool SetVisibility(object Desc, int length) { return Desc.ToString().Length > length; } protected void btnreadMore_Click(object sender, EventArgs e) { try { LinkButton button = (LinkButton)sender; GridViewRow row = button.NamingContainer as GridViewRow; Label descLabel = row.FindControl("lblDesc") as Label; button.Text = (button.Text == "Read More") ? "Hide" : "Read More"; //string temp = descLabel.Text; if (button.Text == "Hide") { Label ExtraLabel1 = row.FindControl("lblExtra") as Label; descLabel.Text = ExtraLabel1.Text; descLabel.ToolTip = ExtraLabel1.ToolTip; } else { Label ExtraLabel = row.FindControl("lblExtra") as Label; descLabel.Text = ExtraLabel.ToolTip; descLabel.ToolTip = ExtraLabel.Text; } } catch (Exception ex) { string ErrMsg = ex.Message; } } } }
Download From HERE
No comments:
Post a Comment