Asked by:
How do i display more description in datalist when "See more" linkbutton is clicked?

Question
-
User-2074858223 posted
Instead of displaying all content on page which can slow down the page, how can i display 10 rows on datalist, but when "see more" buttonlink is clicked more rows will be displayed.
<asp:DataList ID="DataList1" runat="server"> <ItemTemplate> <asp:Label ID="lbldesciption" Text='<%#Eval("desciption") %>' runat="server" Font-Size="Medium" ForeColor="#999999" /> </ItemTemplate> </asp:DataList>
Saturday, June 23, 2018 6:14 PM
All replies
-
User-330142929 posted
Hi Micah2012,
According to your description,
micah2012
how can i display 10 rows on datalist, but when "see more" buttonlink is clicked more rows will be displayed.you want to display more rows when “see more” buttonlink is clicked.
I suggest you could use ItemCommand event handler to load all data to the label.
and format the string into 10 rows. You could handle the string in itemDatabound. Or directly process it in the front.
<asp:Label ID="DescriptionLabel" runat="server" Text='<%# Eval("Description").ToString().Length>10?Eval("Description").ToString().Substring(0,10)+"........":Eval("Description").ToString() %>'/>
I have make a demo, and wish it could be useful to you.
Aspx.
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager> <div> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <asp:DataList ID="DataList1" runat="server" DataKeyField="Id" DataSourceID="SqlDataSource1" GridLines="Both" OnItemDataBound="DataList1_ItemDataBound" OnItemCommand="DataList1_ItemCommand"> <ItemTemplate> Id: <asp:Label ID="IdLabel" runat="server" Text='<%# Eval("Id") %>' /> <br /> Name: <asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' /> <br /> Description: <asp:Label ID="DescriptionLabel" runat="server" Text='<%# Eval("Description") %>'/> <br /> <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Display">See more</asp:LinkButton> <br /> <br /> </ItemTemplate> </asp:DataList> </ContentTemplate> </asp:UpdatePanel> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MyDataStore %>" SelectCommand="SELECT * FROM [Records]"></asp:SqlDataSource> </div>
Code behind.
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e) { Label label = e.Item.FindControl("DescriptionLabel") as Label; string all = ""; int indexss = 0; //display 5 rows. for (int i = 0; i < 5; i++) { //per row 20 character. all = all + label.Text.Substring(indexss, 20) + "<br>"; indexss = indexss + 20; } label.Text = all; } protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e) { if (e.CommandName == "Display") { Label label = e.Item.FindControl("DescriptionLabel") as Label; string id = (e.Item.FindControl("IdLabel") as Label).Text; SqlConnection sqlconenction = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDataStore"].ConnectionString); SqlCommand command = new SqlCommand(); command.CommandText = "select Description from Records where id=" + id; command.CommandType = System.Data.CommandType.Text; command.Connection = sqlconenction; SqlDataAdapter sda = new SqlDataAdapter(command); DataTable dt = new DataTable(); sda.Fill(dt); string result = dt.Rows[0][0].ToString(); string all = ""; int index = 0; for (int i = 0; i < result.Length/20; i++) { all += result.Substring(index, 20) + "<br>"; index += 20; } label.Text = all; } }
How It works.
If the solution could not solve your problem, feel free to let me know.
Best Regards,
Abraham
Monday, June 25, 2018 9:37 AM -
User-2074858223 posted
Thank you for your response, what you did is not really what am looking for, may be i confused you when i said rows. See what i mean, i have 100 records in table, now i want to display from 1 to 5 and then display button view more so that when users click the button more records will be shown, either by bottom view more or automatically load more records when the user scrolls down.
table example
ID Description UserName
--------------------------------------------------
1 record1 usr3
-------------------------------------------------
2 record1 usr37
-------------------------------------------------
3 record3 usr96
--------------------------------------------
4 record51 usr3
-------------------------------------------------
5 record14 usr38y
-------------------------------------------
6 record13 usr3vb
-----------------------------------------------------
7 record15 usr38
-------------------------------------------------
8 record9 usr3
-------------------------------------------------
9 record4 usr3
-------------------------------------------------
--------------------------------------------------
10 record1 usr3
-------------------------------------------------
Thursday, June 28, 2018 9:28 AM -
User-1171043462 posted
Refer
Load images while scrolling page down with jQuery AJAX in ASP.Net
You will need to implemented AJAX based DataList and load data as page scrolls.
Thursday, June 28, 2018 9:50 AM -
User-2074858223 posted
Even though this example is close to what i need but it could work in other questions that i have, please before i mark it answered, i tried this and it didnt work well, are you using jquery for the click?
Saturday, June 30, 2018 9:27 AM