无法完成这项工作。
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnApprove" runat="server" Text="Approve" OnClick ="btnApprove_Click" />
</ItemTemplate>
</asp:TemplateField>
代码背后的代码:
protected void btnApprove_Click(object sender, EventArgs e)
{
Response.Redirect("viewprofile.aspx");
}
单击按钮时甚至没有触发。对此有什么诡计吗?
答案 0 :(得分:0)
在Page指令的顶部设置EnableEventValidation="false"
:
<%@ Page EnableEventValidation="false" Language="C#"
...
请注意,将此值设置为false可能会使您的网站暴露于安全漏洞。作为替代方案,您可以处理网格视图EnableEventValidation="false"
而不是设置OnRowCommand
:
<强> .ASPX:强>
<asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button runat="server" Text="Approve" CommandName="Approve" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
代码背后:
public partial class delete_me : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)//THIS IS IMPORTANT.GridView1_RowCommand will not fire unless you add this line
{
var p1 = new Person() { Name = "Person 1" };
var p2 = new Person() { Name = "Person 2" };
var list = new List<Person> { p1, p2 };
GridView1.DataSource = list;
GridView1.DataBind();
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
System.Diagnostics.Debugger.Break();
}
}
public class Person
{
public string Name { get; set; }
}
答案 1 :(得分:-1)
你刚刚加上你的网格视图。
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click"/>
</ItemTemplate>
</asp:TemplateField>
还要把代码放在
之后protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("WebForm1.aspx");
}
尝试!!!!它工作得很好......