开发者

OnRowClick in a GridView not taking over button clicks?

I have a C#/ASP.net GridView that is contained within a UserControl. The way I have it now, gridview will have an onrowclick attribute added on as follows:

protected void _gridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
  e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(this.GridView, "EditRow$" + e.Row.RowIndex);
}

However, I get an invalid postback error due to EventValidation when this click event occurs. Also, I have buttons on the row that use GridViewRowCommands to redirect to other pages. Unfortunately, I can't get both to work together either (the RowCommand function never gets run).

For example, this is the handler I would use:

protected void _gridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
   if (e.CommandName.ToString() == "EditRow" && e.CommandArgument.ToString() == "0")
      Response.Redirect(anotherURL);
   if (e.CommandName.ToString() == "BACK" && e.CommandArgument.ToString() == "0")
      Response.Redirect(thisURL);
}

And this is a button I use on the row:

<asp:Button ID="_AddButton" runat="server" CommandName="BACK" CommandArgument="0" Text="Add" />开发者_JAVA百科

Ideally, the row-onclick will use this GridViewRowCommands as well so I can have the handling of the redirection all in one place. However, I'm not sure how to go about this. Any suggestions as to a better approach?

Thanks for your continued support!


I'm not sure how to add comments so an answer will have to do.

Are you saying it's not hitting the RowCommand method what-so-ever? If not I would consider wiring the event in the following manner via the Page Init.

GridView1.OnRowCommand += _gridView_RowCommand


In the page directive, add the following:

EnableEventValidation="false";

Should work fine after that. That's probably the simplest way around your problem.

You could also do something like this too:

e.Row.Attributes["onclick"] = "javascript:onRowClick('" + e.Row.RowIndex + "');";

And in the ASPX, add a JavaScript function:

onRowClick = function(rowIndex){
   __doPostBack("<%=dataGridView1.UniqueID%>", rowIndex);
}

And lastly, in your code behind, add the RaisePostBackEvent override:

protected override void RaisePostBackEvent(IPostBackEventHandler source, string eventArgument)
{
    base.RaisePostBackEvent(source, eventArgument);

    int rowIndex = ToInt32.Parse(eventArgument);
    if (source is DataGridView)
    {
        if (source == dataGridView1)
        {
             GridViewRow row = dataGridView1.Rows[rowIndex];
        }
    }
}


I handled a similar problem by appending a client side click event to each row.

  1. Add OnRowDataBound event handler to grid view.

    OnRowDataBound="GridView_RowDataBound
    
  2. In the OnRowDataBound event handler, add an "onclick" attribute that grabs the primary key for the record and passes that along in the redirect URL.

    protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            int id = Convert.ToInt32(e.Row.Cells[0].Text);
            e.Row.Attributes["onclick"] = string.Format("javascript: window.location = 'Edit.aspx?id={0}'", id);
        }
    }
    

Note that this assumes that the first column of your grid view is your primary key.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