1.html

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridView_DeleteUpdate2.aspx.cs"
    Inherits
="GridView_GridView_DeleteUpdate2" MaintainScrollPositionOnPostback="true" 
%>

<!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 id="Head1" runat="server">
    
<title>GridView基本的操作 编辑 更新 取消</title>
</head>
<body>
    
<form id="form1" runat="server">
        
<div>
            
<asp:GridView ID="GridView1" runat="server" Width="100%" AutoGenerateColumns="False" >
                
<Columns>
                    
<asp:TemplateField>
                        
<HeaderTemplate>
                            User_ID
</HeaderTemplate>
                        
<ItemTemplate>
                            
<asp:Label ID="Lbl_UserID" Text='<%#Eval("User_ID") %>' runat="server"></asp:Label>
                        
</ItemTemplate>
                        
<EditItemTemplate>
                            
<asp:Label ID="Lbl_UserID" Text='<%#Eval("User_ID") %>' runat="server"></asp:Label>
                        
</EditItemTemplate>
<%--                        <FooterTemplate>
                            
<asp:Label ID="Lbl_UserID" Text='<%#Eval("User_ID") %>' runat="server"></asp:Label>
                        </FooterTemplate>--
%>
                    
</asp:TemplateField>
                    
                    
<asp:TemplateField>
                        
<HeaderTemplate>
                            User_Email
</HeaderTemplate>
                        
<ItemTemplate>
                            
<asp:Label ID="Lbl_UserEmail" Text='<%#Eval("User_Email") %>' runat="server"></asp:Label>
                        
</ItemTemplate>
                        
<EditItemTemplate>
                            
<asp:TextBox ID="Txt_UserEmail" Text='<%#Eval("User_Email") %>' runat="server"></asp:TextBox>
                        
</EditItemTemplate>
<%--                        <FooterTemplate>
                            
<%#Eval("User_Email")
%>
                        
</FooterTemplate>--%>
                    
</asp:TemplateField>
                    
                    
<asp:TemplateField>
                        
<HeaderTemplate>
                            DaLei_Desc
</HeaderTemplate>
                        
<ItemTemplate>
                          
<asp:Label ID="Lbl_DaLeiDesc" Text='<%#Eval("DaLei_Desc")%>' runat="server"></asp:Label>
                           
<input id="hidden_DaLeiDesc" type="hidden" value='<%#Eval("DaLei_ID")%>' runat="server" />
                        
</ItemTemplate>
                        
<EditItemTemplate>
                            
<asp:DropDownList ID="DropDownList_DaLeiDesc" runat="server" AutoPostBack="true"
                                OnSelectedIndexChanged
="DropDownList_DaLeiDesc_SelectedIndexChanged">
                            
</asp:DropDownList>
                        
</EditItemTemplate>
      
<%--                  <FooterTemplate>
                            
<%#Eval("DaLei_Desc")
%>
                           
<input type="hidden" value="'<%#Eval("DaLei_ID")%>'" />
                        
</FooterTemplate>--%>
                    
</asp:TemplateField>
                    
<asp:TemplateField>
                        
<HeaderTemplate>
                            XiaoLei_Desc
</HeaderTemplate>
                        
<ItemTemplate>
                           
<asp:Label ID="Lbl_XiaoLeiDesc" Text='<%#Eval("XiaoLei_Desc")%>' runat="server"></asp:Label>
                           
<input id="hidden_XiaoLeiDesc" type="hidden" value='<%#Eval("XiaoLei_ID")%>' runat="server" />
                        
</ItemTemplate>
                        
<EditItemTemplate>
                            
<asp:DropDownList ID="DropDownList_XiaoLeiDesc" runat="server">
                                
<asp:ListItem Value="-1" Text="--请选择--"></asp:ListItem>
                            
</asp:DropDownList>
                        
</EditItemTemplate>
<%--                        <FooterTemplate>
                            
<%#Eval("XiaoLei_Desc")
%>
                           
<input type="hidden" value="'<%#Eval("XiaoLei_ID")%>'" />
                        
</FooterTemplate>--%>
                    
</asp:TemplateField>
                    
<asp:CommandField ShowEditButton="True" HeaderText="编辑" />
                    
