ImageButton中的回发或回调参数无效

时间:2013-08-14 15:56:20

标签: c# asp.net .net

我的Click事件对按钮(图像按钮)删除有问题。 我知道有其他问题,但我什么都不懂!

我没有理解没有答案!我做了所有其他问题中指定的内容! 此页面中的其他事件使"重定向"例如,效果很好!

我很清楚吗?需要更多细节?堆栈跟踪或更多代码?

错误是:

  

无效的回发或回调参数。使用配置或<%@ Page EnableEventValidation =" true"启用事件验证。 %GT;在一个页面中。出于安全考虑,此功能可验证回发或回调事件的参数是否来自最初呈现它们的服务器控件。如果数据有效且符合预期,请使用ClientScriptManager.RegisterForEventValidation方法注册回发或回调数据以进行验证。

我的页面:

<div id="Users" align="center">
        <asp:Button ID="btnCreate" class="btn" Font-Bold="true" runat="server" OnClick="btnCreate_OnClick" />
        <br />
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" GridLines="None"
            CssClass="table table-bordered table-striped" HeaderStyle-Font-Size="14px">
            <Columns>
                <asp:BoundField DataField="UserName" />
                <asp:BoundField DataField="DisplayName" />
                <asp:BoundField DataField="Email" />
                <asp:BoundField DataField="ProfileName" />
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:ImageButton ID="btnEdit" ImageUrl="style/edit_16.png" runat="server" OnClick="btnEdit_OnClick" />
                        &nbsp;&nbsp;
                        <asp:ImageButton ID="btnDelete" ImageUrl="style/delete_16.png" runat="server" OnClick="btnDelete_OnClick" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </div>

我的活动:

protected void btnDelete_OnClick(object sender, EventArgs e)
{
    Button btn = (Button)sender;
    GridViewRow row = (GridViewRow)btn.NamingContainer;
    string UserName = row.Cells[0].Text;

    using (objConexao = new SqlConnection(strStringConexao))
    {
        using (objCommand = new SqlCommand(strDeleteUser, objConexao))
        {
            try
            {
                objConexao.Open();
                objCommand.Parameters.AddWithValue("@UserName", UserName);
                var objDataReader = objCommand.ExecuteReader();
            }
            finally
            {
                objConexao.Close();
            }
        }
    }
}

我的Page_Load和CarregaGrid()

public void CarregaGrid()
{
    var listaRequest = new UsersNDR().ConsultUsers();
    if (listaRequest != null)
    {
        this.GridView1.DataSource = listaRequest;
        this.GridView1.DataBind();

        foreach (GridViewRow row in GridView1.Rows)
        {
            string idioma = CultureInfo.CurrentCulture.TwoLetterISOLanguageName.ToString();

            if (Session["idioma"].ToString() != null)
            {
                idioma = Session["idioma"].ToString();
            }

            ImageButton btnDelete = (ImageButton)row.FindControl("btnDelete");
            ImageButton btnEdit = (ImageButton)row.FindControl("btnEdit");
            btnDelete.ToolTip = Idioma.RetornaMensagem("btnDelete");
            btnEdit.ToolTip = Idioma.RetornaMensagem("btnEdit");
        }
    }
}

protected void Page_Load(object sender, EventArgs e)
{
    string idioma = CultureInfo.CurrentCulture.TwoLetterISOLanguageName.ToString();

    if (Session["idioma"].ToString() != null)
    {
        idioma = Session["idioma"].ToString();
    }

    Idioma.MudaCultura(idioma);
    btnCreate.Text = Idioma.RetornaMensagem("btnCreate");
    GridView1.Columns[0].HeaderText = Idioma.RetornaMensagem("UserName");
    GridView1.Columns[1].HeaderText = Idioma.RetornaMensagem("DisplayName");
    GridView1.Columns[2].HeaderText = Idioma.RetornaMensagem("email");
    GridView1.Columns[3].HeaderText = Idioma.RetornaMensagem("ProfileName");
    GridView1.Columns[4].HeaderText = Idioma.RetornaMensagem("Action");
    CarregaGrid();
}

1 个答案:

答案 0 :(得分:5)

我相信在每个帖子上重新绑定网格的事实是问题,尝试在!IsPostBack中包装网格改变逻辑,如下所示:

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        string idioma = CultureInfo.CurrentCulture.TwoLetterISOLanguageName.ToString();

        if (Session["idioma"].ToString() != null)
        {
            idioma = Session["idioma"].ToString();
        }

        Idioma.MudaCultura(idioma);
        btnCreate.Text = Idioma.RetornaMensagem("btnCreate");
        GridView1.Columns[0].HeaderText = Idioma.RetornaMensagem("UserName");
        GridView1.Columns[1].HeaderText = Idioma.RetornaMensagem("DisplayName");
        GridView1.Columns[2].HeaderText = Idioma.RetornaMensagem("email");
        GridView1.Columns[3].HeaderText = Idioma.RetornaMensagem("ProfileName");
        GridView1.Columns[4].HeaderText = Idioma.RetornaMensagem("Action");
        CarregaGrid();
    }
}