简体   繁体   中英

Creating a Clickable GridView in ASP.NET using C#

I've been trying to create a clickable gridview using the following code:

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

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

I've put the code into a custom server control in order to create a .dll file which I've referenced in my main project. As a simple test, I've simply been using the line

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

and

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

in order to see how the table will look like. However, I am unable to get anything to display when I compile the main page.

Is there anything different between this custom control vs the default gridview? The above code displays the default gridview no problem but when using the clickablegridview, nothing shows up (no compilation errors).

好吧,看来问题是您不能在已经绑定到另一个元素的sqldatareader上使用databind / datasource。

请在此处找到解决方案, ASP.NET 4.0中具有Postback的Clickable GridView

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM