一直都只有找到VB版 , 能力有限 , 修改不了
找這這篇C#版,好實用忍不住要把他記下來 ... 原文出處
步驟1.創造兩個GridView+SqlDataSource
步驟2.GridView1 的 RowDataBound 事件撰寫下面
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if ((e.Row.RowState & DataControlRowState.Selected) == DataControlRowState.Selected)
{
GridViewRow oRow = new GridViewRow(0, -1, DataControlRowType.DataRow, e.Row.RowState);
GridView2.Visible = true;
GridView2.DataBind();
string sHTML = ControlToHTML(GridView2);
GridView2.Visible = false;
TableCell oCell = new TableCell();
oCell.Text = sHTML;
oCell.ColumnSpan = e.Row.Cells.Count;
oRow.Cells.Add(oCell);
e.Row.Parent.Controls.AddAt(e.Row.RowIndex + 2, oRow);
}
}
步驟3加入兩段小小程式碼 不然不會動
private string ControlToHTML(System.Web.UI.Control Control)
{
string sHTML = string.Empty;
StringWriter oTextWriter = new StringWriter();
HtmlTextWriter oHtmlWriter = new HtmlTextWriter(oTextWriter);
Control.RenderControl(oHtmlWriter);
sHTML = Convert.ToString(oTextWriter);
return sHTML;
}
public override void VerifyRenderingInServerForm(Control control)
{}
步驟4.在原始檔部分設定一下屬性
設定 GridView1的 EnableViewState="False"
設定 GridView2的 EnableViewState="False",且設定 Visible="False"
Page 的 EnableEventValidation="false"
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default6.aspx.cs" Inherits="Default6" EnableEventValidation="false" %>
文章標籤
全站熱搜