bufferSoft

善积跬步方能至千里,领先一步方能立潮头

导航

gridview显示主从表


<%@ Page Language="C#" MasterPageFile="~/sz668w_web/MasterPage.master" AutoEventWireup="true" CodeFile="sortmanage.aspx.cs" Inherits="sz668w_web_Default" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    
<div style="z-index: 101; left: 0px; width: 650px; position: absolute; top: -22px;
        height: 100px">
        &nbsp;<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
            AutoGenerateColumns
="False" BackColor="White" BorderColor="#E7E7FF" BorderStyle="None"
            BorderWidth
="1px" Caption="栏目信息" CaptionAlign="Top" CellPadding="3" DataKeyNames="sortid"
            OnPageIndexChanging
="GridView1_PageIndexChanging" OnRowCancelingEdit="GridView1_RowCancelingEdit"
            OnRowDataBound
="GridView1_RowDataBound" OnRowDeleting="GridView1_RowDeleting"
            OnRowEditing
="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" PageSize="2"
            Width
="642px">
            
<FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
            
<Columns>
                
<asp:BoundField DataField="sortid" HeaderText="一级栏目ID" ReadOnly="True" />
                
<asp:TemplateField HeaderText="一级栏目名称">
                    
<EditItemTemplate>
                        
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("sortname") %>'></asp:TextBox>
                    
</EditItemTemplate>
                    
<ItemTemplate>
                        
<asp:Label ID="Label2" runat="server" Text='<%# Bind("sortname") %>'></asp:Label>
                    
</ItemTemplate>
                    
<ControlStyle Width="60px" />
                
</asp:TemplateField>
                
<asp:TemplateField HeaderText="一级栏目首页">
                    
<EditItemTemplate>
                        
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("sorturl") %>'></asp:TextBox>
                    
</EditItemTemplate>
                    
<ItemTemplate>
                        
<asp:Label ID="Label4" runat="server" Text='<%# Bind("sorturl") %>'></asp:Label>
                    
</ItemTemplate>
                    
<ControlStyle Width="100px" />
                
</asp:TemplateField>
                
<asp:TemplateField HeaderText="二级栏目信息">
                    
<ItemTemplate>
                        
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" AllowPaging="True" PageSize="2"
                        OnPageIndexChanging
="GridView2_PageIndexChanging" OnRowCancelingEdit="GridView2_RowCancelingEdit"
                        OnRowDeleting
="GridView2_RowDeleting"
            OnRowEditing
="GridView2_RowEditing" OnRowUpdating="GridView2_RowUpdating" DataKeyNames="sortid" BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4" >
                            
<Columns>
                                
<asp:BoundField DataField="sortid" HeaderText="二级栏目ID" ReadOnly="True" />
                                
<asp:TemplateField HeaderText="二级栏目名称">
                                    
<EditItemTemplate>
                                        
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("sortname") %>'></asp:TextBox>
                                    
</EditItemTemplate>
                                    
<ItemTemplate>
                                        
<asp:Label ID="Label1" runat="server" Text='<%# Bind("sortname") %>'></asp:Label>
                                    
</ItemTemplate>
                                
</asp:TemplateField>
                                
<asp:TemplateField HeaderText="二级栏目首页">
                                    
<EditItemTemplate>
                                        
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("sorturl") %>'></asp:TextBox>
                                    
</EditItemTemplate>
                                    
<ItemTemplate>
                                        
<asp:Label ID="Label2" runat="server" Text='<%# Bind("sorturl") %>'></asp:Label>
                                    
</ItemTemplate>
                                
</asp:TemplateField>
                                
<asp:CommandField ShowEditButton="True" />
                                
<asp:CommandField ShowDeleteButton="True" />
                            
</Columns>
                            
<FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
                            
<RowStyle BackColor="White" ForeColor="#330099" />
                            
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
                            
<PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
                            
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
                        
</asp:GridView>
                    
</ItemTemplate>
                
</asp:TemplateField>
                
<asp:CommandField ShowEditButton="True" />
                
<asp:CommandField ShowDeleteButton="True" />
            
</Columns>
            
<RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
            
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
            
<PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
            
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
            
<AlternatingRowStyle BackColor="#F7F7F7" />
        
</asp:GridView>
    
</div>
    
<br />
    
<br />
    
<br />
    
<br />
    
<br />
    
<br />
    
<br />
    
<br />
    
<br />
    
<br />
    
<br />
    
<br />
    
&nbsp;<br />
    
&nbsp;<br />
    
