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

推荐订阅源

MyScale Blog
MyScale Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
阮一峰的网络日志
阮一峰的网络日志
罗磊的独立博客
博客园 - 叶小钗
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
美团技术团队
酷 壳 – CoolShell
酷 壳 – CoolShell
雷峰网
雷峰网
宝玉的分享
宝玉的分享
大猫的无限游戏
大猫的无限游戏
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Last Week in AI
Last Week in AI
爱范儿
爱范儿
小众软件
小众软件
K
Kaspersky official blog
P
Proofpoint News Feed
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - Franky
V
Vulnerabilities – Threatpost
博客园_首页
Microsoft Security Blog
Microsoft Security Blog
C
Cybersecurity and Infrastructure Security Agency CISA
V
V2EX
C
Check Point Blog
S
Schneier on Security
P
Palo Alto Networks Blog
IT之家
IT之家
GbyAI
GbyAI
T
Threat Research - Cisco Blogs
Hugging Face - Blog
Hugging Face - Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Apple Machine Learning Research
Apple Machine Learning Research
C
Cyber Attacks, Cyber Crime and Cyber Security
T
Tailwind CSS Blog
Project Zero
Project Zero
Y
Y Combinator Blog
V
Visual Studio Blog
Simon Willison's Weblog
Simon Willison's Weblog
T
Threatpost
Scott Helme
Scott Helme
L
LINUX DO - 热门话题
S
Securelist
C
CERT Recently Published Vulnerability Notes
A
Arctic Wolf
M
MIT News - Artificial intelligence
人人都是产品经理
人人都是产品经理

博客园 - 石川

moodle更改记录 新校区效果图 大鹏出品,必属精品 ubuntu设置 2009年2月份3月份工作安排 SHOW下房间 book hmidea new eTable “E表”策划书 《绐老师的建议》读后感 高中教学水平评估宣传片 随想 笔记本 大学作品回顾 清华大学统一用户认证及用户漫游系统ASP.NET版 毕业一个月 读取yahoo的天气,并下载其中的图片和FLASH到本地
GridView控件修改、删除、分页、排序示例(修改含有DropDownList控件)
石川 · 2007-07-31 · via 博客园 - 石川

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnSorting="GridView1_Sorting" OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDataBound="GridView1_RowDataBound" OnPageIndexChanging="GridView1_PageIndexChanging" CellPadding="4" AllowSorting="True" AllowPaging="True" PageSize="18">
              
<Columns>
                  
<asp:BoundField DataField="class_id" HeaderText="编号" ReadOnly="True" SortExpression="class_id">
                      
<ItemStyle Width="60px" />
                  
</asp:BoundField>
                  
<asp:BoundField DataField="class_name" HeaderText="类别名称" SortExpression="class_name">
                      
<ItemStyle Width="200px" />
                  
</asp:BoundField>
                  
                  
<asp:TemplateField HeaderText="父类别" SortExpression="class_parent">
                      
<ItemTemplate>
                         
<%#bindParent_Name(Eval("class_parent").ToString())%>
                      
</ItemTemplate>
                      
<EditItemTemplate>
                         
<asp:HiddenField ID="HDFXueli" runat="server" Value='<%# Eval("class_parent") %>' />
                         
<asp:DropDownList ID="DDLXueli" runat="server" Width="90px" />
                      
</EditItemTemplate>
                      
<ItemStyle Width="100px" />
                  
</asp:TemplateField>
                            
                  
<asp:CommandField EditText="修改" HeaderText="修改" ShowEditButton="True" >
                      
<ItemStyle Width="60px" />
                  
</asp:CommandField>
                  
<asp:CommandField HeaderText="删除" ShowDeleteButton="True" >
                      
<ItemStyle Width="60px" />
                  
</asp:CommandField>
              
</Columns>
              
<PagerStyle HorizontalAlign="Center" Font-Bold="True" />
              
<HeaderStyle HorizontalAlign="Left" />
              
<SelectedRowStyle Font-Bold="True" />
              
</asp:GridView>

  private void Bind()
    
{
        
string key = "";
        
string parent_id = "";
        
try
        
{
            key 
= Request.QueryString["key"];
            parent_id 
= Request.QueryString["parent_id"];
        }

        
catch
        
{
            key 
= "";
            parent_id 
= "";
        }


        DataTable dt;
        
if (parent_id != "" && parent_id != null)
        
{
            Data d;
            
string cond = "";
            
if (key == "" || key == null)
            
{
                
if (parent_id == "0")
                    cond 
= "class_parent!=0";
                
else
                    cond 
= "class_parent=" + parent_id;
            }

            
else
            
{
                
if (parent_id == "0")
                    cond 
= "class_name like '%" + key + "%' and class_parent!=0";
                
else
                    cond 
= "class_name like '%" + key + "%' and class_parent=" + parent_id;
            }

            d 
= new Data("bookClass", cond, "class_id desc"1);
            dt 
= d.getDataTable();
        }

        
else
        
{
            dt 
= Dol.dtClassTwo();
        }


        
string sort = (string)ViewState["SortOrder"+ " " + (string)ViewState["OrderDire"];
        bind.bindGridView(
this.GridView1, dt, "class_id", sort);
    }


    
//根据父目录编号返回名称
    public string bindParent_Name(string class_id)
    
{
        
string parent_name = class_id;
        
for (int i = 0; i < dt.Rows.Count; i++)
        
{
            
if (dt.Rows[i][0].ToString().Trim() == class_id.Trim())
            
{
                parent_name 
= dt.Rows[i][1].ToString();
                
break;
            }

        }

        
return parent_name;
    }


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

            ddlxueli.DataSource 
= dt;
            ddlxueli.DataTextField 
= "class_name";
            ddlxueli.DataValueField 
= "class_id";
            ddlxueli.DataBind();

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


        
//如果是绑定数据行 
        if (e.Row.RowType == DataControlRowType.DataRow)
        
{
            
if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            
{
                ((LinkButton)e.Row.Cells[
4].Controls[0]).Attributes.Add("onclick""javascript:return confirm('你确认要删除:[" + e.Row.Cells[1].Text + "]吗?\\n删除类别[" + e.Row.Cells[1].Text + "]后该类别下的所有图书、子目录和子目录下的图书都将被删除!')");
            }

        }

    }


    
//排序
    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    
{
        
string sPage = e.SortExpression;
        
if (ViewState["SortOrder"].ToString() == sPage)
        
{
            
if (ViewState["OrderDire"].ToString() == "Desc")
                ViewState[
"OrderDire"= "ASC";
            
else
                ViewState[
"OrderDire"= "Desc";
        }

        
else
        
{
            ViewState[
"SortOrder"= e.SortExpression;
        }

        Bind();
    }


    
//编辑
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    
{
        GridView1.EditIndex 
= e.NewEditIndex;
        Bind();
    }


    
//删除
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    
{
        
string class_id = GridView1.DataKeys[e.RowIndex].Value.ToString();
        
//删除该 目录下的图书
        DataDel d1 = new DataDel("book""class_id=" + class_id);
        d1.ExceDel();

        
//删除该目录下的子目录
        DataDel d3 = new DataDel("bookClass""class_parent=" + class_id);
        d3.ExceDel();

        
//删除该目录
        DataDel d = new DataDel("bookClass""class_id=" + class_id);
        d.ExceDel();

        Bind();
    }


    
//更新
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    
{
        
string class_name = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim();
        
string class_id = GridView1.DataKeys[e.RowIndex].Value.ToString();
        
string class_parent = ((DropDownList)GridView1.Rows[e.RowIndex].FindControl("DDLXueli")).SelectedValue;

        Data d1 
= new Data();
        
if (d1.count("bookClass""class_name='" + class_name + "' and class_parent=" + class_parent + " and class_id!=" + class_id) == 0)
        
{
            DataUpdate d 
= new DataUpdate("bookClass""class_name='" + class_name + "',class_parent=" + class_parent, "class_id=" + class_id);
            d.ExceUpdate();
            GridView1.EditIndex 
= -1;
            Bind();
        }

        
else
        
{
            Alert a 
= new Alert("" + class_name + "]名称重复!");
            a.show();
        }

    }


    
//取消
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    
{
        GridView1.EditIndex 
= -1;
        Bind();
    }


    
//分页
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    
{
        GridView1.PageIndex 
= e.NewPageIndex;
        Bind();
    }