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

推荐订阅源

Martin Fowler
Martin Fowler
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
A
About on SuperTechFans
Apple Machine Learning Research
Apple Machine Learning Research
The Register - Security
The Register - Security
Vercel News
Vercel News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
人人都是产品经理
人人都是产品经理
MyScale Blog
MyScale Blog
云风的 BLOG
云风的 BLOG
博客园_首页
U
Unit 42
T
Tailwind CSS Blog
G
GRAHAM CLULEY
F
Full Disclosure
V
Vulnerabilities – Threatpost
T
Tenable Blog
月光博客
月光博客
P
Privacy & Cybersecurity Law Blog
P
Privacy International News Feed
K
Kaspersky official blog
Scott Helme
Scott Helme
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
N
News and Events Feed by Topic
T
The Exploit Database - CXSecurity.com
N
News and Events Feed by Topic
有赞技术团队
有赞技术团队
Recent Commits to openclaw:main
Recent Commits to openclaw:main
L
LINUX DO - 最新话题
Recorded Future
Recorded Future
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Help Net Security
Help Net Security
The GitHub Blog
The GitHub Blog
Cisco Talos Blog
Cisco Talos Blog
SecWiki News
SecWiki News
P
Proofpoint News Feed
Security Latest
Security Latest
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
罗磊的独立博客
S
Security Affairs
M
MIT News - Artificial intelligence
L
LINUX DO - 热门话题
美团技术团队
Simon Willison's Weblog
Simon Willison's Weblog
T
Threat Research - Cisco Blogs
Stack Overflow Blog
Stack Overflow Blog
Forbes - Security
Forbes - Security
Hugging Face - Blog
Hugging Face - Blog
博客园 - Franky
V
Visual Studio Blog

博客园 - 冷火

Lucene.Net学习 Argotic Syndication Framework生成RSS - 冷火 Confirm GridView Deletes with the ModalPopupExtender 如何在C#中实现图片缩放 [ASP.NET] 限制上传文件类型的两种方法 asp.net采集 在C#怎用一条正则表达式验证用逗号隔开的email地址 关于LumiSoft.Net.POP3.Client接收邮件例子(包括附件) dhtmlXTreeprofessional asp.net导出xml文件 - 冷火 - 博客园 DateDiff Datagridview下一行下一行 局域网QQ第三版(V1.4) asp.net+JSON+AJAX(基于prototype1.4)做无刷新的2级DropDownList - 冷火 - 博客园 非常好的菜单效果(Accordion风格) 静功解决失眠的问题 ASP.NET中文验证码详解 log4net Config Examples 对XAML进行编辑的辅助类(XamlHelper)
GridView控件修改、删除示例(修改含有DropDownList控件) - 冷火 - 博客园
冷火 · 2009-03-14 · via 博客园 - 冷火

ridView控件修改、删除例子,修改时含有DropDownList控件。
示例运行效果图:

GridViewUp.aspx文件代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridViewUp.aspx.cs" Inherits="gridview_GridViewUp" %>

<!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>
</head>
<body>
    
<form id="form1" runat="server">
        
<div>
            
<table cellpadding="0" cellspacing="0" border="0" width="80%" style="font-size: 11px">
                
<tr>
                    
<td align="center">
                        
<asp:GridView ID="GridView1" runat="server" Width="100%" CellPadding="4" ForeColor="#333333"
                            AutoGenerateColumns
="False" AllowPaging="True" PageSize="12" OnRowCancelingEdit="GridView1_RowCancelingEdit"
                            OnRowEditing
="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnRowDeleting="GridView1_RowDeleting"
                            DataKeyNames
="id,name" OnPageIndexChanging="GridView1_PageIndexChanging" DataMember="card,price" OnRowDataBound="GridView1_RowDataBound" GridLines="None">
                            
<Columns>
                                
<asp:BoundField HeaderText="身份证号" DataField="card" Visible=false />
                                
<asp:BoundField HeaderText="编号" DataField="id" ReadOnly="True" />
                                
<asp:BoundField DataField="name" HeaderText="姓名" ReadOnly="True" />
                                
<asp:TemplateField HeaderText="身份证号">
                                    
<ItemTemplate>
                                        