<br />
    
<br />
</asp:Content>


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;
using Microsoft.ApplicationBlocks.Data;
using System.IO;
using System.Xml;
public partial class sz668w_web_Default :classbase
{
    
public string power = "";
    
protected void Page_Load(object sender, EventArgs e)
    
{
        power 
= Convert.ToString(Session["power"]);
        
if (power == "超级管理员" || power == "城市管理员")
        
{
        }

        
else
        
{
            Response.Redirect(
"manager.aspx");
        }

        
if (!IsPostBack)
        
{
            GridViewBind();
            
//GridView2Bind();
          
//  this.InitDDLValue(DropDownList1, "select  sortid,sortname from sortinfo where len(sortid)=2");
           
        }


    }

    
public void GridViewBind()
    
{
        DataSet ds 
= new DataSet();
        
try
        
{
            
string str = "select * from sortinfo where len(sortid)=2 order by sortid";
            ds 
= SqlHelper.ExecuteDataset(ConStr, CommandType.Text, str);
            
if (ds != null)
            
{
                
this.GridView1.DataSource = ds.Tables[0].DefaultView;
                
this.GridView1.DataBind();
            }

            
else
            
{
                Response.Write(
"<script language='javascript'>alert('数据库为空!');</script>");

            }

        }

        
catch (Exception ee)
        
{
            Response.Write(ee.ToString());
           
// Response.Write("<script language='javascript'>alert('数据库出错!');</script>");
        }


    }

    
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    
{

        GridView1.EditIndex 
= e.NewEditIndex;
        
//当前编辑行背景色高亮
        
//this.GridView1.EditRowStyle.BackColor = Color.FromName("#F7CE90");

        GridViewBind();
    }

    
/// <summary>
    
/// 取消编辑状态
    
/// </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 id = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
        
//转换为整数
        
//int ID=Convert.ToInt32(rowToDelete);
        
//从数据库中删除
       
// Response.Write(id)
        string str = "DELETE FROM sortinfo where left(sortid,2)='" + id + "'";

        
try
        
{
            SqlHelper.ExecuteNonQuery(ConStr, CommandType.Text, str);
            
//重新绑定数据
         
//   GridViewBind();
        }

        
catch (Exception ex)
        
{
            Response.Write(
"数据库错误,错误原因:" + ex.Message);
            Response.End();
        }


        
//xml数据库
      /*  string url = Server.MapPath("~/xml/sortinfo.xml");
        string str_username = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(url); //加载XML文件 
        XmlNode root = xmlDoc.SelectSingleNode("sortinfo");
        XmlNodeList xnl = root.ChildNodes; 
        for (int i = 0; i < xnl.Count; i++)
        {
            XmlNodeList xnl11 = xnl.Item(i).SelectNodes("sortid");//查找username下面节点集合 
            XmlElement xe = (XmlElement)xnl11.Item(0); //username节点下面的元素 
            if (xe.InnerText == str_username) //获取节点以及所有子集的串联值 
            {

                root.RemoveChild(xe.ParentNode);//移除指顶节点 
                // xe.ParentNode.RemoveChild(xe); 
                // xnl.Item(i).RemoveChild(xe); 
                // if(i<xnl.Count)i=i-1; 
            }
        }
        xmlDoc.Save(url);
*/

        GridViewBind();
    }

    
/// <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 password = getmd5(((TextBox)GridView1.Rows[e.RowIndex].FindControl("textbox1")).Text);

        
string str1 = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2")).Text;
        
string str2 = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox3")).Text;
        
//判断表单项是否有空并给出提示信息
       
        
try
        
{
            
string str = "update sortinfo set sortname='" +str1 + "' ,sorturl='"+str2+"' where sortid='" + ID + "'";
            
// Response.Write(str);
            SqlHelper.ExecuteNonQuery(ConStr, CommandType.Text, str);

        }

        
catch (SqlException err)
        
{

        }

        
/*try
        {
            string url=Server.MapPath("~/xml/sortinfo.xml");
            XmlDocument xmlDoc=new XmlDocument();
            xmlDoc.Load(url);
            XmlNode root=xmlDoc.SelectSingleNode("sortinfo");
*/


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


    
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    
{

        GridView2Bind();
        
        
     
//   Response.Write(GridView1.Rows.Count.ToString());
        
        
//加入鼠标滑过的高亮效果
        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)
        
