2014-07-10 43 views
0

我已創建一個gridview,其中有一列有一些圖像按鈕來執行某些操作。隨着它,我也創建了標題點擊排序事件。 Image for gridview is enclosed in the attachmentGridView標題行點擊查看排序

當我點擊「任務標題」標題時,命令會轉到Grid View的RowCommand事件,在此處我檢測到圖像按鈕,並根據其CommandName執行干預操作。

但問題是我想排序列和命令是指向rowCommand它無法找到imagebutton。

我該如何解決這個問題?

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」。

嘿guyz !!你沒有得到我... 我說我該怎麼做,以便命令不應該去RowCommand事件,而單擊HeaderText。

回答

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


    } 
+0

它doesn沒有工作。當我們創建GridViewRow對象'gvr'時,它會發生異常。例外情況是:無法投射「System.Web.UI.WebControls.GridView」類型的對象來鍵入「System.Web.UI.WebControls.ImageButton」。 – Mayank

-1

在RowCommand事件,你可以試試下面的代碼:

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

    } 

相同的代碼寫入另外兩個圖像按鈕

謝謝......