博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
代码控制选中行的颜色
阅读量:5886 次
发布时间:2019-06-19

本文共 2972 字,大约阅读时间需要 9 分钟。

 

大家都知道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
 }

}

转载地址:http://edlix.baihongyu.com/

你可能感兴趣的文章
Oracle DG 备库恢复--gap
查看>>
iptables简介1
查看>>
AdaNet — 轻量级的 TensorFlow AutoML 框架
查看>>
斯坦福凸优化课程Video2.4_
查看>>
阿里AI设计师一秒出图,小撒连连惊呼,真相是...
查看>>
python之Unittest单元测试框架
查看>>
Spring学习笔记
查看>>
C# Lambda表达式详解,及Lambda表达式树的创建
查看>>
基于Docker搭建Jumpserver堡垒机操作实践
查看>>
vs2017开发IOS(vs2017 xamarin 连接mac)
查看>>
聊聊 scala 的模式匹配
查看>>
百度L4量产杀入乘用车后院,李彦宏首谈“车路城”终极形态
查看>>
Tornado 在模板里使用 Python 语句
查看>>
飞天2.0 | 万物智能的基础服务
查看>>
Android 拦截WebView加载URL,控制其加载CSS、JS资源
查看>>
Confluence 6 导入 SSL 证书和问题解决
查看>>
采用个hook技术对writefile函数进行拦截(2)
查看>>
马化腾可能不需要知乎,但知乎需要马化腾
查看>>
uC/OS-II源码分析(三)
查看>>
什么是SEM和付费搜索营销?
查看>>