在GridView使整个行可点击

在GridView使整个行可点击

本文介绍了在GridView使整个行可点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个gridview,我需要做一个事件火单击某一行的时候。

I have a gridview and I need to make an event fire when a row is clicked.

有一个现有的GridView的事件中,我需要绑定来实现这一目标?

Is there an existing GridView event I need to bind to to make this happen?

推荐答案

这里的东西我prepared前面:

Here's something I prepared earlier:


public class RowClickableGridView : GridView
    {
        public Style HoverRowStyle
        {
            get { return ViewState["HoverRowStyle"] as Style; }
            set { ViewState["HoverRowStyle"] = value; }
        }

        public bool EnableRowClickSelection
        {
            get { return ViewState["EnableRowClickSelection"] as bool? ?? true; }
            set { ViewState["EnableRowClickSelection"] = value; }
        }

        public string RowClickCommand
        {
            get { return ViewState["RowClickCommand"] as string ?? "Select"; }
            set { ViewState["RowClickCommand"] = value; }
        }

        public string RowToolTip
        {
            get
            {
                if (!RowToolTipSet) return string.Format("Click to {0} row", RowClickCommand.ToLowerInvariant());
                return ViewState["RowToolTip"] as string;
            }
            set
            {
                ViewState["RowToolTip"] = value;
                RowToolTipSet = true;
            }
        }

        private bool RowToolTipSet
        {
            get { return ViewState["RowToolTipSet"] as bool? ?? false; }
            set { ViewState["RowToolTipSet"] = value; }
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            foreach (GridViewRow row in Rows)
            {
                if (row.RowType != DataControlRowType.DataRow) continue;

                if (EnableRowClickSelection && row.RowIndex != SelectedIndex && row.RowIndex != EditIndex)
                {
                    if (string.IsNullOrEmpty(row.ToolTip)) row.ToolTip = RowToolTip;
                    row.Style[HtmlTextWriterStyle.Cursor] = "pointer";

                    PostBackOptions postBackOptions = new PostBackOptions(this,
                                                                          string.Format("{0}${1}",
                                                                                        RowClickCommand,
                                                                                        row.RowIndex));
                    postBackOptions.PerformValidation = true;
                    row.Attributes["onclick"] = Page.ClientScript.GetPostBackEventReference(postBackOptions);


                    foreach (TableCell cell in row.Cells)
                    {
                        foreach (Control control in cell.Controls)
                        {
                            const string clientClick = "event.cancelBubble = true;{0}";
                            WebControl webControl = control as WebControl;
                            if (webControl == null) continue;
                            webControl.Style[HtmlTextWriterStyle.Cursor] = "Auto";
                            Button button = webControl as Button;
                            if (button != null)
                            {
                                button.OnClientClick = string.Format(clientClick, button.OnClientClick);
                                continue;
                            }
                            ImageButton imageButton = webControl as ImageButton;
                            if (imageButton != null)
                            {
                                imageButton.OnClientClick = string.Format(clientClick, imageButton.OnClientClick);
                                continue;
                            }
                            LinkButton linkButton = webControl as LinkButton;
                            if (linkButton != null)
                            {
                                linkButton.OnClientClick = string.Format(clientClick, linkButton.OnClientClick);
                                continue;
                            }
                            webControl.Attributes["onclick"] = string.Format(clientClick, string.Empty);
                        }
                    }
                }

                if (HoverRowStyle == null) continue;
                if (row.RowIndex != SelectedIndex && row.RowIndex != EditIndex)
                {
                    row.Attributes["onmouseover"] = string.Format("this.className='{0}';", HoverRowStyle.CssClass);
                    row.Attributes["onmouseout"] = string.Format("this.className='{0}';",
                                                                 row.RowIndex%2 == 0
                                                                     ? RowStyle.CssClass
                                                                     : AlternatingRowStyle.CssClass);
                }
                else
                {
                    row.Attributes.Remove("onmouseover");
                    row.Attributes.Remove("onmouseout");
                }
            }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            base.Render(writer);
            foreach (GridViewRow row in Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
                    Page.ClientScript.RegisterForEventValidation(row.ClientID);
                }
            }
        }
    }

然后勾入行标准的命令事件...

You then hook into the standard row command events...

这篇关于在GridView使整个行可点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 09:28