ASP.NET 2.0 中动态添加 GridView 模板列的例子

news/2023/12/1 7:11:41

 

动态添加列,关键是实现 ITemplate.InstantiateIn 方法。下面是一个添加 GridView 模板列的例子。

C#代码

<% @ Page Language="C#"  %>
<% @ Import Namespace="System.Data"  %>
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>

<script runat="server">
  ICollection CreateDataSource()
  
{
    DataTable dt 
= new DataTable();
    DataRow dr;
    dt.Columns.Add(
new DataColumn("id"typeof(Int32)));
    dt.Columns.Add(
new DataColumn("text"typeof(string)));
    
for (int i = 0; i < 6; i++)
    
{
      dr 
= dt.NewRow();
      dr[
0= i;
      dr[
1= "列表项目 " + i.ToString();
      dt.Rows.Add(dr);
    }

    DataView dv 
= new DataView(dt);
    
return dv;
  }


  public class GridViewTemplate : ITemplate
  
{
    private DataControlRowType templateType;
    private string columnName;

    public GridViewTemplate( DataControlRowType type, string colname )
    
{
      templateType 
= type;
      columnName 
= colname;
    }


    public 
void InstantiateIn( System.Web.UI.Control container )
    
{
      
switch (templateType)
      
{
        
case DataControlRowType.Header:
          Literal lc 
= new Literal();
          lc.Text 
= columnName;          
          container.Controls.Add(lc);          
          
break;
        
case DataControlRowType.DataRow:
          DropDownList drr 
= new DropDownList();
          drr.ID 
= "dropdown";
          drr.AppendDataBoundItems 
= true;
          drr.Items.Add(
new ListItem("-----请选择------",""));
          drr.Items.Add(
new ListItem("AA""a"));
          drr.Items.Add(
new ListItem("BB""b"));
          drr.Items.Add(
new ListItem("CC""c"));
          container.Controls.Add(drr);
          
break;
        
default:
         
break;
      }

    }

  }

  
  protected 
void Page_Load(object sender, EventArgs e)
  
{
    
if (!IsPostBack)
    
{
      TemplateField customField 
= new TemplateField();
      customField.ShowHeader 
= true;
      customField.HeaderTemplate 
= new GridViewTemplate(DataControlRowType.Header, "动态添加列");
      customField.ItemTemplate 
= new GridViewTemplate(DataControlRowType.DataRow, "");
      GridView1.Columns.Add(customField);
      GridView1.DataSource 
= CreateDataSource();
      GridView1.DataBind();
    }

  }


  protected 
void GridView1_RowDataBound( object sender, GridViewRowEventArgs e )
  
{
    
if (e.Row.RowType == DataControlRowType.DataRow)
    
{
      
//可以在这里访问数据库的其它字段的值,可以设置默认选择项,具体应用,看自己的发挥了。
      //下面只是例子,举一反三,不再费话了
      DataRowView gv = (DataRowView)e.Row.DataItem;
      
int itemSeleted = Int32.Parse(gv.Row["id"].ToString()) > 3 ? 0 : Int32.Parse(gv.Row["id"].ToString());
      DropDownList dr 
= (DropDownList)e.Row.FindControl("dropdown");
      dr.SelectedIndex 
= itemSeleted;
    }

  }

script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
  
<title>GridView动态添加模板列的例子 title>
head>
<body>
<form id="form1" runat="server">
  
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
     OnRowDataBound
="GridView1_RowDataBound">
    
<Columns>
      
<asp:BoundField HeaderText="标题"  DataField="text"/>
    
Columns>
  
asp:GridView> 
form>
body>
html>

 



http://www.niftyadmin.cn/n/3656587.html

相关文章

ASP.NET 2.0中将 GridView 导出到 Excel 文件中

下面代码实现将 GridView 导出到 Excel文件中。值得注意的是VerifyRenderingInServerForm重载方法&#xff1a;  MSDN上的 VerifyRenderingInServerForm 方法的描述&#xff1a;  必须位于 标记中的控件可以在呈现之前调用此方法&#xff0c;以便在控件被置于标记外时显示错…

《ASP.NET 2.0应用开发技术》9月份出版发行

网上购买地址&#xff1a;http://www.dearbook.com.cn/book/110870http://www.china-pub.com/computers/common/info.asp?id31665http://990.ln183.com/mulu/6/9/8/5/9/3/698593.htmhttp://www.wfjbs.com.cn/list.asp?id711515112http://www.huachu.com.cn/itbook/itbookinfo…

《ASP.NET 2.0应用开发技术》随想

我喜欢上网&#xff0c;也是CSDN社区的版主&#xff0c;需要经常泡论坛&#xff0c;回答一些网友的问题&#xff0c;时间长了&#xff0c;就会发现&#xff0c;相同的问题总会经常出现。仔细分析了这些问题之后发现&#xff1a;出现这些问题的根本原因就是基本概念不是很清楚&a…

关于loose.dtd和xhtml1-transitional.dtd等文档类型定义模型中CSS失效的解决办法。

最近&#xff0c;很多人反映 下面的CSS定义在 loose.dtd和xhtml1-transitional.dtd下无效了&#xff1a;body{SCROLLBAR-FACE-COLOR: #f2f2f2; SCROLLBAR-HIGHLIGHT-COLOR: #ffffff; SCROLLBAR-SHADOW-COLOR: #999999; SCROLLBAR-3DLIGHT-COLOR: #999999; SCROLLBAR-ARROW-COLO…

《ASP.NET 2.0应用开发技术》技术支持和服务站点上线

本站提供与《ASP.NET 2.0 应用开发技术》一书相关的技术支持&#xff0c;答疑&#xff0c;勘误&#xff0c;例子解释&#xff0c;例子运行结果等内容。网址是&#xff1a;http://dotnet.aspx.cc/Books.aspx

IIS代码管理(1):遍历应用程序池和属性

下面的代码实现遍历 IIS 6应用程序池的一个方法&#xff1a;System.DirectoryServices.DirectoryEntry appPoolRoot new System.DirectoryServices.DirectoryEntry("IIS://localhost/W3SVC/AppPools");//得到默认应用程序池的方法可以直接使用 IIS://localhost/W3SV…

Linux管道解惑

管道过滤器&#xff08;Pipe-And-Filter&#xff09;模式 按照《POSA(面向模式的软件架构)》里的说法&#xff0c;管道过滤器&#xff08;Pipe-And-Filter&#xff09;应该属于架构模式&#xff0c;因为它通常决定了一个系统的基 本架构。管道过滤器和生产流水线类似&…

IIS代码管理(2):创建应用程序池和属性

下面的代码实现应用程序池的创建&#xff0c;并设置一些属性。string strAppPoolName "MyAppPool1";System.DirectoryServices.DirectoryEntry appPoolRoot new System.DirectoryServices.DirectoryEntry("IIS://localhost/W3SVC/AppPools");System.Dire…

DNN4.0(SQL Server2000数据库)安装过程

1&#xff0c;下载 DNN4.0&#xff08;需要.NET2.0支持&#xff09;源代码软件包&#xff0c;下载地址&#xff1a; http://prdownloads.sourceforge.net/dnn2&#xff0c;解压到物理文件夹D:/DotNetNuke400 &#xff08;文件夹名字任意&#xff0c;但最好不要使用中文文件夹名…

FireFox值得称道的地方

FireFox的优点有许多&#xff0c;昨天在浏览页面时又发现一个以前没有发现的优点。一个图片的URL不存在或者图片不能显示的时候&#xff0c;在IE中会显示一个红叉&#xff0c;而FireFox中会显示一个文字链接&#xff0c;这样给人的感觉页面没有任何错误。分别如下&#xff1a;F…

检测远程URL是否存在的三种方法

本文用3种方法检测远程URL是否存在。private void Page_Load(object sender, System.EventArgs e){string url1 "http://dotnet.aspx.cc/"; string url2 "http://dotnet.aspx.cc/Images/logo.gif"; Response.Write("方法1&#xff1a;"); Resp…

对shell命令执行简单的跟踪

程序是人写的&#xff0c;难免会出错。 一个好的方法是将跟踪功能&#xff08;execution tracing&#xff09;打开。这会使得shell显示每个被执行的命令&#xff0c;并在前面加上“ ”&#xff1a; 一个加号后面跟着一个空格。&#xff08;你可以通过给Shell变量PS4赋一个新值以…

上传之前检测图片大小

<script>function CheckFileSize(){if(document.fileSize){var oImgSize parseInt(document.images.oImg.fileSize,10)alert(oImgSize)}else{alert("浏览器不支持此功能。")}return false}</script>

随机得到Access数据库记录

由于Access数据库记录集缓存的原因&#xff0c;从代码里得到Access数据库随机记录是得不到&#xff0c;需要用随机SQL语句的办法来消除缓存。下面就是例子&#xff1a;查看例子<% Page Language"C#" Debug"true" %><% import Namespace"Syst…

将纯数字导入Excel时数据格式仍保持不变的方法

办法就是利用Excel的特性查看例子代码 <% Page language"c#" AutoEventWireup"true" %><script runat"server">void Page_Load(object sender, System.EventArgs e){ string data1 "000000001"; long data2 12345678912…
最新文章