我有一个 Gridview,其中放置了两个用于编辑和删除行的 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 中添加分页并尝试转到第 2 页、第 3 页...时,它给了我错误:-
无法将“System.Web.UI.WebControls.GridView”类型的对象转换为“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();
}
像这样尝试并相应地、小心地改变一切:
更改视图如下:
<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;
}
错误原因是
string cmdText = ((LinkButton)e.CommandSource).Text;
线
和
(LinkButton)e.CommandSource
部分。 它返回命令源,它是一个网格视图,并且您试图将其转换为链接按钮,因此这是无效的转换。
另一件事,
e.CommandArgument
中的gridview_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");
}
}
或
但在这种情况下,您的 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");
}
}