开发者

How to find control on GridView RowCommand Event?

开发者 https://www.devze.com 2023-02-07 23:50 出处:网络
i tried something like this but did not work: GridViewRow row = (GridViewRow)(((Repeater)e.CommandSource).NamingContainer);

i tried something like this but did not work:

GridViewRow row = (GridViewRow)(((Repeater)e.CommandSource).NamingContainer); 
Repeater _rpt1 = row.Cells[8].FindControl("rptReg") as Repeater;

error:

Unable to cast object of type 'System.Web.UI.WebControls.Button' to type 'System.Web.UI.WebControls.Repeater'.

is there a way i can have the below code in OnRowCommand event using GridView?

actually i am trying to delete the row from the gridview control and when the user click on it and i am grabbing the mulitple ids and passing to SP and update the table and databind the gridview

GridViewRow row = gv.SelectedRow;           
    Repeater _rpt = gv.Rows[e.RowIndex].Cells[8].FindControl("rptReg") as Repeater;
    Repeater _rpt1 = gv.Rows[e.RowIndex].Cells[9].FindControl("rptVisitor") as Repeater;

    foreach (RepeaterItem item in _rpt.Items)
    {
        TextBox _txt = item.FindControl("tx开发者_开发技巧tId") as TextBox;
        TextBox _txt1 = item.FindControl("txtName") as TextBox;
        //update db
    }


Please try this:

var viewRow = (GridViewRow)(((ImageButton)e.CommandSource).NamingContainer);
foreach (GridViewRow gvr in gvDetails.Rows)
{
    var btnSelect = (ImageButton)viewRow.FindControl("btnSelect");
    if (gvr == viewRow)
    {
        if (btnSelect.ImageUrl.ToLower() == "~/images/rbnormal.jpg")
        {
            btnSelect.ImageUrl = "~/Images/rbSelected.jpg";
            btnSelect.Enabled = false;
        }
    }
    else
    {
        btnSelect.ImageUrl = "~/Images/rbnormal.jpg";
        btnSelect.Enabled = true;
    }
}


Your error is telling me that the control named "rptReg" is a button.

The GridView has a property called DataKeys which can hold multiple IDs So you would pull out the "PersonId" like:

     string personId = GridView1.DataKeys[e.RowIndex]["PersonId"].ToString();
0

精彩评论

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