我将Datagrid嵌到DIV中(目的是可以拉动光标条浏览全部数据),页面为30秒刷新一次,Datagrid中有一个按钮列,我想在点击Datagrid的按钮列的某一行后此行变色,当且在页面刷新后保持此行的状态
请问高手们有什么好办法吗?帮帮我吧

解决方案 »

  1.   

    用Session存行的ID,绑定时判断一下改变颜色
      

  2.   

    可不可以说的明白点,ID我怎么可以得到呀我是刚刚学习NET的
      

  3.   

    [转载]在本例子中,我们首先动态产生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 booked
     /// </summary>
     public class datagrid : System.Web.UI.Page
     {  protected System.Web.UI.WebControls.DataGrid DGProducts;
      int bookIndex = 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 booked
     
      #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) {
         bookIndex = 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.bookIndex.ToString());
       jScript.Append("\";");
       jScript.Append("</script>");   this.RegisterClientScriptBlock("Book", 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
     }}
      

  4.   

    在datagrid后面敲个点,你会看见一个selected index, 把这个值存入session
    然后,在page load 的时候,检测session,如果有你存的那个值,则让datagrid 的selected index = session里的值
    over
      

  5.   

    您可以在DataGrid中存放一个字段ID,但是让它不可见,然后直接把您所选记录的这行的这个字段中的值保存在SESSION中
      

  6.   

    DataGrid的样式中有这个选项吧。DataGrid属性-->格式-->项-->选定项里面设置