我有一個Gridview,其中我已經放置了兩個LinkButtons編輯和刪除行。無法投射「System.Web.UI.WebControls.GridView」類型的對象以鍵入「System.Web.UI.WebControls.LinkButton」。而使用分頁
<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>
<asp:LinkButton ID="LinkEdit" runat="server" CommandArgument='<%#Eval("eid") %>'>Edit</asp:LinkButton>
|
<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();
}
也請提供' ShowAll();'更好的審查功能。 –
ShowAll();是綁定網格的公共方法。 –
e.CommandSource中斷時的值是什麼。在GridView1_RowCommand上放置一個調試點並重現錯誤。 – niksofteng