·天新网首页·加入收藏·设为首页
首页|笔记本|手机|数码相机|摄像机|MP3/MP4|主板|内存|显示器|办公|打印机|下载|开发|汽车|学院|业界
硬件|台式机|数码|数字家庭|投影仪|GPS/CPU|显卡|硬盘|服务器|网络|一体机|驱动|源码|游戏|考试|报价
您现在的位置:天新网 > 软件开发 > 开发文摘 > .Net文摘
datagrid技巧之一:代码控制选中行的颜色
http://dev.21tx.com 2003年04月07日 Blog qieyj

大家都知道DataGrid是一个使用频率很高的控件,在编写ASP.net代码的时候,我们总希望能让用户选中指定的行以后,让那一行用不同的颜色显示,虽然datagrid样式也有这个功能,但是我们如何编写代码实现呢?

在本例子中,我们首先动态产生1000行,然后当用户选中datagrid中的某一行的时候,那一行就会变为蓝色。代码如下:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace WebApplication_rd
{
 /// <summary>
 /// Demostrates how to have a datagrid server control be bookmarked
 /// </summary>
 public class datagrid : System.Web.UI.Page
 {

  protected System.Web.UI.WebControls.DataGrid DGProducts;
  int bookmarkIndex = 0; // The index of the row that should be scrolled to
  int itemCount = 0; // Counter for the amount of items on the page
  bool bookMark = true; // Controls whether or not the page is bookmarked
 
  #region DGProducts EventHandlers

  private void DGProducts_ItemDataBound(object source, DataGridItemEventArgs e) {
  
   if (bookMark) {
   
    LiteralControl anchor = new LiteralControl();
    anchor.Text = "<a name=\"" + itemCount.ToString() + "\">";
    itemCount ++;
    e.Item.Cells[0].Controls.Add(anchor);
    
   }
   
  }

  private void DGProducts_ItemCommand(object source, DataGridCommandEventArgs e) {
   
   if (e.CommandName == "Select") {
  
    e.Item.BackColor = Color.Blue;

    if (bookMark) {
     bookmarkIndex = e.Item.ItemIndex;
     this.InsertScriptBlock();
    }   
   }
  
  }

  
  #endregion

  #region EventHandlers
  private void Page_Load(object sender, System.EventArgs e)
  {

   this.Bind();

  }

  #endregion

  #region User Defined

  private void InsertScriptBlock() {
  
   System.Text.StringBuilder jScript = new System.Text.StringBuilder();
   jScript.Append("<script language=\"JavaScript\">");
   jScript.Append("location.href=\"#");
   jScript.Append(this.bookmarkIndex.ToString());
   jScript.Append("\";");
   jScript.Append("</script>");

   this.ReGISterClientScriptBlock("Bookmark", jScript.ToString());
 
  }

  
  private void Bind()
  {
  
   DGProducts.DataSource = this.CreateDataSource(1000);
   DGProducts.DataBind();  
  
  }


  private DataTable CreateDataSource(int count) {
  
   DataTable table = new DataTable();
   DataColumn column = null;
   DataRow row = null;

   // Create 5 columns
   for (int iCol = 0; iCol < 5; iCol++) {
    
    column = new DataColumn("Column: " + iCol.ToString(), typeof(string));
    table.Columns.Add(column);

   }

   //Create Rows based on count variable
   for (int iRows = 0; iRows < count; iRows ++) {
   
    row = table.NewRow();
       
    for (int iCol = 0; iCol < 5; iCol ++) {
     
     row[iCol] = "Value: " + iCol.ToString();
    
    
    }

    table.Rows.Add(row);
   
   }
   
   return table;
  
  }


  #endregion

  #region Web Form Designer generated code
  override protected void OnInit(EventArgs e)
  {
   InitializeComponent();
   base.OnInit(e);
  }
  
  private void InitializeComponent()
  {   
   this.DGProducts.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DGProducts_ItemCommand);
   this.DGProducts.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DGProducts_ItemDataBound);
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion
 }

}

上一篇: ASP.NET工程中日历控件的典型应用之一
下一篇: 快速认识asp.net(一)

Google
 
热点文章
关于我们 | 联系我们 | 广告服务 | 工作机会 | 版权声明 | 欢迎投稿 | 网站地图
Copyright © 2000-2008 , www.21tx.com , All Rights Reserved .
© 晨新科技 版权所有 Created by TXSite.net