Aspx cs example(Homepage CodeBehind and interpretation, index.aspx.cs)

Lionsure 2020-08-19 Original by the website

The CodeBehind of homepage is mainly to realize data binding, that is, use SQL statements to select records from the database and then bind them to the control. The code implementation is relatively simple. In addition, there are also methods for assigning values to controls, string connection method, and displaying and hiding controls.

 

I. Knowledge points involved in the CodeBehind of homepage(Aspx cs example)

1. Assign a value to the server-side control(Literal);

2. Data binding;

3. Connection method of string;

4. Display and hide controls according to the content.

 

II. Knowledge analysis

1. Assign a value to the server-side control(Literal)

Non-list-type commonly used server-side controls include Literal, Label, HyperLink, Image, HiddenField, TextBox, Button. The first four are commonly used for pages that do not submit forms, and the last three are commonly used for submitting forms.

Literal and Label both output text to the foreground. The difference is that Literal only outputs text without outputting any Html elements, while Label outputs Html elements and text, that is, use <span>text</span>;

HyperLink is a hyperlink, which outputs <a href="">text</a> in the foreground;

HyperLink is a hyperlink, which outputs <a href="">text</a> in the foreground;

Image is a picture, output in the foreground <img src="" />;

The last three are introduced one by one on the page where the form is submitted. Use Literal to assign values to the title, keywords, and description of homepage. For example, the front-end code for assigning a value to the title of homepage is:

<title><asp:Literal ID="litTitle" runat="server" /></title>

 

The code in the CodeBehind is:

litTitle.Text = PublicStatic.webName + "_" + PublicStatic.keywords;

It can be seen from the assignment code that the title of homepage is composed of "site name and keywords", which is beneficial for ranking in search engines. Both the name(webName) and keywords refer to variables in the static class(PublicStatic), which are got from the database by this class. For specific implementation, please refer to the PublicStatic class.

 

2. Data binding(eg. How to bind data in repeater in asp.net using C#)

The realization process: first get the records from the database or file, and then bind it to the control.

Since the homepage opens as quickly as possible, the Repeater control with the least output data is used to bind data, for example, to bind "Company News".

Code in the foreground:

<asp:Repeater ID="repNews" runat="server">
              <ItemTemplate>
                     <li><a href="/news/<%# Eval("newsid") %>.htm" target="_blank"><%# EpWeb.privates.classes.PublicClass.ToString(Eval("newsname")).Length > 12 ? EpWeb.privates.classes.PublicClass.newstr(EpWeb.privates.classes.PublicClass.ToString(Eval("newsname")), 12) + "…" : EpWeb.privates.classes.PublicClass.ToString(Eval("newsname"))%></a>[<%# Eval("adddate","{0:MMMM dd, yyyy}") %>]
                     </li></ItemTemplate>
       </asp:Repeater>

 

Code in the CodeBehind;

//Bind news
       private void BindNews()
       {
              string strSQL = "Select top 11 newsid,newsname,adddate From news Order By adddate Desc";
              DataTable dt = SqlData.dataTable(strSQL);

       repNews.DataSource = dt.DefaultView;
              repNews.DataBind();
       }

The code in the CodeBehind defines the binding process as a method, and the implementation process is as follows:

1) First define the SQL statement to get data from the database. The meaning of SQL statement is to get 11 records(top 11) sorted by adddate from the news table(news) of database. The selected fields are newsid, newsname,adddate.

2) Then call the dataTable method of the SqlData class to execute the SQL statement, and assign the got records to the DataTable variable dt; the SqlData class mainly realizes connecting to the database and getting records from the database. Please see the SqlData class for specific implementation methods.

3) Bind the got data to the control Repeater, using the last two lines of code in the method.

Although only the binding method of Repeater control is mentioned above, the binding methods of other controls are also the same.

 

3. Connection method of string

There are usually two ways to connect several characters to a variable.

1) Connect with plus sign +, the code is as follows:

string names = "a" + "b" +"c";

 

2) Using string.Format, take the keyword assignment on the homepage as an example, the code is as follows:

string meta = "<meta name=\"{0}\" content=\"{1}\" />";
       litKeywords.Text = string.Format(meta, "Keywords", PublicStatic.keywords);

There are {0} and {1} in the variable meta, which are numbers used to be replaced by strings. {0} will be replaced by "Keywords" and {1} will be replaced by PublicStatic.keywords.

 

4. Display and hide controls according to the content

