突出显示和编辑网格

时间:2012-08-07 19:26:45

标签: c# html visual-studio-2008 gridview

我目前有一个gridview,通过c#方法通过数据库填充。

我想知道是否有办法在行上的任何地方点击时选择行,而不是完全使用选择按钮。然后将该行中的信息发回并填充网页上的其他区域。

网格是否比gridview更好?我应该外包给jQuery吗?或者是我应该需要的gridview吗?

1 个答案:

答案 0 :(得分:1)

您需要做的是开发一个可单击行的GridView。最好的办法是按照链接中的说明操作。如果你对VB没问题,你可以沿着这条路走。用户还在评论部分将其转换为C#。我包括它,因为你没有看到它。

这是我保存的链接:http://aspadvice.com/blogs/joteke/archive/2006/01/07/14576.aspx

using System; 
using System.ComponentModel; 
using System.Configuration; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

namespace CustomGridView 
{ 
 /// <summary> 
 /// Summary description for ClickableGridView 
 /// </summary> 
 public class ClickableGridView : GridView 
 { 
   public string RowCssClass 
   { 
     get 
     { 
       string rowClass = (string)ViewState["rowClass"]; 
       if (!string.IsNullOrEmpty(rowClass)) 
         return rowClass; 
       else 
         return string.Empty; 
     } 
     set 
     { 
       ViewState["rowClass"] = value; 
     } 
   } 

   public string HoverRowCssClass 
   { 
     get 
     { 
       string hoverRowClass = (string)ViewState["hoverRowClass"]; 
       if (!string.IsNullOrEmpty(hoverRowClass)) 
         return hoverRowClass; 
       else 
         return string.Empty; 
     } 
     set 
     { 
       ViewState["hoverRowClass"] = value; 
     } 
   } 

   private static readonly object RowClickedEventKey = new object(); 

   public event GridViewRowClicked RowClicked; 
   protected virtual void OnRowClicked(GridViewRowClickedEventArgs e) 
   { 
     if (RowClicked != null) 
       RowClicked(this, e); 
   } 

   protected override void RaisePostBackEvent(string eventArgument) 
   { 
     if (eventArgument.StartsWith("rc")) 
     { 
       int index = Int32.Parse(eventArgument.Substring(2)); 
       GridViewRowClickedEventArgs args = new GridViewRowClickedEventArgs(Rows[index]); 
       OnRowClicked(args); 
     } 
     else 
       base.RaisePostBackEvent(eventArgument); 
   } 

   protected override void PrepareControlHierarchy() 
   { 
     base.PrepareControlHierarchy(); 

     for (int i = 0; i < Rows.Count; i++) 
     { 
       string argsData = "rc" + Rows[i].RowIndex.ToString(); 
       Rows[i].Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(this, argsData)); 

       if (RowCssClass != string.Empty) 
         Rows[i].Attributes.Add("onmouseout", "this.className='" + RowCssClass + "';"); 

       if (HoverRowCssClass != string.Empty) 
         Rows[i].Attributes.Add("onmouseover", "this.className='" + HoverRowCssClass + "';"); 
     } 
   } 
 } 

 public class GridViewRowClickedEventArgs : EventArgs 
 { 
   private GridViewRow _row; 

   public GridViewRowClickedEventArgs(GridViewRow aRow) 
     : base() 
   { 
     _row = aRow; 
   } 

   public GridViewRow Row 
   { 
     get 
     { return _row; } 
   } 
 } 

 public delegate void GridViewRowClicked(object sender, GridViewRowClickedEventArgs args); 
}