{
            
/*GridView oGridView = (GridView)e.Row.FindControl("GridView2");
            DataSet ds = new DataSet();
            string sql = "select * from sortinfo where left(sortid,2)='" + GridView1.DataKeys[e.Row.RowIndex].Value + "' and len(sortid)=4";
            ds = SqlHelper.ExecuteDataset(ConStr, CommandType.Text, sql);
            oGridView.DataSource = ds.Tables[0].DefaultView;
            oGridView.DataBind();
*/

            
//GridView2Bind(GridView1.DataKeys[e.Row.RowIndex].Value.ToString());
            e.Row.Attributes.Add("onclick""this.style.backgroundColor='#99cc00'; this.style.color='buttontext';this.style.cursor='default';");
        }


      
    }


  
    
//gridview2
    protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
    
{
        GridView a 
= (GridView)sender;
        a.EditIndex 
= e.NewEditIndex;
        
//当前编辑行背景色高亮
        
//this.GridView1.EditRowStyle.BackColor = Color.FromName("#F7CE90");

         GridView2Bind();
    }

    
protected void GridView2_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    
{
        GridView a 
= (GridView)sender;
        a.EditIndex 
= -1;
        GridView2Bind();
    }

    
protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e)
    
{
        GridView a 
= (GridView)sender;

        
//得到单位编号
        string id = a.DataKeys[e.RowIndex].Values[0].ToString();
        
//转换为整数
        
//int ID=Convert.ToInt32(rowToDelete);
        
//从数据库中删除
        
// Response.Write(id)
        string str = "DELETE FROM sortinfo where sortid='" + id + "'";

        
try
        
{
            SqlHelper.ExecuteNonQuery(ConStr, CommandType.Text, str);
            
//重新绑定数据
            GridView2Bind();
        }

        
catch (Exception ex)
        
{
            Response.Write(
"数据库错误,错误原因:" + ex.Message);
            Response.End();
        }


    }

    
protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
    
{
        GridView a 
= (GridView)sender;
        
string ID = a.DataKeys[e.RowIndex].Values[0].ToString();
        
//  string password = getmd5(((TextBox)GridView1.Rows[e.RowIndex].FindControl("textbox1")).Text);

        
string str1 = ((TextBox)a.Rows[e.RowIndex].FindControl("TextBox1")).Text;
        
string str2 = ((TextBox)a.Rows[e.RowIndex].FindControl("TextBox2")).Text;
        
//判断表单项是否有空并给出提示信息

        
try
        
{
            
string str = "update sortinfo set sortname='" + str1 + "' ,sorturl='" + str2 + "' where sortid='" + ID + "'";
            
// Response.Write(str);
            SqlHelper.ExecuteNonQuery(ConStr, CommandType.Text, str);
        }

        
catch (SqlException err)
        
{

        }


        Response.Write(
"<script language='javascript'>alert('数据已被保存!');</script>");
        
//返回浏览状态
       a.EditIndex = -1;
        GridView2Bind();
    }

    
protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
    
{
        GridView a 
= (GridView)sender;
        a.PageIndex 
= e.NewPageIndex;
      
//  GridView1.PageIndex = e.NewPageIndex;
        GridView2Bind();
    }

    
public void GridView2Bind()
    
{
      
     
        GridView oGridView;
   
//     this.TextBox4.Text = this.DropDownList1.Items[1].Value.ToString();
       
        
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        
{
            oGridView 
= (GridView)GridView1.Rows[i].FindControl("GridView2");

            
string str1 = GridView1.Rows[i].Cells[0].Text;
       
//     Response.Write(str1);
            if (oGridView != null)
            
{

                DataSet ds1 
= new DataSet();
                
string sql = "select * from sortinfo where left(sortid,2)= '"+str1+"' and len(sortid)=4";
                
                
try
                
{
                    ds1 
= SqlHelper.ExecuteDataset(ConStr, CommandType.Text, sql);
                    
if (ds1 != null)
                    
{
                        oGridView.DataSource 
= ds1.Tables[0].DefaultView;
                        oGridView.DataBind();
                    }

                    
else
                    
{
                        Response.Write(
"<script language='javascript'>alert('数据库为空!');</script>");
                    }

                }

                
catch (Exception ee)
                
{
                    Response.Write(ee.ToString());
                    
//Response.Write("<script language='javascript'>alert('数据出错!');</script>");
                }


            }

        }
       
    }


   
    
//xml
    public void up_sortinfo()
    
{

    }


   
}

posted on 2007-07-27 19:15  qyfan  阅读(3196)  评论(0编辑  收藏  举报