All articles(网络文学目录) All Pictures(图片目录) All Softwares(软件目录)

 
利用GridView显示主细表并添加删除、打开、关闭功能(续)_[Asp.Net教程]

Writer: delv Article type: Programming skills(编程技巧) Time: 2014/1/23 3:14:00 Browse times: 355 Comment times: 0

利用GridView显示主细表并添加删除、打开、关闭功能(续)_[Asp.Net教程]


Head photo

Go homepage
Upload pictures
Write articles

利用GridView显示主细表并添加删除、打开、关闭功能(续)_[Asp.Net教程]























本文是利用GridView显示主细表并添加打开、关闭功能的扩展。




有网友在论坛上询问:如何在使用GridView显示主细表时添加删除功能?其实,如果单个GridView能够实现删除对话,N个GridView嵌套也没有任何问题。下面就将例子粘贴如下,望各位需要的朋友仔细揣摩一下,道理很简单的:)




查看例子




aspx




<%@ Page Language="C#" AutoEventWireup="true" EnableViewState="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>







利用GridView显示主细表并添加打开、关闭功能













BorderWidth="1" OnRowDataBound="MasterGridView_RowDataBound" DataKeyNames="id"
ShowHeader="false">





float: left">
栏目名称:<%#Eval("Title") %>onclick="ShowHidden('<%#Eval("id") %>',event)">隐藏




">
HorizontalAlign="left" DataKeyNames="id" AutoGenerateDeleteButton="true" OnRowCommand="DetailGridView_RowCommand"
OnRowDeleting="DetailGridView_RowDeleting" Width="720px">




/read.aspx">
<%#Eval("Title") %>
[<%# Eval("HitCount") %>]


HeaderStyle-Width="100px" ItemStyle-HorizontalAlign="Center" />













C#




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.Data.OleDb;
using System.Web.UI.HtmlControls;




public partial class Default2 : System.Web.UI.Page
{
string ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\aspxWeb.mdb;Persist Security Info=True";
OleDbConnection cn1;





protected void Page_Load( object sender, EventArgs e )
{
if (!Page.IsPostBack)
{
OleDbConnection cn = new OleDbConnection(ConnectionString);
cn.Open();
cn1 = new OleDbConnection(ConnectionString);
cn1.Open();
OleDbCommand cmd = new OleDbCommand("select * from [Subject]", cn);
OleDbDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
MasterGridView.DataSource = dr;
MasterGridView.DataBind();
dr.Close();
cmd.Dispose();
cn.Dispose();
cn1.Dispose();
cn = cn1 = null;
}
}
protected void MasterGridView_RowDataBound( object sender, GridViewRowEventArgs e )
{
if (e.Row.RowType == DataControlRowType.DataRow)
{




GridView oGridView = (GridView)e.Row.FindControl("DetailGridView");
if (oGridView != null)
{
OleDbCommand cmd = new OleDbCommand("select top 10 * from Document Where pid = " + MasterGridView.DataKeys[e.Row.RowIndex].Value, cn1);
OleDbDataReader dr1 = cmd.ExecuteReader();
oGridView.DataSource = dr1;
oGridView.DataBind();
dr1.Close();
cmd.Dispose();
}
}
}




protected void DetailGridView_RowDeleting( object sender, GridViewDeleteEventArgs e )
{
GridView a = (GridView)sender;
Response.Write("您要删除的记录是:" + a.DataKeys[e.RowIndex].Value.ToString() + "&nbsp;&nbsp;&nbsp;&nbsp;TODO:执行删除动作");
// TODO:执行删除动作
}
protected void DetailGridView_RowCommand( object sender, GridViewCommandEventArgs e )
{




}
}
注意:EnableViewState="true" 是必须的。





来源:孟宪会之精彩世界


































There are 0 records,
Comment:
Must be registered users to comment(必须是注册用户才能发表评论)

Disclaimer Privacy Policy About us Site Map
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.