【问题标题】:GridView Header row Click for SortingGridView 标题行 点击排序
【发布时间】:2014-08-31 14:49:56
【问题描述】:

我创建了一个gridview,其中有一列有一些图像按钮来执行一些操作。与此同时,我还在标题点击上创建了排序事件。

当我点击“任务标题”标题时,命令转到网格视图的 RowCommand 事件,在该事件中我检测到图像按钮并根据其命令名称执行操作。

但问题是我想对列进行排序,命令被定向到无法找到图像按钮的 rowCommand。

我该如何解决这个问题??

aspx代码是:

<asp:GridView ID="TableTask" runat="server" BackColor="White" BorderColor="#CCCCCC"
        BorderStyle="None" BorderWidth="1px" CellPadding="3" AutoGenerateColumns="False"
        ViewStateMode="Enabled" Width="300px" Style="margin-left: 50px; margin-top: 50px;
        margin-bottom: 50px;" RowStyle-HorizontalAlign="Center" OnRowCommand="TableTask_RowCommand"
        OnRowCreated="TableTask_RowCreated">
        <Columns>
            <asp:TemplateField HeaderText="Task Titl`enter code here`e" SortExpression="Task_Title">
                <ItemTemplate>
                    <asp:Label ID="lblTaskName" Text='<%#BIND("Task_Title") %>' runat="server"></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText=" ">
                <ItemTemplate>
                    <asp:ImageButton ID="btnView" runat="server" CommandName="View" ImageUrl="~/Images/ViewIcon.png"
                        ToolTip="View" />&nbsp;&nbsp;
                    <asp:ImageButton ID="btnEdit" runat="server" CommandName="Change" ImageUrl="~/Images/EditIcon.png"
                        ToolTip="Edit" />&nbsp;&nbsp;
                    <asp:ImageButton ID="btnDelete" CommandArgument='<%# Eval("Task_ID") %>' runat="server"
                        CommandName="Remove" ImageUrl="~/Images/DeleteIcon.png" ToolTip="Delete" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <FooterStyle BackColor="White" ForeColor="#000066" />
        <HeaderStyle BackColor="Gray" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Center" />
        <RowStyle ForeColor="#000066" />
        <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
        <SortedAscendingCellStyle BackColor="#F1F1F1" />
        <SortedAscendingHeaderStyle BackColor="#007DBB" />
        <SortedDescendingCellStyle BackColor="#CAC9C9" />
        <SortedDescendingHeaderStyle BackColor="#00547E" />
    </asp:GridView>

RowCommand 方法后面的代码定义如下:

protected void TableTask_RowCommand(object sender, GridViewCommandEventArgs e)
{
    GridViewRow gvr = (GridViewRow)((ImageButton)e.CommandSource).NamingContainer;
    int rowIndex = gvr.RowIndex;
    GridViewRow gv = TableTask.Rows[rowIndex];
    Label task_title = (Label)gv.FindControl("lblTaskName");

    if (e.CommandName == "View")
    {
        // My code    
    }

    if (e.CommandName == "Change")
    {
        // My code
    }

    if (e.CommandName == "Remove")
    {
        // My code
    }
}

当我们创建 GridViewRow 对象 'gvr' 时会出现异常。

例外是:无法将“System.Web.UI.WebControls.GridView”类型的对象转换为“System.Web.UI.WebControls.ImageButton”类型。

嘿家伙!你没有得到我... 我是说我应该怎么做才能让命令在单击 HeaderText 时不转到 RowCommand 事件。

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    处理Gridview的排序事件,设置允许排序属性为True,并添加以下代码 .cs 文件:

        public SortDirection dir
        {
            get {
                if (ViewState["dirstate"] == null)
                {
                    ViewState["dirstate"] = SortDirection.Ascending;
                }
               return (SortDirection)ViewState["dirstate"];
            }
            set {
                ViewState["dirstate"] = value;
            }
        }
    
        protected void grdemp_Sorting(object sender, GridViewSortEventArgs e)
        {
            BindGrid();
            string Sortdir = string.Empty;
            if (dir == SortDirection.Ascending)
            {
                dir = SortDirection.Descending;
                Sortdir = "DESC";
            }
            else
            {
                dir = SortDirection.Ascending;
                Sortdir = "ASC";
            }
    
            DataView dv = new DataView(dt);
            dv.Sort = e.SortExpression + " " + Sortdir;
            grdemp.DataSource = dv;
            grdemp.DataBind();
    
    
        }
    

    【讨论】:

    • 它不起作用。当我们创建 GridViewRow 对象“gvr”时,它会出现异常。例外是:无法将“System.Web.UI.WebControls.GridView”类型的对象转换为“System.Web.UI.WebControls.ImageButton”类型。
    【解决方案2】:

    在 RowCommand 事件中,您可以尝试以下代码:

      if(e.CommandName=="View")
      {
          int id=int.parse(e.CommandArgument.ToString());
    
      }
    

    同样的代码写入另外两个图像按钮

    谢谢......

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-09
      • 1970-01-01
      • 2020-05-25
      • 1970-01-01
      • 1970-01-01
      • 2018-06-30
      • 2016-05-26
      • 1970-01-01
      相关资源
      最近更新 更多