How to Restrict the content for particular Length [Resolved]

Posted by Dharanidhar under ASP.NET on 9/11/2012 | Points: 10 | Views : 2817 | Status : [Member] | Replies : 2
How to restrict the content for particular length which is stored in database and then create "Read More.." link..So that On clicking on that link total content should be visible.We can see this in many websites.
For Example,I have 10 lines of content.I want to be visible only 3 lines of code and a "Read More.." link.on clicking that 10 lines of content should be visible.This can be done using sql query or IS there any mechanism to do it..

Hoping the Responses from Anyone for this.....

.NET Trainee


Posted by: Vasanthmvp on: 9/21/2012 [Member] Starter | Points: 50


Hello Dharanidhar,
Here is the explanation for your question: Here we have two cases: Suppose if you want to display Description of a book. (Description column of a table)
Case 1: Displaying a substring along with "Read More ..." link where read more link redirects you to another page, at which the whole description is rendered.

To get this, Directly after binding data to a gridview.. modify the text on the gridview displayed as substring with Read More link is appeared.

DataTable table = new DataTable();

grid.DataSource = table; // grid is my asp:gridview id. Normal way of getting table & assigning to the gridview.
int i = 0;
foreach (DataRow row in table.Rows)
string x = row["Description"].ToString();
if (x.Length > 10)
grid.Rows[i].Cells[0].Text = x.Substring(0, 10) + "<a href=\"\">Read More </a>";

// In href specify the link where you want to give the complete details.
if (i >= table.Rows.Count)

Case 2:
Without redirecting to an another page, if you want get the whole description to be displayed beneath the row itself. Use the concept of Nested Grid View and Java Script.
You want to display without page refresh or redirecting to another page, so it is all with java script to do.



<script type="text/javascript">
function showfiles(id) {
if (document.getElementById(id).style.display == "none") {
document.getElementById(id).style.display = "block";
else {
document.getElementById(id).style.display = "none";

aspx :

<asp:GridView ID="grid" runat="server" AutoGenerateColumns="false" OnRowDataBound="WriteReadMore" DataKeyNames="AutoId" >
<asp:TemplateField HeaderText="Description">
<%# Eval("ReadMore") %> // Read More is the column which contains the substring.
<a onclick="showfiles('<%#Eval ("AutoId") %>')" href="#">Read More:</a>
<div style="display: none" id="<%# Eval("AutoId") %>">
<asp:GridView ID="GridViewNested" AutoGenerateColumns="false" ShowHeader="false" runat="server" EnableViewState="false" BackColor="Aquamarine"
<asp:BoundField DataField="Description" />

Here, i used the concept of Nested GridView.
The main issue comes over here is we should and must use <columns> to embed an another gridview inside the parent gridview.
If we use columns, <%# Eval() %> should be used. Eval takes the value directly from the table column. So, the complete Description comes. Hence, while insertion itself we have to maintain a seperate column inside the table which contains the part i.e substring of the description to be shown.

First, Get the Substring using <%# Eval("ReadMore") %> then with a link.
<a onclick="showfiles('<%#Eval ("AutoId") %>')" href="#">Read More:</a> (In the above case)

In the nested gridview, on click we get the same table but the whole description part (or you may include any other columns that you want) will be rendered.

Code to get Nested Grid View:

protected void WriteReadMore(object sender, GridViewRowEventArgs e)
DataTable tablefull = new DataTable();
if (e.Row.RowType != DataControlRowType.DataRow) return;
if (e.Row.RowType == DataControlRowType.DataRow)
int autoid = int.Parse(grid.DataKeys[e.Row.RowIndex].Value.ToString());
GridView nestedgrid = (GridView)e.Row.FindControl("GridViewNested");
using (SqlConnection conn = new SqlConnection(_connStr))
string sql = "Select * From InboxDescription where AutoId = @AutoId";
using (SqlCommand cmd = new SqlCommand(sql, conn))
cmd.Parameters.AddWithValue("@AutoId", autoid);

using (SqlDataAdapter da = new SqlDataAdapter(cmd))
nestedgrid.DataSource = tablefull;

Note: If you had already created a table in the database. Now you want to add a column Read More( with allow nulls so as not to re-create the table).
My InboxDescription table contains AutoId, FileName, Description, ReadMore as columns


@readmore varchar(50),
@Description varchar(200),
@FileName varchar(max),
@AutoId int

select @readmore = SUBSTRING([Description], 0, 5) from InboxDescription
select @AutoId = AutoId from InboxDescription
select @FileName = [FileName] from InboxDescription
select @Description = [Description] from InboxDescription

Update InboxDescription set [FileName] = @FileName, [Description] = @Description, ReadMore = @Readmore where AutoId = @AutoId

I hope, this gave you a clear Explanation.


Awesome Coding !! :)

Dharanidhar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kotra.Ramakrishna on: 9/17/2012 [Member] Starter | Points: 25


Not Sql query. you have to restrict from C#

Get all 8 line data from Sql server, then in you C# code if that string exceeds 3 lines length(check approximate 3 lines length)

that string exceeds that length. use Substring methods

string str="This is my 8 lines string";
string temp=string.Empty;


based on your requirement you can design your method

Thanks & Regards

Dharanidhar, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response