Asp.net 2.0 practice (1) _ creating a custom dynamic news LIST _ jaye.net

Source: Internet
Author: User
Tags truncated
(This series is some of Asp.net 2.0's skills in use. please correct me at the blog Office)

Sometimes, there is a requirement for the News list. for the reason of the format, it is required that the display part exceed the fixed number of words, and the excess part is truncated into..., as shown below:

The format of the postgraduate degree thesis...

The format of the postgraduate degree thesis...

The format of the postgraduate degree thesis...

The format of the postgraduate degree thesis...

The HTML syntax is <p>
<A Title = "Notice on the form of postgraduate degree thesis" href = ".htm">
The format of the postgraduate degree thesis... </a> </P>

Of course, if you do not want to customize the image, it can also be expressed
<Li> <a Title = notice on the form of postgraduate degree thesis href = "..htm" target = _ blank> notice on the form of postgraduate degree thesis </a> <br>

Here, the title is used to show the prompts that appear when the mouse moves to the truncated display content. The truncated display content is dynamically truncated from the title attribute, while the href attribute is the title and the path content is a URL.

1. Process Repeater

In Asp.net 2.0, you can use bulletedlist as a news list, but what you cannot do is generate the title attribute of the <A> tag. Ide will prompt that bulletedlist cannot use eval, bind and other data binding methods.

 

Therefore, the repeater control is used to display controls. The Repeater control is a powerful control. When other controls cannot achieve special effects, they can only be used, but its visual operations are poor.

Operations can only be performed at the source code level.

Enter the source code. Define the repeater display template by writing HTML tags. For example:

<Asp: repeater id = "repeater1" runat = "server" performanceid = "objectperformancebinghai"> <itemtemplate>

<A href = '<% # eval ("title", "201702.16.htm") %> 'title =' <% # eval ("title ") %> '> <% # eval ("Subtitle title") %> </a> <br>
</Itemtemplate>

</ASP: repeater>

You can use other repeater templates, such as <footertemplate>, but this news list is not required.

Ii. Data logic layer

In performanceid, we can see that objectdatasource is used instead of the commonly used sqldatasource, because you use sqldatasource, it is difficult to generate the function of truncating a field value and processing it (the method class is applicable to the substring function used to process the SELECT statement of sqldatasource In the second article of this series ).. the function of objectdatasource is to create a data source by yourself. The data source can return dataset, datareader, array, and so on, and bind the control.

Create a class under app_code. Named newsshow. cs. newsshow is used to retrieve data from the News Database and truncate the data.

Add the static method (getnews) and return a dataset containing two fields. The title field is the title truncation result.

Public static dataset getnews (string newstype) // type of incoming news
{
Sqlconnection con = new sqlconnection (configurationmanager. connectionstrings ["bobbyconnectionstring"]. connectionstring );
Sqldataadapter SDQ = new sqldataadapter ("select top" + configurationmanager. appsettings ["topnews"]. tostring () + "[title] from [News] Where [newstype] = @ newstype", con );
Dataset DS = new dataset ();
Int newscut;
Newscut = int. parse (configurationmanager. receivettings ["newscut"]. tostring ());
SDQ. selectcommand. Parameters. addwithvalue ("@ newstype", newstype );
Con. open ();
SDQ. Fill (DS );
DS. Tables [0]. Columns. Add ("partition title ");
DS. tables [0]. columns ["partition title"]. expression = "isnull (IIF (LEN (title)>" + newscut + ", substring (title, 1," + newscut + "), title ),'') + '... '"; // This sentence is used to cut the title
Con. Close ();
Return Ds;

}

In the corresponding web. config

<Deleetask>
<Add key = "topnews" value = "4"/>
<Add key = "newscut" value = "10"/>
</Appsettings>

Three visualization binding

Next, we recommend that you use an objectdatasource to establish a connection with newsshow. CS and bind the repeater. All are visualized operations.

1. Select logical components

2. Select the select method, update and other methods are not required for the moment.

3. Set the parameter. This parameter transfers the news type to getnews (string newstype)

 

4. Set the performanceid of Repeater

<Asp: repeater id = "repeater1" runat = "server" performanceid = "objectperformancebinghai">

<Itemtemplate>
<
</Itemtemplate>

</ASP: repeater>

5. Bind the attributes of the control in the repeate itemtemple Template

IMG src = "image/soft08.gif"/>
<A href = '<% # eval ("title", "201702.16.htm") %> 'title =' <% # eval ("title ") %> '> <% # eval ("Subtitle title") %> </a> <br>

OK. Finished.

Related Article

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.