1

我试图控制编辑模板,它是行命令事件中的复选框,但我无法获得它,但我得到了行索引中的标签控制。

我尝试了上面的代码来获得控制权:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    GridViewRow gvr = (GridViewRow)(((ImageButton)e.CommandSource).NamingContainer);

    Label icllbl = (Label)GridView1.Rows[gvr.RowIndex].FindControl("icllbl");
    CheckBox iclcb = (CheckBox)GridView1.Rows[gvr.RowIndex].FindControl("iclcb");

    if (e.CommandName.Equals("Edit"))
    {                
        if (icllbl.Text == "Y")
        {
            iclcb.Checked = true;
        }

    }
}

我也尝试了 RowDataBound 事件,幸运的是我在这里获得了复选框控件,但是这次我无法在下面的代码中获得 Label 控件:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{

    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        Label icllbl = (Label)e.Row.FindControl("icllbl");

        if ((e.Row.RowState & DataControlRowState.Edit) > 0)
        {

            CheckBox iclcb = (CheckBox)e.Row.FindControl("iclcb");
            if (icllbl.Text == "Y")
            {
                iclcb.Checked = true;
            }
        }
    }
}

如果我在任何地方错了,请纠正我。

提前致谢!

4

1 回答 1

0

In your RowCommand event use control class to cast into GridViewRow:

GridViewRow row = (GridViewRow)(((Control)e.CommandSource).NamingContainer);
int rowIndex = row.RowIndex;

And in RowDataBound event place Label control inside Edit (check for EditTemplate) check:

if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
    Label icllbl = (Label)e.Row.FindControl("icllbl");
    CheckBox iclcb = (CheckBox)e.Row.FindControl("iclcb");

    //... other code of lines will be here
}
于 2018-03-14T13:39:18.513 回答