<asp:CommandField ShowDeleteButton="True" HeaderText="删除" />
                
</Columns>
            
</asp:GridView>
        
</div>
    
</form>
</body>
</html>
2.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;

public partial class GridView_GridView_DeleteUpdate2 : System.Web.UI.Page
{
    Practice.BLL.T_User userbll 
= new Practice.BLL.T_User();
    Practice.Model.T_User usermodel 
= new Practice.Model.T_User();
    Practice.BLL.T_DaLei daleibll 
= new Practice.BLL.T_DaLei();
    Practice.BLL.T_XiaoLei xiaoleibll 
= new Practice.BLL.T_XiaoLei();
    
static int index = 0;//主要记录编辑的index行

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

        GridView1.RowDeleting 
+= new GridViewDeleteEventHandler(GridView1_RowDeleting);
        GridView1.RowEditing 
+= new GridViewEditEventHandler(GridView1_RowEditing);
        GridView1.RowCancelingEdit 
+= new GridViewCancelEditEventHandler(GridView1_RowCancelingEdit);
        GridView1.RowUpdating 
+= new GridViewUpdateEventHandler(GridView1_RowUpdating);
    }


    
/// <summary>
    
/// 功    能:GridView删除事件
    
/// 作  者:PUKE
    
/// 完成时间:2007-06-04
    
/// 版  权:pukesys@tom.com
    
/// </summary>
    
/// <param name="sender"></param>
    
/// <param name="e"></param>

    void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    
{
        
string User_ID = ((Label)GridView1.Rows[e.RowIndex].Cells[0].FindControl("Lbl_UserID")).Text.Trim();

        Response.Write(
"删除时:<br/>User_ID = " + User_ID);
        
//userbll.Delete(Convert.ToInt32(User_ID));
        DataBindGridView();//重新绑定数据源
    }


    
/// <summary>
    
/// 功    能:GridView编辑事件
    
/// 作  者:PUKE
    
/// 完成时间:2007-06-04
    
/// 版  权:pukesys@tom.com
    
/// </summary>
    
/// <param name="sender"></param>
    
/// <param name="e"></param>

    void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    
{
    
//    string DaLei_Desc = ((Label)GridView1.Rows[e.NewEditIndex].Cells[2].FindControl("Lbl_DaLeiDesc")).Text.Trim();
    
//    string DaLei_ID = ((HtmlInputHidden)GridView1.Rows[e.NewEditIndex].Cells[2].FindControl("hidden_DaLeiDesc")).Value.Trim();
        GridView1.EditIndex = e.NewEditIndex;
        index 
= GridView1.EditIndex;
        DataBindGridView();
        
//((DropDownList)GridView1.Rows[e.NewEditIndex].Cells[2].FindControl("DropDownList_DaLeiDesc")).SelectedItem.Text = DaLei_Desc;
        
//((DropDownList)GridView1.Rows[e.NewEditIndex].Cells[2].FindControl("DropDownList_DaLeiDesc")).SelectedValue = DaLei_ID;
        DataBindDropDownList((DropDownList)GridView1.Rows[e.NewEditIndex].Cells[0].FindControl("DropDownList_DaLeiDesc"));

        

    }

    
/// <summary>
    
/// 功    能:绑定DropDownList_DaLeiDesc
    
/// 作  者:PUKE
    
/// 完成时间:2007-06-04
    
/// 版  权:pukesys@tom.com
    
/// </summary>
    
/// <param name="DropDownList_DaLeiDesc"></param>

    private void DataBindDropDownList(DropDownList DropDownList_DaLeiDesc)
    
{
        DropDownList_DaLeiDesc.Items.Clear();
        DataSet ds 
= daleibll.GetList("");
        DropDownList_DaLeiDesc.DataSource 
= ds;
        DropDownList_DaLeiDesc.DataValueField 
= "DaLei_ID";
        DropDownList_DaLeiDesc.DataTextField 
= "DaLei_Desc";
        DropDownList_DaLeiDesc.DataBind();

        ListItem LI 
= new ListItem();
        LI.Text 
= "--请选择--";
        LI.Value 
= "-1";
        DropDownList_DaLeiDesc.Items.Insert(
0, LI);
    }


    
/// <summary>
    
