当前位置:  开发笔记 > 编程语言 > 正文

在gridview中使整行可单击

如何解决《在gridview中使整行可单击》经验,为你挑选了1个好方法。

我有一个gridview,我需要在单击一行时触发事件.

是否存在我需要绑定的现有GridView事件才能实现此目的?



1> MPritchard..:

这是我之前准备的东西:

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

然后你挂钩标准的行命令事件......

推荐阅读
wurtjq
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有