向GridView动态增加一列,DropDownList控件及Header

<script type="text/javascript">google_ad_client = "pub-2048279401139630";google_ad_slot = "8856771542";google_ad_width = 728;google_ad_height = 90;document.write("<s"+"cript type='text/javascript' s"+"rc='http://pagead2.googlesyndication.com/pagead/show_ads"+"."+"js'></scr"+"ipt>");</script>

因为在做项目的时候需要在gridview动态增加一列,也就是带有操作列表的DropDownList。现在将我的方法说出来和大家分享一下,如果有好的方法,希望能告诉我。

首先在网站项目里添加一个新项,就是web窗体default.aspx。前台代码如下:

 

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

<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< script  language ="javascript"  type ="text/javascript" >
//<!CDATA[
这段代码就是重置DropDownList控件
如果需要执行选定的动作,需要弹出确认窗口,否则就将选择的项
重置到第一个项
**/
function ResetDDL(obj)
{
    
if(obj.selectedIndex==0)
    
{
        window.alert(
"请选择您需要的操作!");
        
this.focus();
        
return;
    }

    
    bool
=confirm('您确定执行此操作吗?');
    
if(!bool)
    
{
        obj.selectedIndex
=0;
    }

}


//]]>
</ script >

< html  xmlns ="http://www.w3.org/1999/xhtml"   >
< head  runat ="server" >
    
< title > 无标题页 </ title >
</ head >
< body >
    
< form  id ="form1"  runat ="server" >
        
< asp:GridView  ID ="gvUser"  runat ="server"  AllowPaging ="True"  AutoGenerateColumns ="False"
            CellPadding
="4"  ForeColor ="#333333"  GridLines ="None"  OnDataBound ="gvUser_DataBound"
            OnPageIndexChanging
="gvUser_PageIndexChanging"  OnRowCreated ="gvUser_RowCreated" >
            
< FooterStyle  BackColor ="#1C5E55"  Font-Bold ="True"  ForeColor ="White"   />
            
< Columns >
                
< asp:BoundField  DataField ="Id"  HeaderText ="用户ID"   />
                
< asp:BoundField  DataField ="UserName"  HeaderText ="姓名"   />
                
< asp:BoundField  DataField ="Sex"  HeaderText ="性别"   />
                
< asp:BoundField  DataField ="Addr"  HeaderText ="详细地址"   />
            
</ Columns >
            
< RowStyle  BackColor ="#E3EAEB"   />
            
< EditRowStyle  BackColor ="#7C6F57"   />
            
< SelectedRowStyle  BackColor ="#C5BBAF"  Font-Bold ="True"  ForeColor ="#333333"   />
            
< PagerStyle  BackColor ="#666666"  ForeColor ="White"  HorizontalAlign ="Center"   />
            
< HeaderStyle  BackColor ="#1C5E55"  Font-Bold ="True"  ForeColor ="White"   />
            
< AlternatingRowStyle  BackColor ="White"   />
        
</ asp:GridView >
        
&nbsp;
    
</ form >
</ body >
</ html >  

接下来是关键,也就是后台代码,default.aspx.cs的代码如下:


/****************************************************
*Author:水若寒
*QQ:49940396
*Date:2007-04-22
***************************************************
*/

using  System;
using  System.Data;
using  System.Configuration;
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.Collections.Generic;
using  System.Collections;

public   partial   class  _Default : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
         
if (!IsPostBack)
        
{
            Bind();
//此处就是首次加载页面的时候绑定GridView
        }

    }

    
protected void Bind()
    
{
        
this.gvUser.DataSource = CreateDataSource();
        
this.gvUser.DataBind();
    }

    
protected void gvUser_PageIndexChanging(object sender, GridViewPageEventArgs e)
    
{
        
this.gvUser.PageIndex = e.NewPageIndex;
        Bind();
    }

   
/// <summary>
   
/// 此处创建数据源,用户可以根据需要创建自己的数据源。
   
/// </summary>
   
/// <returns>返回一个实现ICollection的DataView数据源</returns>

    protected ICollection CreateDataSource()
    
