简体   繁体   中英

Gridview cell value on RowCommand

I'm attempting to get the cell value from a gridview but running into a few issues. I have setup my code similar to this example except that I'm adding my button fields on the server side. For some reason RowCommand is firing twice when clicking the cell values? TIA for any help

Page Load is empty: code

 protected void Page_Load(object sender, EventArgs e)
{

}

Adding Button Field: code

   foreach (DataColumn col in transposedTable.Columns)
    {

        ButtonField bfield = new ButtonField();



        bfield.DataTextField = col.ColumnName;

        bfield.HeaderText = col.ColumnName;

        bfield.CommandName = "ColumnClick";

        gvTest.Columns.Add(bfield);

    }

The RowDataBound and RowCommand events are the same as the example link above

This is a known bug. I had the same problem.

I removed the Handles GridView.RowCommand from the code behind event declaration, and added this line to the properties declaration for the grid in the .aspx source:

OnRowCommand="GridView_RowCommand"

Worked perfectly.

ok...There seems to be an open bug in MSFT connect... GridView RowCommad Event Firing Twice

seems like there are some workarounds posted in the Workarounds tab...

Hope this helps...

One way to access row values in a grid is to use the Cells collection along with the grid's current index (which you can access via the eventargs) as shown below

void YOUR_GRID_EVENT(Object sender, GridViewDeleteEventArgs e) 
{
    Grid.Rows[e.RowIndex].Cells[0];
}

you can also make use of the findcontrol as below:

var txtName = e.Row.FindControl("txtName") as TextBox;

Hope this helps...

Update

Also check your code to make sure that you are calling the GridView's DataBind() method appropriately...because every time you call GridView.DataBind()...the rowcommand event of the grid view gets called...I think (guessing) you currently have a gridView.DataBind() in your onload as well as in the button click event..so that might cause the rowcommand event to be called twice...if this is not the case then post some code so that we can explore more...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM