程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> ASP.NET2.0下含有DropDownList的GridView編輯、刪除的完整例子!

ASP.NET2.0下含有DropDownList的GridView編輯、刪除的完整例子!

編輯:.NET實例教程

<ASP:GridView ID="GridVIEw1" runat="server" AutoGenerateColumns="False" PageSize="10" 
                        Width="542px" AllowPaging="True" AllowSorting="True"
                         DataKeyNames="DB31_1,DB31_2" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowDataBound="GridView1_RowDataBound" OnSelectedIndexChanged="GridVIEw1_SelectedIndExchanged" OnSorting="GridVIEw1_Sorting" >
                        <Columns>
                            <ASP:TemplateFIEld HeaderText="序號">
                                <ItemTemplate>
                                <%# this.GridView1.PageIndex * this.GridView1.PageSize + this.GridVIEw1.Rows.Count + 1%>
                                </ItemTemplate>
                            </ASP:TemplateFIEld>
                            <ASP:TemplateFIEld HeaderText="學歷代碼" SortExpression="DB1_1">
                                <EditItemTemplate>
                                    <%--<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("DB1_1") %>'></ASP:TextBox>--%>    <asp:DropDownList ID ="ddlXL" runat="server"  DataValueFIEld='<%# Bind("DB1_1") %>'></ASP:DropDownList>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("xueliText") %>'></ASP:Label>
                                </ItemTemplate>
                            </ASP:TemplateFIEld>
                            <ASP:TemplateFIEld HeaderText="學歷名稱" SortExpression="DB1_2">
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("DB1_2") %>'></ASP:TextBox>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("DB1_2") %>'></ASP:Label>
                              &nb
sp; </ItemTemplate>
                            </ASP:TemplateFIEld>
                          
                        <ASP:TemplateFIEld HeaderText="操作" ShowHeader="False">
                        <EditItemTemplate>
                            <ASP:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update"
                                 Text="更新"></ASP:LinkButton>
                            <ASP:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel"
                                Text="取消"></ASP:LinkButton>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <ASP:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit"
                                Text="編輯" OnClIEntClick="return confirm('確認要編輯嗎?');"></ASP:LinkButton>
                        
                            <ASP:LinkButton ID="LinkButton3" runat="server" CausesValidation="False" CommandName="Delete"            Text="刪除" OnClIEntClick="return confirm('確認要刪除嗎?');"></ASP:LinkButton>
                            <ASP:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Select"
                                Text="選擇"></ASP:LinkButton>
                        </ItemTemplate>
                        </ASP:TemplateFIEld>
                       </Columns>
                       <AlternatingRowStyle BackColor="Aquamarine" />
                    </ASP:GridVIEw>
    /// <summary>
    /// 綁定數據到GridVIEw
    /// </summary>
    private void GridVIEwBind()
    {
        檢索數據庫
        string strSql = "SELECT * FROM DB1";
        得到數據集
        this.GridView1.DataSource=conn.GetDs(strSql).Tables[0].DefaultVIEw;
        this.GridVIEw1.DataBind();
   
    }
    /// <summary>
    /// 編輯當前行
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView1_RowEditing(object sender, GridVIEwEditEventArgs e)
    {
        GridVIEw1.EditIndex = e.NewEditIndex;
        //當前編輯行背景色高亮
        this.GridVIEw1.EditRowStyle.BackColor = Color.FromName("#F7CE90");
        GridVIEwBind();
    }
    /// <summary>
    /// 取消編輯狀態
    /// &
lt;/summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView1_RowCancelingEdit(object sender, GridVIEwCancelEditEventArgs e)
    {
        GridVIEw1.EditIndex = -1;
        GridVIEwBind();
    }
    /// <summary>
    /// 刪除記錄過程
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView1_RowDeleting(object sender, GridVIEwDeleteEventArgs e)
    {
        //得到單位編號
        string rowToDelete = GridVIEw1.DataKeys[e.RowIndex].Values[0].ToString();
        //轉換為整數
        //int ID=Convert.ToInt32(rowToDelete);
        //從數據庫中刪除
        string str = "DELETE FROM DB1 where DB1_1=" + "'" + rowToDelete + "'" + "";
       
        try
        {
        conn.RunSql(str);
        //重新綁定數據
        GridVIEwBind();
        }
        catch (Exception ex)
        {
        Response.Write("數據庫錯誤,錯誤原因:" + ex.Message);
        Response.End();
        }

    }
    /// <summary>
    /// 更新記錄過程
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView1_RowUpdating(object sender, GridVIEwUpdateEventArgs e)
    {
        string ID = GridVIEw1.DataKeys[e.RowIndex].Values[0].ToString();
        string DB1_1 = ((TextBox)GridVIEw1.Rows[e.RowIndex].FindControl("TextBox1")).Text;
        //string DB1_2 = ((TextBox)GridVIEw1.Rows[e.RowIndex].FindControl("TextBox2")).Text;
        string DB1_2 = (((DropDownList))GridVIEw1.Rows[e.RowIndex].FindControl("ddlXL")).SelectedItem.Text;
        
    //判斷表單項是否有空並給出提示信息
&
nbsp;       if (DB1_1 == "" || DB1_2 == "")
        {
            conn.Alert("請輸入完整信息!", Page);
            return;
        }             
       try
       {
        conn.BuilderEdit("select * from DB1 where DB1_1 ='" + ID + "'");
        conn.dr["DB1_1"] = DB1_1;
        conn.dr["DB1_2"] = DB1_2;
        conn.BuilderEditClose();
       }
       catch (OracleException err)
       {
            if (err.Code.ToString() == "1")
                conn.Alert("錯誤:已存在具有相同主鍵的記錄", Page);
            else
                conn.Alert("錯誤:未能添加記錄", Page);
        }

        Response.Write("<script language='Javascript'>alert('數據已被保存!');</script>");
        //返回浏覽狀態
        GridVIEw1.EditIndex = -1;
        GridVIEwBind();
    }
    /// <summary>
    /// 分頁事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView1_PageIndexChanging(object sender, GridVIEwPageEventArgs e)
    {
        GridVIEw1.PageIndex = e.NewPageIndex;
        GridVIEwBind();
    }
    /// <summary>
    /// 加入鼠標效果及為DropDownList綁定值
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView1_RowDataBound(object sender, GridVIEwRowEventArgs e)
    {
        //為DropDownList綁定值
        if (((DropDownList)e.Row.FindControl("ddlXL")) != null)
        {     DropDownList ddlXL = (DropDownList)e.Row.FindControl("ddlXL");
            ddlXL.Items.Clear();
            ddlXL.Items.Add(new ListItem("博士", "1"));
            ddlXL.Items.Add(new ListItem("碩士", "2"));
            ddlXL.Items.Add(new ListItem("學士", "3"));
        }

        //加入鼠標滑過的高亮效果
        if (e.Row.RowType == DataControlRowType.DataRow)//判定當前的行是否屬於datarow類型的行
        {
            //當鼠標放上去的時候 先保存當前行的背景顏色 並給附一顏色
            e.Row.Attributes.Add("onmouSEOver", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='yellow',this.style.fontWeight='';");
            //當鼠標離開的時候 將背景顏色還原的以前的顏色
            e.Row.Attributes.Add("onmouSEOut", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
        }
        //單擊行改變行背景顏色
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onclick", "this.style.backgroundColor='#99cc00'; this.style.color='buttontext';this.style.cursor='default';");
        } 
    }


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved