2015-10-19 29 views
0

我有一個Gridview,其中我已經放置了兩個LinkBut​​tons編輯和刪除行。無法投射「System.Web.UI.WebControls.GridView」類型的對象以鍵入「System.Web.UI.WebControls.LinkBut​​ton」。而使用分頁

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand" Width="631px" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Horizontal" Height="144px" style="text-align: right" AllowPaging="True" OnPageIndexChanging="GridView1_PageIndexChanging"> 
        <Columns> 
         <asp:TemplateField> 
          <ItemTemplate> 
           &nbsp;&nbsp;&nbsp; 
           <asp:LinkButton ID="LinkEdit" runat="server" CommandArgument='<%#Eval("eid") %>'>Edit</asp:LinkButton> 
           &nbsp;| 
           <asp:LinkButton ID="LinkDelete" runat="server" CommandArgument='<%#Eval("eid") %>'>Delete</asp:LinkButton> 
          </ItemTemplate> 
         </asp:TemplateField> 

但是,當我在GridView的添加分頁,並試圖去頁第二,第三......,它給我的錯誤: -

Unable to cast object of type 'System.Web.UI.WebControls.GridView' to type 'System.Web.UI.WebControls.LinkButton'.

Source Error: 
Line 29:  { 
Line 30:   string id = e.CommandArgument.ToString(); 
Line 31:   string cmdText = ((LinkButton)e.CommandSource).Text; 
Line 32:   if (cmdText.Equals("Edit")) 
Line 33:   { 

實際誤差顯示第31行:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) 
{ 
    string id = e.CommandArgument.ToString(); 
    string cmdText = ((LinkButton)e.CommandSource).Text; 
    if (cmdText.Equals("Edit")) 
    { 
     Response.Redirect("Emp_Edit.aspx?id=" + id); 
    } 
    else 
    { 
     Class1.EmpDelete(id); 
     Response.Redirect("Emp_Reg.aspx"); 
    } 
} 

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) 
    { 
     GridView1.PageIndex = e.NewPageIndex; 
     ShowAll(); 
    } 

public void ShowAll() 
{ 
    GridView1.DataSource = Class1.ShowData(); 
    GridView1.DataBind(); 
} 
+2

也請提供' ShowAll();'更好的審查功能。 –

+0

ShowAll();是綁定網格的公共方法。 –

+0

e.CommandSource中斷時的值是什麼。在GridView1_RowCommand上放置一個調試點並重現錯誤。 – niksofteng

回答

1

嘗試這樣,並相應地和carefuly改變一切:

更改視圖如下:

<asp:LinkButton id="LinkEdit" 
      Text="Edit" 
      CommandName="Edit" 
      CommandArgument='<%#Eval("eid") %>' 
      runat="server"/> 

<asp:LinkButton id="LinkDelete" 
      Text="Delete" 
      CommandName="Edit" 
      CommandArgument='<%#Eval("eid") %>' 
      runat="server"/> 

更改代碼隱藏如下:

string cmdText = e.CommandName; // Line 31 

或過改變你的方法爲:

<asp:LinkButton id="LinkEdit" 
       Text="Edit" 
       CommandName="Edit" 
       CommandArgument='<%#Eval("eid") %>' 
       OnCommand="LinkButton_Command" 
       runat="server"/> 

而且

void LinkButton_Command(Object sender, CommandEventArgs e) 
     { 
     string cmdText = e.CommandName; 
     } 
+0

選擇我的答案,並投票我的答案,如果它的工作:) –

0

導致錯誤的是 string cmdText = ((LinkButton)e.CommandSource).Text;線 和

(LinkButton)e.CommandSource 

一部分。 它返回的是一個GridView的命令源,並且您試圖將其轉換爲鏈接按鈕,所以它是無效的轉換。

另一件事,e.CommandArgumentgridview_RowCommand返回行的索引,而不是控件CommandArgument文本或值。

所以,你必須嘗試這樣的事情

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) 
    { 
     int index = Convert.ToInt32(e.CommandArgument.ToString()); 
     LinkButton lb=(LinkButton)GridView1.Rows[index].FindControl("lblCmd");//lblCmd is the Id of Your Link Button 
     string id = lb.CommandArgument.ToString(); 
     string cmdText = lb.Text; 
     if (cmdText.Equals("Edit")) 
     { 
      Response.Redirect("Emp_Edit.aspx?id=" + id); 
     } 
     else 
     { 
      Class1.EmpDelete(id); 
      Response.Redirect("Emp_Reg.aspx"); 
     } 
    } 

OR

但在這種情況下,你的GridView編輯按鈕必須包含CommandName屬性與價值"Edit"

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) 
{ 
int index = Convert.ToInt32(e.CommandArgument.ToString()); 
LinkButton lb=(LinkButton)GridView1.Rows[index].FindControl("lblCmd");//lblCmd is the Id of Your Link Button 
if(e.CommandName=="Edit") 
{ 
    Response.Redirect("Emp_Edit.aspx?id=" + lb.CommandArgument); 
} 
else 
{ 
    Class1.EmpDelete(idlb.CommandArgument; 
    Response.Redirect("Emp_Reg.aspx"); 
} 
} 
相關問題