DataList初识及实现分页效果11.25

 

看帖评论是美德!

 

DataList是一种纯模板控件!灵活性比GridView灵活许多,但是相对应的代码量也增加了许多!

今天学习了DataList控件的使用!贴一下基本的使用方法!希望对大家有所帮助!

//前台(aspx)代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:DataList ID="DataList1" runat="server" Width="384px" RepeatColumns="2" 
            RepeatDirection="Horizontal" onitemcommand="DataList1_ItemCommand" 
            oncancelcommand="DataList1_CancelCommand" 
            oneditcommand="DataList1_EditCommand" ondeletecommand="DataList1_DeleteCommand" 
            onupdatecommand="DataList1_UpdateCommand">
            <EditItemTemplate>
                <table class="style1">
                    <tr>
                        <td>
                            名称</td>
                        <td>
                            <asp:TextBox ID="TextBox1" Text='<%#Eval("sname") %>' runat="server"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            性别</td>
                        <td>
                            <asp:TextBox ID="TextBox2" Text='<%#Eval("sex") %>' runat="server"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            年龄</td>
                        <td>
                            <asp:TextBox ID="TextBox3" Text='<%#Eval("age") %>' runat="server"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>
                             </td>
                        <td>
                            <asp:Button ID="Button4" runat="server" CommandName="update" Text="更新" 
                                CommandArgument='<%# Eval("sid") %>' />
                            <asp:Button ID="Button5" runat="server" CommandName="cancel" Text="取消" />
                        </td>
                    </tr>
                </table>
            </EditItemTemplate>
            <ItemTemplate>
                名称:<%#Eval("sname") %><br />图片:<asp:Image ID="imgPhoto" Width="60px" Height="60px" runat="server" ImageUrl='<%#Eval("photo") %>' /><br/>
                性别:<%#Eval("sex") %><br/>年龄:<asp:Label ID="Label1" runat="server" Text='<%# Eval("age") %>'></asp:Label>
                <br />
                <asp:Button ID="Button1" runat="server" CommandArgument='<%#Eval("sid") %>' 
                    Text="放入购物车" CommandName="buy" />
                <br />
                <asp:Button ID="Button2" runat="server" CommandArgument='<%#Eval("sid") %>' 
                    CommandName="Edit" Text="编辑" />
                <asp:Button ID="Button3" runat="server" Text="删除" 
                    CommandArgument='<%# Eval("sid") %>' CommandName="Delete" />
            </ItemTemplate>
        </asp:DataList>
    
        <br />
        <asp:Button ID="Button6" runat="server" οnclick="Button6_Click" Text="Last" />
        <asp:Button ID="Button7" runat="server" οnclick="Button7_Click" Text="Prev" />
        <asp:Button ID="Button8" runat="server" οnclick="Button8_Click" Text="Next" />
        <asp:Button ID="Button9" runat="server" οnclick="Button9_Click" Text="Last" />
        <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
        <br />
        <asp:HiddenField ID="HiddenField1" runat="server" />
        <asp:HiddenField ID="HiddenField2" runat="server" />
    
    </div>
    </form>
</body>
</html>
 
 
//后台(CS)代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
 
