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

本文详细介绍了如何在ASP.NET中使用GridView控件进行数据的修改和删除操作,包括与DropDownList控件的结合使用。示例代码展示了GridViewUp.aspx文件的关键实现,帮助开发者理解数据绑定和事件处理过程。
摘要由CSDN通过智能技术生成
  <script language="javascript" type="text/javascript"> </script>
<script type="text/javascript"> </script> <script src="/WebResource.axd?d=VT-DPrLTYP31jDF3TQ1B-w2&t=632963535947587500" type="text/javascript"></script> <script src="/WebResource.axd?d=1wkz58Z1rsBL-eFHJPld5ERhyO-4qXtUzHp7Q1KwrOE1&t=632963535947587500" type="text/javascript"></script> <script type="text/javascript"> </script>

xzr2004(只有偏执狂才能生存)的专栏

乘风破浪会有时,直挂云帆济沧海

  CSDN首页 |  Blog |  我的Blog |  后台管理 论坛 |  文档 |  档案 |  联系 |  RSS订阅  |  搜索 | 
   5篇原创: 0篇翻译: 5篇转载: 649次点击: 0个评论: 0个Trackbacks
<script language="javascript" src="http://tag.csdn.net/urltag.aspx" type="text/javascript"></script>
<script type="text/javascript">function StorePage(){d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(keyit=window.open('http://www.365key.com/storeit.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes'));keyit.focus();}</script>

 

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();
        }

    }

}



Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1381537


<script src="http://localhost:82/PromoteIcon.aspx?id=1381537" type="text/javascript"></script> [ 收藏到我的网摘]   似水年华发表于 2006年11月13日 14:07:00
href="http://blog.csdn.net/xzr2004/Services/Pingback.aspx" rel="pingback" /> <script type="text/javascript">function hide(){showComment();}</script>

<script language="javascript" type="text/javascript"> ad_width=468; ad_height=60; adcss=2; unionuser=19; </script> <script language="javascript" src="http://tagegg.csdn.net/showads.js" type="text/javascript"> </script> <script language="JavaScript1.1" src="http://tagegg.csdn.net/a.aspx?action=displayad&unionuser=19&unionurl=http%3A%2F%2Fblog.csdn.net%2Fxzr2004%2Farchive%2F2006%2F11%2F13%2F1381537.aspx&adcss=2&ad_type=c&width=468&height=60&ad_color=&ad_color_border=&count=3" type="text/javascript"></script> href="http://tagegg.csdn.net/css/csdn_zhaigao_style.css" type="text/css" rel="stylesheet" />
<script type="text/javascript">document.write(" ");</script> 没有评论。

发表评论

  
大名:
网址:
评论 
验证码 VerifyCode <script src="/scripts/deferShowVerifyImage.js" type="text/javascript"></script>
   
<script src="http://www.csdn.net/common/counter.js" type="text/javascript"></script> id="myframe" border="0" name="myframe" src="http://www.csdn.net/ggmm/dd333.htm" frameborder="no" width="0" scrolling="no" height="0"> <script type="text/javascript"> </script> <script type="text/javascript"> </script>
<script type="text/javascript"> </script>
<script language="javascript" type="text/javascript"> </script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值