Asp.net text-and-text hybrid Long News pagination

Source: Internet
Author: User

The problem encountered some time ago finally solved. Now I share the code, and the Code has not been sorted out. It is a bit messy, but I still have some ideas.

First, paging is implemented by searching the <p> tag, so it is best to use the <p> tag segment when publishing news in the background.

Second, we should consider that html tags cannot be truncated during paging. Otherwise, the page may become terrible. Therefore, the program needs to find the </p> tag and then pagination after it.

Finally, I have to think that the last page of news should not contain too little content. For example, if a user clicks a button and the result shows a line of news, is it funny, determine the length of news at any time. Note that the number of news pages should not be too large. Here I set a maximum of 9 pages.

The following code divides a page based on 1000 characters. The basic idea is that the program retrieves 1000 characters and then searches for the nearest <p> tag from here, locate and paging before it. At the same time, we also need to determine the number of remaining non-Paging characters. I set that if the number of remaining characters is less than 1800, we do not need to go to the page, to prevent the last page from being too small.

Private void BindNewContent () // method for obtaining news
{
Int id = Convert. ToInt32 (Request. QueryString ["id"]); // obtain the news id
Int newPageId = Convert. ToInt32 (Request. QueryString ["newPageId"]); // obtain the page where the news is displayed.
DBClass db = new DBClass ();
String SelectStr =
"Select * from new where newId = @ id ";
SqlCommand com = db. GetCommandStr (SelectStr );
Com. Parameters. Add ("@ id", SqlDbType. Int). Value = id;
SqlDataReader sdr = db. ExecDataReader (com );
If (sdr. Read ())
{
// Bind the news body and pagination.
String newMainText = sdr ["newMainText"]. ToString ();
NewMainText = SetNewMainPage (newMainText, newPageId, id); // use SetNewMainPage to display the news page

TitleLabel. Text = sdr ["newTitle"]. ToString ();
Page. Header. Title = "[" + sdr ["newTitle"]. ToString () + "]";
SourceLabel. Text = sdr ["newSource"]. ToString ();
TimeLabel. Text = sdr ["newTime"]. ToString ();
SummaryLabel. Text = sdr ["newSummary"]. ToString ();
ContentLabel. Text = newMainText;
// ContentLabel. Text = sdr ["newMainText"]. ToString ();
EditorLabel. Text = "Edit responsibility:" + sdr ["newEditor"]. ToString ();
}
Else
{
Response. Write ("<script> alert ('the page you accessed does not exist'); window. close (); </script> ");
Response. End ();
Return;
}
Sdr. Close ();
}

Private string SetNewMainPage (string newMainText, int newPageId, int id) // method of news paging. newMainText is the news body string, and newPageId indicates which page is the news currently, id indicates the id of the news.
{
String strContent = newMainText;
ArrayList min = new ArrayList ();
String [] strNew = new string [10];
Int index = 0;
While (! String. IsNullOrEmpty (strContent ))
{
If (strContent. Length <= 1800 | index> = 8) // to prevent the last page of the news from being too short or too many pages of the news, let's take a look.
{
// Response. Write (index); return null;
StrNew [index] = strContent;
Break;
}
Else
{
StrNew [index] = strContent. Substring (0, strContent. IndexOf ("<p", 1000 ));
StrContent = strContent. Remove (0, strContent. IndexOf ("<p", 1000 ));
Index ++;
}
}
// The following is to center the navigation buttons on the front-end display news page and place them in a table.
// Adds a layer surrounded by news page links
Table hrefDiv = new Table ();
TableRow row = new TableRow ();
TableCell cell = new TableCell ();
HrefDiv. Controls. Add (row );
Row. Controls. Add (cell );
HrefDiv. Attributes. Add ("class", "hrefDiv ");
NewPagePanel. Controls. Add (hrefDiv );

For (int I = 0; I <strNew. Length; I ++)
{
If (strNew [I]! = Null)
{
HyperLink HyperLink1 = new HyperLink ();
HyperLink1.ID = "HyperLink" + I;
HyperLink1.NavigateUrl = "~ /DetailedNew. aspx? Id = "+ id +" & newPageId = "+ (I + 1 );
HyperLink1.Text = (I + 1). ToString ();
HyperLink1.Attributes. Add ("class", "newHref ");
If (newPageId = (I + 1 ))
{
HyperLink1.Attributes. Add ("style", "background-color: # cc0000; color: # fff; border: 2px # cc0000 solid ;");
}
Cell. Controls. Add (HyperLink1 );
}
If (strNew [I] = null)
{
Min. Add (I );
}
}
// Add the clear layer. Add a <div style = "clear: both"/> layer to be compatible with css.
Panel clearDiv = new Panel ();
ClearDiv. Attributes. Add ("class", "clear ");
Cell. Controls. Add (clearDiv );
// Prevent Users From navigating to a news page that does not exist by changing the address bar
If (newPageId> = (int. Parse (min [0]. ToString () + 1 ))
{
Response. Write ("<script> alert ('the page you accessed does not exist'); window. close (); </script> ");
Response. End ();
Return null;
}
If (int. Parse (min [0]. ToString () <= 1)
{
NewPagePanel. Visible = false;
}
Return strNew [newPageId-1]. ToString ();

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.