{
        DataTable dtUser 
= new DataTable();
        DataColumn dc;
        DataRow dr;
        Random r;

        
//Create a new datacolumn whose name is ID
        dc = new DataColumn("ID"typeof(System.Int32));
        dc.AutoIncrement 
= true;
        dc.AutoIncrementSeed 
= 1;
        dtUser.Columns.Add(dc);

        dc 
= new DataColumn("UserName"typeof(System.String));
        dtUser.Columns.Add(dc);

        dc 
= new DataColumn("Sex"typeof(System.String));
        dtUser.Columns.Add(dc);

        dc 
= new DataColumn("Addr"typeof(System.String));
        dtUser.Columns.Add(dc);

        r 
= new Random(1000);
        
for (int i = 0; i < 1000; i++)
        
{
            dr 
= dtUser.NewRow();

            
switch (i % 3)
            
{
                
case 0:
                    dr[
"UserName"= "水若寒" + r.Next(100200).ToString();
                    dr[
"Sex"= "";
                    dr[
"Addr"= "北京市朝阳区";
                    
break;
                
case 1:
                    dr[
"UserName"= "Eyis" + r.Next(300400).ToString();
                    dr[
"Sex"= "";
                    dr[
"Addr"= "北京市通州区";
                    
break;
                
case 2:
                    dr[
"UserName"= "Juics" + r.Next(500600).ToString();
                    dr[
"Sex"= "";
                    dr[
"Addr"= "北京市朝阳区赵公村";
                    
break;

            }


            dtUser.Rows.Add(dr);
        }


        
return dtUser.DefaultView;
    }

    
/// <summary>
    
/// 在RowCreated事件中动态添加DropDownList以及Header
    
/// 思路如下:
    
/// 首先,通过创建一个TableCell对象作为DropDownList对象的容器
    
/// 接着,创建DropDownList控件,根据需要对其项进行初始化
    
/// 然后,将DropDownList对象的AutoPostBack属性打开,这样可以自动回发
    
/// 添加对DropDownList的OnSelectedIndexChanged的时间委托
    
/// 然后,将DropDownList控件添加到刚才的TableCell对象中
    
/// 最后,通过e.Row.RowType为DataRow时将TableCell对象添加到GridView的列中
    
/// 接下来,就是添加GridView的Header的值方法类似
    
/// </summary>
    
/// <param name="sender"></param>
    
/// <param name="e"></param>

    protected void gvUser_RowCreated(object sender, GridViewRowEventArgs e)
    
{
         
if (e.Row.RowType == DataControlRowType.DataRow)
        
{
            TableCell tc 
= new TableCell();
            DropDownList ddlOP 
= new DropDownList();//创建一个DropDownList对象
            ListItem[] ltc = {
                            
new ListItem("-----请选择-----","0"),
                            
new ListItem("从展厅入库","1"),
                            
new ListItem("从展厅撤出""2"),
                            
new ListItem("从库房借出""3"),
                            
new ListItem("从库房入库""4")
                          }
;//创建一个ListItem的数组,并且初始化值,可以根据实际需要进行修改

            ddlOP.Items.AddRange(ltc);
            ddlOP.ID 
= "OP";
            ddlOP.AutoPostBack 
= true;
            ddlOP.EnableViewState 
= true;
            ddlOP.Attributes.Add(
"onchange""ResetDDL(this);");//通过添加自定义的js函数来确定用户是否执行操作
            ddlOP.SelectedIndexChanged += new EventHandler(ddlOP_SelectedIndexChanged);//此处通过添加委托来处理SelectedIndexChanged事件
            tc.Controls.Add(ddlOP);

            e.Row.Cells.Add(tc);
        }


        
if (e.Row.RowType == DataControlRowType.Header)
        
{
            TableCell tcHeader 
= new TableCell();
            Literal lit 
= new Literal();
            lit.Text 
= "操作";
            tcHeader.Controls.Add(lit);
            tcHeader.HorizontalAlign 
= HorizontalAlign.Center;

            e.Row.Cells.Add(tcHeader);
        }

    }

    
/// <summary>
    
/// 这里就是处理动态添加对DropDownList控件的OnSelectedIndexChanged事件处理函数了
    
/// </summary>
    
/// <param name="sender"></param>
    
/// <param name="e"></param>

    protected void ddlOP_SelectedIndexChanged(object sender, EventArgs e)
    
{

        DropDownList ddlOp 
= sender as DropDownList;//将sender转换成DropDownList对象
        GridViewRow gvr;

        gvr 
= (GridViewRow)ddlOp.Parent.Parent;//此处最为关键,就是获取DropDownList对象所在的GridViewRow对象

        Response.Write(ddlOp.SelectedValue
+"====="+this.gvUser.Rows[gvr.RowIndex].Cells[0].Text);//此处可以获取当前选取的Value的值和DropDownList所在行的Id的值
    }



}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值