Click the content to query the details

1. Connect to the database

2. Create two tables

3. Primary key table

Field nametypeconstraint
IDintPrimary key, auto increment
Contentnvarchar(20)Non empty

4. Foreign key table

Field nametypeconstraint
IDintPrimary key, auto increment
Titlenvarchar(20)Non empty
Authornvarchar(20)Non empty
Contnvarchar(50)Non empty
LeibieintNon empty, foreign key, primary key, table ID
DatadateDefault current date

5. Add data to the two tables,

Project operation

1. Create ASP Net project (. Net fromwork)

2. Create class library Model

3. Create the second class library DAL

4. Create the third class library BLL

5. Add ADO to the Model layer Net entity data Model

6. Follow the operation and finally connect to the database

DAL layer operation

1. Reference Model layer

2. Add reference EntityFromework

3.. Add reference entityfromework sqlserver

4. Create two classes because there are two tables. One class corresponds to one table

5. The operation code of the primary key table is as follows:

ZuoEntities db = new ZuoEntities();
        public IEnumerable<Catelog> Select()
        {
            var result = from u in db.Catelog select u;
            return result.ToList();
        }

6. The operation codes of the foreign key table are as follows:

ZuoEntities db = new ZuoEntities();
        public object Select()
        {
            var result = (from a in db.Article
                          join c in db.Catelog
                          on a.Leibie equals c.ID
                          select new {a.ID,a.Title,a.Author, a.Data,a.Cont,c.Content }).ToList();
            return result;

        }   
    
        View detail code
         public object Sel(int id)
        {
            var result = from a in db.Article where a.ID==id select new { a.Title, a.Cont };
            return result.ToList();
        }

BLL layer operation

1. Reference model layer and DAL layer

2. Operate on two tables

3. Create two classes because there are two tables. One class corresponds to one table

4. Foreign key table codes are as follows:

ArtDAO da = new ArtDAO();
        public object Select()
        {
            return da.Select();
        }
      
        View details
          public object Sel(int id)
        {
            return da.Sel(id);
        }

View layer ASP Net operation

1. Reference the Model layer and BLL layer

2. Add reference EntityFromework

3.. Add reference entityfromework sqlserver

4. Find app in the Model layer Config file

5. Cut file

6. Put the code just cut into the web view layer Config

7. Create a new form named fault

8. The code is as follows:

 <asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">
   <ItemTemplate>
 <div style="border: 1px solid #ffd800; width: 250px; margin: 0 auto; margin-top: 10px;  border-radius: 35px; background-color: lightgray;">

                            <div style="flex-direction: row; text-align: center; margin: 0 auto;">

                                <span style="color:white">number<%# Eval("ID") %></span>

                                <p style="color: mediumpurple">title:<%# Eval("Title") %></p>

                                <span style="color: #d200ff">author:<%# Eval("Author") %></span>
                                <p style="color: aquamarine">date:<%# Eval("Data") %></p>

                                <div style="border: 1px solid white; background-color: pink; border-radius: 20px;">
                                    <p>
                                        <asp:LinkButton ID="LinkButton1" runat="server" CommandName="di" CommandArgument='<%# Eval("ID") %>' ForeColor="#ccffff"><%# Eval("Cont") %></asp:LinkButton>
                                    </p>

                                </div>
                                <span >category:<%# Eval("Content") %></span>
                            </div>
                        </div>
                          </ItemTemplate>

9. The binding code of Repeater control is as follows:

 ArtService se = new ArtService();
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Repeater1.DataSource = se.Select();
            this.Repeater1.DataBind();
        }

10. Note that there are two very important properties in the LinkButton control. One is CommandName, which is named after di. The other is CommandArgument, which is the ID of the binding and the ID to be checked later. It is also used for value transfer

11. Add a property OnItemCommand in the repeater control, which is used to write methods

12. Create a new web form XQ Aspx, jump to query details later

13. In XQ Add data in the ASPX form. The code is as follows:

<asp:Repeater ID="Repeater1" runat="server">
                <ItemTemplate>
                    <div style="margin:0px auto;width:250px;height:100px;border-radius:50px;line-height:40px; text-align:center;background-color:aquamarine">
                        <p>title:<%# Eval("Title") %></p>
                        <p>Content:<%# Eval("Cont") %></p>
                    </div>
                </ItemTemplate>
            </asp:Repeater>

14. Go to the background code of the property OnItemCommand just added in the repeater control, as follows:

 protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
        int id = int.Parse(e.CommandArgument.ToString());
            if (e.CommandName == "di")
            {
                Response.Redirect("xq.aspx?id="+id);
            }
        }

15. Because the id value is sent, we need to receive it.

16. In XQ In the ASPX form, go to the background code to receive as follows:

This code is used to receive the information transmitted id value
 int id = int.Parse(Request.QueryString["id"]);

17. Full details are as follows:

public partial class xq : System.Web.UI.Page
    {
        ArtService se = new ArtService();
        protected void Page_Load(object sender, EventArgs e)
        {
            int id = int.Parse(Request.QueryString["id"]);
           
            this.Repeater1.DataSource = se.Sel(id);
            this.Repeater1.DataBind();
        }
    }

This code teaching is over. Have you learned it?

Keywords: Software development

Added by pietbez on Fri, 21 Jan 2022 03:43:13 +0200