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

推荐订阅源

Jina AI
Jina AI
T
The Blog of Author Tim Ferriss
G
Google Developers Blog
P
Privacy & Cybersecurity Law Blog
SecWiki News
SecWiki News
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Hacker News: Ask HN
Hacker News: Ask HN
Engineering at Meta
Engineering at Meta
Hugging Face - Blog
Hugging Face - Blog
PCI Perspectives
PCI Perspectives
www.infosecurity-magazine.com
www.infosecurity-magazine.com
T
The Exploit Database - CXSecurity.com
博客园_首页
Latest news
Latest news
T
Threat Research - Cisco Blogs
N
News and Events Feed by Topic
TaoSecurity Blog
TaoSecurity Blog
罗磊的独立博客
月光博客
月光博客
C
Cisco Blogs
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
V
V2EX
Last Week in AI
Last Week in AI
Cloudbric
Cloudbric
MyScale Blog
MyScale Blog
Spread Privacy
Spread Privacy
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
P
Proofpoint News Feed
G
GRAHAM CLULEY
N
News and Events Feed by Topic
MongoDB | Blog
MongoDB | Blog
N
Netflix TechBlog - Medium
M
MIT News - Artificial intelligence
I
InfoQ
Microsoft Security Blog
Microsoft Security Blog
人人都是产品经理
人人都是产品经理
K
Kaspersky official blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
量子位
I
Intezer
Microsoft Azure Blog
Microsoft Azure Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
A
About on SuperTechFans
D
DataBreaches.Net
宝玉的分享
宝玉的分享
S
Security @ Cisco Blogs
Help Net Security
Help Net Security
Hacker News - Newest:
Hacker News - Newest: "LLM"

博客园 - 默默無語中

类与结构的区别 GridView分页-编辑-删除 弄了一晚上研究连接字符串,给自己留个清楚的回忆~! - 默默無語中 - 博客园 [数据库连接字符串] SQL Server 2005 Compact Edition 连接字符串 委托对象(收集) 委托的笔记(整理) [导入]asp.net高级教程(三)-实战篇 [导入]asp.net高级教程(三)-对象 [导入]asp.net高级教程(五)-实战篇(中) [导入].NET之ASPWebApplication快速入门(3) [导入].NET之ASPWebApplication快速入门(2) [导入].NET之ASPWebApplication快速入门(1) [导入].NET之ASPWebApplication快速入门(4) [导入].NET之ASPWebApplication快速入门(5) [导入]asp.NET特写 [导入]亲密接触ASP.Net(1) [导入]亲密接触ASP.Net(3) [导入]亲密接触ASP.Net(4) [导入]亲密接触ASP.Net(2) [导入]亲密接触ASP.Net(6) [导入]亲密接触ASP.Net(5)
DataList修改删除 - 默默無語中 - 博客园
默默無語中 · 2008-06-06 · via 博客园 - 默默無語中

aspx网页代码:

<asp:DataList ID="DataList1" runat="server" DataKeyField ="ProductKey" 
        CaptionAlign
="Bottom" CellPadding="4" EnableTheming="True" 
        ForeColor
="#333333" 
        onselectedindexchanged
="DataList1_SelectedIndexChanged" RepeatColumns="4" 
        RepeatDirection
="Horizontal" oncancelcommand="DataList1_CancelCommand" 
        ondeletecommand
="DataList1_DeleteCommand" 
        oneditcommand
="DataList1_EditCommand" ondatabinding="DataList1_DataBinding" 
        onitemcreated
="DataList1_ItemCreated" 
        onupdatecommand
="DataList1_UpdateCommand" Font-Bold="False" Font-Italic="False" 
        Font
-Overline="False" Font-Strikeout="False" Font-Underline="False" 
        
>
        
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        
<AlternatingItemStyle BackColor="White" 
            ForeColor
="#284775" />
        
<ItemStyle BackColor="#F7F6F3" 
            ForeColor
="#333333" />
        
<EditItemTemplate>
            序号:
<asp:TextBox ID="textnum" runat="server" Text='<%# Eval("ProductKey") %>'></asp:TextBox>
            
<br />
            时间:
<asp:TextBox ID="texttime" runat="server" 
                Text
='<%# Eval("ProductAlternateKey") %>'></asp:TextBox>
            
<br />
            
<asp:LinkButton ID="savebtn" runat="server" CommandName="update">保存</asp:LinkButton>
            
&nbsp;
            
<asp:LinkButton ID="LinkButton4" runat="server" CommandName="cancel">取消</asp:LinkButton>
        
</EditItemTemplate>
        
<SelectedItemStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        
<HeaderTemplate>
            演示头模板
        
</HeaderTemplate>
        
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        
<FooterTemplate>
            脚模板
        
</FooterTemplate>
        
<SelectedItemTemplate > 序号:<asp:Label ID="num" runat="server" Text='<%# Eval("ProductKey") %>'></asp:Label>
            
<br />
            时间:
<asp:Label ID="time" runat="server" 
                Text
='<%# Eval("ProductAlternateKey") %>'></asp:Label>
            
<br />
            数字:
<asp:Label ID="Label3" runat="server" Text='<%# Eval("ReorderPoint") %>'></asp:Label>
            
<br /></SelectedItemTemplate>
        
<ItemTemplate>
            序号:
<asp:Label ID="num" runat="server" Text='<%# Eval("ProductKey") %>'></asp:Label>
            
<br />
            时间:
<asp:Label ID="time" runat="server" 
                Text
='<%# Eval("ProductAlternateKey") %>'></asp:Label>
            
<br />
            
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="edit">修改</asp:LinkButton>
            
&nbsp;&nbsp;
            
<asp:LinkButton ID="delebtn" runat="server" CommandName="delete">删除</asp:LinkButton>
            
&nbsp;
        
</ItemTemplate>
    
</asp:DataList>

cs代码文件:

using System;
using System.Data;
using System.Configuration;
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 _Default : System.Web.UI.Page
{
    
private void  con()
    
{
        
string connstring = ConfigurationManager.ConnectionStrings["AdventureWorksDWConnectionString"].ConnectionString;
        SqlConnection con 
= new SqlConnection(connstring);
        SqlConnection conn 
= new SqlConnection();
        DataSet ds 
= new DataSet();
        SqlDataAdapter sda 
= new SqlDataAdapter("select * from DimProduct", con);
        sda.Fill(ds, 
"name");
        
//SqlDataAdapter sda2 = new SqlDataAdapter("select * from ProspectiveBuyer", con);
        
//sda2.Fill(ds, "title");
        PagedDataSource pds = new PagedDataSource();
        pds.DataSource 
= ds.Tables["name"].DefaultView;
        
//PagedDataSource aa = new PagedDataSource();
        pds.AllowPaging = true;//允许分页
  DataList1.DataSource = pds;
        DataList1.DataBind();

    }


    
protected void Page_Load(object sender, EventArgs e)
    
{
        
if (!IsPostBack)
        
{
            con();
        }


    }

    
protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
    
{
       
        DataList1.EditItemIndex 
= e.Item.ItemIndex;
        con();

    }

    
protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
    
{
        DataList1.EditItemIndex 
= -1;
        con();
    }

    
protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)
    
{
        
string id = Convert.ToString(DataList1.DataKeys[e.Item.ItemIndex]);
        
string sqlcmd = "delete from DimProduct where ProductKey='" + id + "'";
            
string connstring = ConfigurationManager.ConnectionStrings["AdventureWorksDWConnectionString"].ConnectionString;
            SqlConnection conn 
= new SqlConnection(connstring);
            SqlCommand cmd 
= new SqlCommand(sqlcmd, conn);
            conn.Open();
            cmd.ExecuteNonQuery();
            con();
      }

    
protected void DataList1_DataBinding(object sender, EventArgs e)
    
{

    }

    
protected void DataList1_ItemCreated(object sender, DataListItemEventArgs e)
    
{

        
switch (e.Item.ItemType)
        
{
            
case ListItemType.Item:
            
case ListItemType.AlternatingItem:
            
case ListItemType.SelectedItem:
                ((LinkButton)(e.Item.FindControl(
"delebtn"))).Attributes.Add(
                    
"onclick"" return confirm('你确认要删除');");
                
break;
        }

    }

    
protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
    
{
        
string id =Convert.ToString(DataList1.DataKeys[e.Item.ItemIndex]);
        
string num = ((TextBox)e.Item.FindControl("textnum")).Text;
        
string time = ((TextBox)e.Item.FindControl("texttime")).Text;
        
string sqlcmd = "update DimProduct set  ProductAlternateKey='" + time + "'where ProductKey='" + id + "'";
        
string connstring = ConfigurationManager.ConnectionStrings["AdventureWorksDWConnectionString"].ConnectionString;
        SqlConnection conn 
= new SqlConnection(connstring);
        SqlCommand cmd 
= new SqlCommand(sqlcmd, conn);
        conn.Open();
        cmd.ExecuteNonQuery();
        
this.DataList1.EditItemIndex = -1;
        con();
    }

}