Asp.net controls are hidden and displayed with Visible property; if Visible is not set, or set to true, the control will be displayed; if it is set to false, the control will be hidden. In the "Friendly Links" part of homepage, which should decide whether to display according to whether a friendship link is added.

 

Code in the foreground:

<asp:Panel ID="paLink" runat="server" CssClass="friendlink" Visibl="false">
              <h3 class="title border-b">Friendly Links:</h3>
              <ul class="friendlink_list">
                     <asp:Repeater ID="repLink" runat="server"><ItemTemplate>
                            <li><a href="<%# Eval("url") %>" target="_blank"><%# Eval("sitename") %></a></li></ItemTemplate></asp:Repeater></ul></asp:Panel>

The Panel control is used in the code, visible is set to "false", that is, set to hidden.

 

Code in the CodeBehind£º

//Bind Friendly Links
       private void BindLink()
       {
              string strSQL = "select sitename,url from friendlink";
              DataTable dt = SqlData.dataTable(strSQL);

       if (dt.Rows.Count > 0)
              {
                     repLink.DataSource = dt;
                     repLink.DataBind();
                     paLink.Visible = true;
              }
       }

If the "Friendly Link" record can be selected from the database(i.e. if (dt.Rows.Count > 0)), the Panel control(i.e. paLink.Visible = true) will be displayed.

 

III. Complete source code of aspx cs example


using System;
using System.Web;
using System.Web.UI.WebControls;
using System.Data;
using EpWeb.privates.classes;

namespace EpWeb
{
       public partial class index : System.Web.UI.Page
       {
                     protected void Page_Load(object sender, EventArgs e)
                     {
                                   SetWebHeader();
                                   BindNews();
                                   BindProduct();
                                   BindClass();
                                   BindLink();
                     }

                     private void SetWebHeader()
                     {
                                   litTitle.Text = PublicStatic.webName + "_" + PublicStatic.keywords;
                                   string meta = "<meta name=\"{0}\" content=\"{1}\" />";

                                   litKeywords.Text = string.Format(meta, "Keywords", PublicStatic.keywords);
                                   litDescription.Text = string.Format(meta, "Description", PublicStatic.description);
                     }

                     //Bind products
                     private void BindProduct()
                     {
                                   //Bind the latest supply
                                   string strSQL = "Select top 6 id,pname,psize,price1,pic,pic1 From product Where newProduct = 1 and firstFlag = 1 Order By adddate Desc,id Desc";
                                   DataTable dt = SqlData.dataTable(strSQL);
                                   repNewProduct.DataSource = dt.DefaultView;
                                   repNewProduct.DataBind();

                                   //Bind recommended products
                                   strSQL = "Select top 7 id,pname,psize,price1,pic,pic1 From product Where recommend = 1 and firstFlag = 1 Order By adddate Desc,id Desc";
                                   dt = SqlData.dataTable(strSQL);
                                   repRecommend.DataSource = dt.DefaultView;
                                   repRecommend.DataBind();

                                   //Bind products
                                   strSQL = "Select top 8 id,pname,psize,price1,pic,pic1 From product Where recommend = 0 and firstFlag = 1 Order By adddate Desc,id Desc";
                                   dt = SqlData.dataTable(strSQL);
                                   repProduct.DataSource = dt.DefaultView;
                                   repProduct.DataBind();
                     }

                     //Bind categories
                     private void BindClass()
                     {
                                   string strSQL = "Select Top 14 categoryid,category From category order by categoryorder";
                                   DataTable dt = SqlData.dataTable(strSQL);
                                   repClass.DataSource = new DataView(dt);
                                   repClass.DataBind();
                     }

                     //Bind news
                     private void BindNews()
                     {
                                   string strSQL = "Select top 11 newsid,newsname,adddate From news Order By adddate Desc";
                                   DataTable dt = SqlData.dataTable(strSQL);
                                   repNews.DataSource = dt.DefaultView;
                                   repNews.DataBind();
                     }

                     //Bind Friendly Links
                     private void BindLink()
                     {
                                   string strSQL = "select sitename,url from friendlink";
                                   DataTable dt = SqlData.dataTable(strSQL);

                                   if (dt.Rows.Count > 0)
                                   {
                                                 repLink.DataSource = dt;
                                                 repLink.DataBind();
                                                 paLink.Visible = true;
                                   }
                     }
       }
}

Aspx: Examples of html page layout using div(Homepage design and interpretation, index.aspx source code)