/// 功    能:DropDownList_DaLeiDesc的SelectedIndexChanged事件,绑定DropDownList_XiaoLeiDesc
    
/// 作  者:PUKE
    
/// 完成时间:2007-06-04
    
/// 版  权:pukesys@tom.com
    
/// </summary>
    
/// <param name="sender"></param>
    
/// <param name="e"></param>

    protected void DropDownList_DaLeiDesc_SelectedIndexChanged(object sender, EventArgs e)
    
{
        DropDownList DropDownList_XiaoLeiDesc1 
= new DropDownList();
        DropDownList_XiaoLeiDesc1 
= (DropDownList)GridView1.Rows[index].FindControl("DropDownList_XiaoLeiDesc");

        DropDownList_XiaoLeiDesc1.Items.Clear();
        DataSet ds 
= xiaoleibll.GetList(Convert.ToInt32(((DropDownList)sender).SelectedValue));
        DropDownList_XiaoLeiDesc1.DataSource 
= ds;
        DropDownList_XiaoLeiDesc1.DataValueField 
= "XiaoLei_ID";
        DropDownList_XiaoLeiDesc1.DataTextField 
= "XiaoLei_Desc";
        DropDownList_XiaoLeiDesc1.DataBind();

        ListItem LI 
= new ListItem();
        LI.Text 
= "--请选择--";
        LI.Value 
= "-1";
        DropDownList_XiaoLeiDesc1.Items.Insert(
0, LI);
    }


    
/// <summary>
    
/// 功    能:GridView取消事件
    
/// 作  者:PUKE
    
/// 完成时间:2007-06-04
    
/// 版  权:pukesys@tom.com
    
/// </summary>
    
/// <param name="sender"></param>
    
/// <param name="e"></param>

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


    
/// <summary>
    
/// 功    能:GridView更新事件
    
/// 作  者:PUKE
    
/// 完成时间:2007-06-04
    
/// 版  权:pukesys@tom.com
    
/// </summary>
    
/// <param name="sender"></param>
    
/// <param name="e"></param>

    void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    
{
        usermodel.User_ID 
= Convert.ToInt32(((Label)GridView1.Rows[e.RowIndex].Cells[0].FindControl("Lbl_UserID")).Text.Trim());
        usermodel.User_Email 
= ((TextBox)GridView1.Rows[e.RowIndex].Cells[0].FindControl("Txt_UserEmail")).Text.Trim();
        usermodel.DaLei_ID 
= Convert.ToInt32(((DropDownList)GridView1.Rows[e.RowIndex].Cells[0].FindControl("DropDownList_DaLeiDesc")).SelectedValue.Trim());
        usermodel.XiaoLei_ID 
= Convert.ToInt32(((DropDownList)GridView1.Rows[e.RowIndex].Cells[0].FindControl("DropDownList_XiaoLeiDesc")).SelectedValue.Trim());

        
string DaLei_Desc = ((DropDownList)GridView1.Rows[e.RowIndex].Cells[0].FindControl("DropDownList_DaLeiDesc")).SelectedItem.Text.Trim();
        
string XiaoLei_Desc = ((DropDownList)GridView1.Rows[e.RowIndex].Cells[0].FindControl("DropDownList_XiaoLeiDesc")).SelectedItem.Text.Trim();

        Response.Write(
"更新时:<br/>User_ID = " + usermodel.User_ID + "<br/>User_Email = " + usermodel.User_Email + "<br/>DaLei_ID = " + usermodel.DaLei_ID + "      DaLei_Desc = " + DaLei_Desc + "<br/>XiaoLei_ID = " + usermodel.XiaoLei_ID + "      XiaoLei_Desc = " + XiaoLei_Desc);

        userbll.Update(usermodel);
        Response.Write(
"<br/>更新完成!");

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


    
/// <summary>
    
/// 功    能:绑定GridView
    
/// 作  者:PUKE
    
/// 完成时间:2007-06-04
    
/// 版  权:pukesys@tom.com
    
/// </summary>

    private void DataBindGridView()
    
{
        DataSet ds 
= userbll.GetList("");
        GridView1.DataSource 
= ds;
        GridView1.DataBind();
    }

}

posted on 2007-06-02 12:25  秦岭过客  阅读(1366)  评论(0编辑  收藏  举报