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

推荐订阅源

S
Schneier on Security
J
Java Code Geeks
小众软件
小众软件
WordPress大学
WordPress大学
T
Tailwind CSS Blog
雷峰网
雷峰网
美团技术团队
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
腾讯CDC
The Cloudflare Blog
博客园 - Franky
罗磊的独立博客
V
Visual Studio Blog
宝玉的分享
宝玉的分享
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 【当耐特】
博客园_首页
V
V2EX
博客园 - 三生石上(FineUI控件)
Jina AI
Jina AI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
S
SegmentFault 最新的问题
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
S
Security Affairs
C
Cybersecurity and Infrastructure Security Agency CISA
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
月光博客
月光博客
P
Palo Alto Networks Blog
L
Lohrmann on Cybersecurity
爱范儿
爱范儿
酷 壳 – CoolShell
酷 壳 – CoolShell
大猫的无限游戏
大猫的无限游戏
人人都是产品经理
人人都是产品经理
C
CERT Recently Published Vulnerability Notes
Hugging Face - Blog
Hugging Face - Blog
Attack and Defense Labs
Attack and Defense Labs
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
T
Troy Hunt's Blog
博客园 - 司徒正美
博客园 - 叶小钗
Last Week in AI
Last Week in AI
Google DeepMind News
Google DeepMind News
Simon Willison's Weblog
Simon Willison's Weblog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Privacy International News Feed
IT之家
IT之家
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Help Net Security
Help Net Security
NISL@THU
NISL@THU
H
Hacker News: Front Page

博客园 - 夜飞

滚动DATAGRID数据 和股票一样滚动 vs.2005 中对COOKIES 加密解密 例子 - 夜飞 asp.net(c#) datelist DataGrid 中截取字符串加"..." 和 鼠标放上去字符全部显示 - 夜飞 vs.2005 比较时间大小 可精确到秒 - 夜飞 VS.2005 子窗体提交数据刷新父窗体 GRIDVIEW 子窗体(2) VS.2005 子窗体提交数据刷新父窗体 GRIDVIEW 父窗体(1) - 夜飞 gridview中加弹出窗口用例 创建数据库连接对象 ASP.NET程序员笔试最常见问题 海量数据库的查询优化及分页算法方案 表格列头加菜单(datagrid) 实现无刷新DropdownList联动效果 实现的不让同一个用户登陆 asp.net打印 Web打印控制技术的几种方案: ASP.NET中弹出窗口技术 在asp.net中读取XML文件信息的4种方法 Asp.Net 动态生成验证码 ----- 把DataGrid1的一列的统计显示在页脚
GridView控件修改、删除示例(修改含有DropDownList控件)
夜飞 · 2006-07-12 · via 博客园 - 夜飞
 

GridView控件修改、删除示例(修改含有DropDownList控件)

Posted on 2006-05-16 10:54 星期零 阅读(160) 评论(0)  编辑 收藏 收藏至365Key 所属分类: Asp.Net2.0

GridView控件修改、删除例子,修改时含有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="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("xueliText")%>
                                    
</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 *,case xueli when '1' then '博士' when '2' then '硕士' when '3' then '学士' else '' end as xueliText FROM test01 where id<1000 and 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 的值,也可以取得数据库中的数据绑定
            ddlxueli.Items.Clear();
            ddlxueli.Items.Add(
new ListItem("博士", "1"));
            ddlxueli.Items.Add(
new ListItem("硕士", "2"));
            ddlxueli.Items.Add(
new ListItem("学士", "3"));
            
//

            //  

选中 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