public partial class _Default : System.Web.UI.Page
{
    private string strCnn;
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
            BindProduct("1");
    }
 
    private void BindProduct(string index)
    {
        strCnn = ConfigurationManager.ConnectionStrings["studentCnn"].ConnectionString;
        using (SqlConnection sqlCnn = new SqlConnection(strCnn))
        {
            using (SqlCommand sqlCmm = sqlCnn.CreateCommand())
            {
                sqlCmm.CommandText = "sp_Student_Select_by_Page_rowNumber";
                sqlCmm.CommandType = System.Data.CommandType.StoredProcedure;
                SqlDataAdapter da = new SqlDataAdapter(sqlCmm);
                sqlCmm.Parameters.AddWithValue("pageIndex", index);
                sqlCmm.Parameters.Add("@pageCount", SqlDbType.Int).Direction = ParameterDirection.Output;
                sqlCmm.Parameters.AddWithValue("@pageSize", 2);
                
                DataSet ds = new DataSet();
                da.Fill(ds);
                this.DataList1.DataSource = ds.Tables[0].DefaultView;
                this.DataBind();
                this.HiddenField1.Value = index.ToString();
                this.HiddenField2.Value = sqlCmm.Parameters["@pageCount"].Value.ToString();
                this.Label2.Text = index + "/" + this.HiddenField2.Value;
            }
 
 
            效果同上
            //SqlDataAdapter da = new SqlDataAdapter("select * from student", sqlCnn);
            //DataSet ds = new DataSet();
            //da.Fill(ds);
            //this.DataList1.DataSource = ds.Tables[0].DefaultView;
            //this.DataBind();
        }
    }
    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName == "buy")
        {
            Response.Write(e.CommandArgument);
        }
    }
    protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
    {
        this.DataList1.EditItemIndex = e.Item.ItemIndex;
        this.BindProduct(this.HiddenField1.Value);
    }
    protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
    {
        this.DataList1.EditItemIndex = -1;
        this.BindProduct(this.HiddenField1.Value);
    }
    protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
    {
        strCnn = ConfigurationManager.ConnectionStrings["studentCnn"].ConnectionString;
        string name=(e.Item.FindControl("TextBox1") as TextBox).Text;
        string sex=(e.Item.FindControl("TextBox2") as TextBox).Text;
        string age=(e.Item.FindControl("TextBox3") as TextBox).Text;
        using (SqlConnection sqlCnn=new SqlConnection(strCnn))
        {
            using (SqlCommand sqlCmm=sqlCnn.CreateCommand())
            {
                sqlCmm.CommandText = "update student set sname=@sname,sex=@sex,age=@age where sid=@sid";
                sqlCmm.Parameters.AddWithValue("@sname", name);
                sqlCmm.Parameters.AddWithValue("@sex", sex);
                sqlCmm.Parameters.AddWithValue("@age", Convert.ToInt32(age));
                sqlCmm.Parameters.AddWithValue("@sid", e.CommandArgument);
                sqlCnn.Open();
                sqlCmm.ExecuteNonQuery();                
            }            
        }
        this.DataList1.EditItemIndex = -1;
        this.BindProduct(this.HiddenField1.Value);
    }
    protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)
    {
        strCnn = ConfigurationManager.ConnectionStrings["studentCnn"].ConnectionString;       
        using (SqlConnection sqlCnn = new SqlConnection(strCnn))
        {
            using (SqlCommand sqlCmm = sqlCnn.CreateCommand())
            {
                sqlCmm.CommandText = "delete from student where sid=@sid";                
                sqlCmm.Parameters.AddWithValue("@sid", e.CommandArgument);
                sqlCnn.Open();
                sqlCmm.ExecuteNonQuery();
            }            
        }
        this.DataList1.EditItemIndex = -1;
        this.BindProduct(this.HiddenField1.Value);
    }
    protected void Button6_Click(object sender, EventArgs e)
    {
        this.BindProduct("1");
    }
 
    protected void Button9_Click(object sender, EventArgs e)
    {
        string total = this.HiddenField2.Value;
        this.BindProduct(total);
    }
    protected void Button7_Click(object sender, EventArgs e)
    {
        int index = Convert.ToInt32(this.HiddenField1.Value);
        if (index > 0)
        {
            index--;
        }
        this.BindProduct(index.ToString());
    }
    protected void Button8_Click(object sender, EventArgs e)
    {
        int index = Convert.ToInt32(this.HiddenField1.Value);
        int total = Convert.ToInt32(this.HiddenField2.Value);
        if (index < total)
            index++;
        this.BindProduct(index.ToString());
    }
}
 
 
//web.config配置中添加一条连接数据库语句

 

存储过程在上一遍关于GridView中有贴出来!

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
目标检测(Object Detection)是计算机视觉领域的一个核心问题,其主要任务是找出图像中所有感兴趣的目标(物体),并确定它们的类别和位置。以下是对目标检测的详细阐述: 一、基本概念 目标检测的任务是解决“在哪里?是什么?”的问题,即定位出图像中目标的位置并识别出目标的类别。由于各类物体具有不同的外观、形状和姿态,加上成像时光照、遮挡等因素的干扰,目标检测一直是计算机视觉领域最具挑战性的任务之一。 二、核心问题 目标检测涉及以下几个核心问题: 分类问题:判断图像中的目标属于哪个类别。 定位问题:确定目标在图像中的具体位置。 大小问题:目标可能具有不同的大小。 形状问题:目标可能具有不同的形状。 三、算法分类 基于深度学习的目标检测算法主要分为两大类: Two-stage算法:先进行区域生成(Region Proposal),生成有可能包含待检物体的预选框(Region Proposal),再通过卷积神经网络进行样本分类。常见的Two-stage算法包括R-CNN、Fast R-CNN、Faster R-CNN等。 One-stage算法:不用生成区域提议,直接在网络中提取特征来预测物体分类和位置。常见的One-stage算法包括YOLO系列(YOLOv1、YOLOv2、YOLOv3、YOLOv4、YOLOv5等)、SSD和RetinaNet等。 四、算法原理 以YOLO系列为例,YOLO将目标检测视为回归问题,将输入图像一次性划分为多个区域,直接在输出层预测边界框和类别概率。YOLO采用卷积网络来提取特征,使用全连接层来得到预测值。其网络结构通常包含多个卷积层和全连接层,通过卷积层提取图像特征,通过全连接层输出预测结果。 五、应用领域 目标检测技术已经广泛应用于各个领域,为人们的生活带来了极大的便利。以下是一些主要的应用领域: 安全监控:在商场、银行
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值