1

我一直在尝试使用以下代码创建一个可点击的网格视图:

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); 
}

来自:http ://aspadvice.com/blogs/joteke/archive/2006/01/07/14576.aspx

我已将代码放入自定义服务器控件中,以创建我在主项目中引用的 .dll 文件。作为一个简单的测试,我只是在使用这条线

<cgv:ClickableGridView ID = "MyGridView" runat = "server" />

    MyGridView.DataSource = reader;
    MyGridView.DataBind();

以查看表格的外观。但是,当我编译主页时,我无法显示任何内容。

这个自定义控件与默认的 gridview 有什么不同吗?上面的代码显示默认的gridview没有问题,但是当使用clickablegridview时,什么都没有显示(没有编译错误)。

4

2 回答 2

0

好的,问题似乎在于您不能在已经数据绑定到另一个元素的 sqldatareader 上使用数据绑定/数据源。

于 2012-09-05T15:55:30.953 回答
-1

请在此处找到解决方案, 可单击 GridView with Postback in ASP.NET 4.0

于 2012-09-17T05:38:09.273 回答