惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Threat Research - Cisco Blogs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
V
Vulnerabilities – Threatpost
GbyAI
GbyAI
P
Proofpoint News Feed
L
LINUX DO - 热门话题
P
Palo Alto Networks Blog
A
About on SuperTechFans
T
Tenable Blog
M
MIT News - Artificial intelligence
IT之家
IT之家
I
Intezer
D
DataBreaches.Net
爱范儿
爱范儿
T
Threatpost
C
CERT Recently Published Vulnerability Notes
云风的 BLOG
云风的 BLOG
博客园 - 三生石上(FineUI控件)
WordPress大学
WordPress大学
K
Kaspersky official blog
大猫的无限游戏
大猫的无限游戏
A
Arctic Wolf
Y
Y Combinator Blog
Cyberwarzone
Cyberwarzone
酷 壳 – CoolShell
酷 壳 – CoolShell
D
Darknet – Hacking Tools, Hacker News & Cyber Security
H
Help Net Security
Microsoft Security Blog
Microsoft Security Blog
Spread Privacy
Spread Privacy
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
AWS News Blog
AWS News Blog
博客园 - 聂微东
C
Check Point Blog
S
Securelist
有赞技术团队
有赞技术团队
雷峰网
雷峰网
aimingoo的专栏
aimingoo的专栏
Last Week in AI
Last Week in AI
Stack Overflow Blog
Stack Overflow Blog
MongoDB | Blog
MongoDB | Blog
D
Docker
G
GRAHAM CLULEY
T
The Exploit Database - CXSecurity.com
C
Cybersecurity and Infrastructure Security Agency CISA
T
Tailwind CSS Blog
L
Lohrmann on Cybersecurity
G
Google Developers Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
L
LangChain Blog

博客园 - 花****半

lr三大组件关系图 C#-----属性和索引器 C#----继承 实验1----GridView 增删改查 开发实训 发生类型为 System.OutOfMemoryException 的异常。请问这是什么原因 进度条的问题 学习ajax-二级联动下拉列表 - 花****半 - 博客园 SharpZipLib 怎么提高压缩比? 2月14号 句话是什么意思?if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[PerPer SQL Server中的sysobjects” 是什么意思 在sysobjects表中,xtype=rf ASP好东西!所以 —共享 target 怎么把一个页面的数据传到另一个页面? 提示以下的错误信息:“未能在设计视图中打开,"" 块中,以不同方式将值括起来 ” 什么问题这是? split
实验2 -DataList 增删改查等操作
花****半 · 2009-03-25 · via 博客园 - 花****半
 

具体效果如下所示:

  1. 使用Datalist显示数据,
  2. 新增数据,
  3. 修改
  4. 删除数据等操作
  5. 主要设置下面两个属性修改显示的外观。

6:在编辑模板列中,寻找事件选项,生成相关事件。并注意拖入的按钮的CommandName要改为delete,update,cancel等。

 全例

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Data.SqlClient;

public partial class xin : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

       Page.ti

        if (!Page.IsPostBack)

        {

            Bind();

        }

    }

    /// <summary>

    /// 数据绑定

    /// </summary>

    public void Bind()

    {

        string strCon = "server=.;database=studentTable;pwd=123;user id=sa";

        SqlConnection con = new SqlConnection(strCon);

        con.Open();

        string strSql = "select * from stuinfo";

        SqlDataAdapter da = new SqlDataAdapter(strSql, con);

        DataSet ds = new DataSet();

        da.Fill(ds);

        PagedDataSource ps = new PagedDataSource();

        ps.AllowPaging = true;

        ps.DataSource = ds.Tables[0].DefaultView;

        int curpage ;

        if (Request.QueryString["page"] != null)

        {

            curpage = Convert.ToInt32(Request.QueryString["page"]);

        }

        else

        {

            curpage = 1;

        }

        ps.CurrentPageIndex = curpage - 1;

        ps.PageSize = 2;

        int count = ds.Tables[0].DefaultView.Count / ps.PageSize;

        if (ds.Tables[0].DefaultView.Count % ps.PageSize != 0)

        {

            count++;

        }

        this.Label5.Text = "当前页:" + curpage.ToString() + 共" + count.ToString() + "页";

        this.HyperLink3.NavigateUrl = Request.CurrentExecutionFilePath;

        if (!ps.IsFirstPage)

        {

            this.HyperLink1.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + (curpage - 1);

        }

        if (!ps.IsLastPage)

        {

            this.HyperLink2.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + (curpage + 1);

        }

        this.HyperLink4.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" +count;

        this.DataList1.DataSource = ps;

        this.DataList1.DataBind();

        con.Close();

    }

    /// <summary>

    /// 编辑

    /// </summary>

    /// <param name="source"></param>

    /// <param name="e"></param>

    protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)

    {

        this.DataList1.EditItemIndex = e.Item.ItemIndex;

        Bind();

    }

    /// <summary>

    /// 取消

    /// </summary>

    /// <param name="source"></param>

    /// <param name="e"></param>

    protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)

    {

        this.DataList1.EditItemIndex = -1;

    }

    /// <summary>

    /// 更新

    /// </summary>

    /// <param name="source"></param>

    /// <param name="e"></param>

    protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)

    {

        string strName = ((TextBox)this.DataList1.Items[e.Item.ItemIndex].FindControl("TextBox2")).Text.ToString();

        string strSq = "update stuinfo set name ='"+strName+"' where id= "+this.DataList1.DataKeys[e.Item.ItemIndex].ToString()+"";

        string strCon = "server=.;database=studentTable;pwd=123;user id=sa";

        SqlConnection con = new SqlConnection(strCon);

        con.Open();

        SqlCommand com = new SqlCommand(strSq,con);

        com.ExecuteNonQuery();

        this.DataList1.EditItemIndex = -1;

        con.Close();

        Bind();

    }

    /// <summary>

    /// 删除后绑定

    /// </summary>

    /// <param name="source"></param>

    /// <param name="e"></param>

    protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)

    {

        string strSq = "delete stuinfo where id= " + this.DataList1.DataKeys[e.Item.ItemIndex].ToString() + "";

        string strCon = "server=.;database=studentTable;pwd=123;user id=sa";

        SqlConnection con = new SqlConnection(strCon);

        con.Open();

        SqlCommand com = new SqlCommand(strSq, con);

        com.ExecuteNonQuery();

        con.Close();

        Bind();

    }

    protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)

    {

        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)

        {

            e.Item.Attributes.Add("onmouseover", "c=this.stle.backgroundColor,this.style.backgroundColor='#223344'");

            e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor=c");

       (( Button)e.Item.FindControl("Button4")).Attributes.Add("onclick","return confirm('是否删除')");

        }

    }

    protected void Button1_Click(object sender, EventArgs e)

    {

        string str = "1/" + this.FileUpload1.FileName;

        string strPath = Server.MapPath( str);

        FileUpload1.PostedFile.SaveAs(strPath);

        string strSq = "insert into stuinfo(name,sex,age,pwd,image)";

         strSq += "values('"+this.TextBox1.Text+"',"+this.RadioButtonList1.SelectedIndex+",'"+this.TextBox3.Text+"','"+this.TextBox4.Text+"','"+str+"') ";

        string strCon = "server=.;database=studentTable;pwd=123;user id=sa";

        SqlConnection con = new SqlConnection(strCon);

        con.Open();

        SqlCommand com = new SqlCommand(strSq, con);

        com.ExecuteNonQuery();

        con.Close();

        Bind();

    }

}