<%Eval("card"%>
                                    
</ItemTemplate>
                                    
<EditItemTemplate>
                                        
<asp:TextBox ID="TBCard" Text='<%# Eval("card") %>' runat="server" Width="140px" />
                                    
</EditItemTemplate>
                                    
<ItemStyle Width="150px" />
                                
</asp:TemplateField>
                                
<asp:TemplateField HeaderText="学历">
                                    
<ItemTemplate>
                                        
<%Eval("description")%>
                                    
</ItemTemplate>
                                    
<EditItemTemplate>
                                        
<asp:HiddenField ID="HDFXueli" runat="server" Value='<%# Eval("xueli") %>' />
                                        
<asp:DropDownList ID="DDLXueli" runat="server" Width="90px" />
                                    
</EditItemTemplate>
                                    
<ItemStyle Width="100px" />
                                
</asp:TemplateField>
                                
<asp:TemplateField HeaderText="价格">
                                    
<ItemTemplate>
                                        
<%Eval("price"%>
                                    
</ItemTemplate>
                                    
<EditItemTemplate>
                                        
<asp:TextBox ID="TBPrice" Text='<%# Eval("price") %>' runat="server" Width="90px" />
                                    
</EditItemTemplate>
                                    
<ItemStyle Width="100px" />
                                
</asp:TemplateField>
                                
<asp:BoundField HeaderText="建立时间" DataField="createdate" ReadOnly="True" />
                                
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" HeaderText="操作" />
                            
</Columns>
                            
<PagerSettings FirstPageText="" LastPageText="" NextPageText="" PreviousPageText="" />
                            
<RowStyle Height="20px" BackColor="#F7F6F3" ForeColor="#333333" />
                            
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                            
<EditRowStyle BackColor="#999999" />
                            
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                            
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                            
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                            
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                        
</asp:GridView>
                    
</td>
                
</tr>
            
</table>
        
</div>
    
</form>
</body>
</html>

GridViewUp.aspx.cs文件代码:

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 gridview_GridViewUp : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
if (!IsPostBack)
        
{
            GridViewBind();
        }

    }


    
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    
{
        GridView1.PageIndex 
= e.NewPageIndex;
        GridViewBind();
    }


    
private void GridViewBind()
    
{
        
string connStr = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
        
string SqlStr = "SELECT a.*,b.description FROM test01 a,xueli b where a.xueli=b.code and a.id<1000 and a.id>200";
        DataSet ds 
= new DataSet();

        
try
        
{
            SqlConnection conn 
= new SqlConnection(connStr);
            
if (conn.State.ToString() == "Closed") conn.Open();
            SqlDataAdapter da 
= new SqlDataAdapter(SqlStr, conn);
            da.Fill(ds, 
"test01");
            
if (conn.State.ToString() == "Open") conn.Close();

            GridView1.DataSource 
= ds.Tables[0].DefaultView;
            GridView1.DataBind();
        }

        
catch (Exception ex)
        
{
            Response.Write(
"数据库错误,错误原因:" + ex.Message);
            Response.End();
        }

    }


    
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    
{
        
if (((DropDownList)e.Row.FindControl("DDLXueli")) != null)
        
{        
            DropDownList ddlxueli 
= (DropDownList)e.Row.FindControl("DDLXueli");

            
//  生成 DropDownList 的值,绑定数据
            string connStr = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
            
string SqlStr = "SELECT * from xueli";
            DataSet ds 
= new DataSet();

            SqlConnection conn 
= new SqlConnection(connStr);
            
if (conn.State.ToString() == "Closed") conn.Open();
            SqlDataAdapter da 
= new SqlDataAdapter(SqlStr, conn);
            da.Fill(ds, 
"xueli");
            
if (conn.State.ToString() == "Open") conn.Close();

            ddlxueli.DataSource 
= ds.Tables[0].DefaultView;
            ddlxueli.DataTextField 
= "description";
            ddlxueli.DataValueField 
= "code";
            ddlxueli.DataBind();
            
//

            
//  选中 DropDownList
            ddlxueli.SelectedValue = ((HiddenField)e.Row.FindControl("HDFXueli")).Value;
            
//
        }

    }


    
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    
{
        GridView1.EditIndex 
= e.NewEditIndex;
        GridViewBind();
    }

    
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    
{
        GridView1.EditIndex 
= -1;
        GridViewBind();
    }


    
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    
{
        
string id = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
        
string card = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TBCard")).Text;
        
string xueli = ((DropDownList)GridView1.Rows[e.RowIndex].FindControl("DDLXueli")).SelectedValue;
        
string price = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TBPrice")).Text;

        
string connStr = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
        
string SqlStr = "update test01 set card='" + card + "',xueli='" + xueli + "',price='" + price + "' where id=" + id;

        
try
        
{
            SqlConnection conn 
= new SqlConnection(connStr);
            
if (conn.State.ToString() == "Closed") conn.Open();
            SqlCommand comm 
= new SqlCommand(SqlStr, conn);
            comm.ExecuteNonQuery();
            comm.Dispose();
            
if (conn.State.ToString() == "Open") conn.Close();

            GridView1.EditIndex 
= -1;
            GridViewBind();
        }

        
catch (Exception ex)
        
{
            Response.Write(
"数据库错误,错误原因:" + ex.Message);
            Response.End();
        }

    }


    
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    
{
        
string id = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
        
string connStr = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
        
string SqlStr = "delete from test01 where id=" + id;

        
try
        
{
            SqlConnection conn 
= new SqlConnection(connStr);
            
if (conn.State.ToString() == "Closed") conn.Open();
            SqlCommand comm 
= new SqlCommand(SqlStr, conn);
            comm.ExecuteNonQuery();
            comm.Dispose();
            
if (conn.State.ToString() == "Open") conn.Close();

            GridView1.EditIndex 
= -1;
            GridViewBind();
        }

        
catch (Exception ex)
        
{
            Response.Write(
"数据库错误,错误原因:" + ex.Message);
            Response.End();
        }

    }

}

sql server2000生成表代码:

CREATE TABLE [dbo].[test01] (
    
[id] [decimal](180IDENTITY (11NOT NULL ,
    
[name] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
    
[card] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
    
[xueli] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
    
[price] [decimal](180NULL ,
    
[createdate] [datetime] NULL 
ON [PRIMARY]
GOALTER TABLE [dbo].[test01] ADD 
    
CONSTRAINT [DF_test01_createdate] DEFAULT (getdate()) FOR [createdate],
    
CONSTRAINT [PK_test01] PRIMARY KEY  CLUSTERED 
    (
        
[id]
    )  
ON [PRIMARY] 
GO

CREATE TABLE [dbo].[xueli](
    
[id] [int] IDENTITY(1,1NOT NULL,
    
[code] [int] NULL,
    
[description] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,
 
CONSTRAINT [PK_xueli] PRIMARY KEY CLUSTERED 
(
    
[id] ASC
)
WITH (IGNORE_DUP_KEY = OFFON [PRIMARY]
ON [PRIMARY]

xueli表中的数据:

id    code    description
1    1    博士
2    2    硕士
3    3    学士