简体   繁体   English

在行命令中找不到编辑模板控件复选框

[英]unable to find edit template control checkbox in row command

I tried to get the control of edit template which is checkbox in row command event but I am unable to get it, but I am getting label control which is in row index. 我试图获取编辑模板的控件,该控件是行命令事件中的复选框,但我无法获取它,但我正在获取行索引中的标签控件。

I tried the above below code to get the control: 我尝试了下面的代码来获取控件:

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;
        }

    }
}

And I tried RowDataBound event also luckily I am getting checkbox control here but this time I am unable to get the Label control in below code: 而且我还尝试了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;
            }
        }
    }
}

Please correct me if I am wrong anywhere. 如果我在任何地方错了,请纠正我。

Thanks in advance! 提前致谢!

In your RowCommand event use control class to cast into GridViewRow : 在您的RowCommand事件中,使用control类强制转换为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: 并在RowDataBound事件中将Label控件放置在Edit(检查EditTemplate)内部:

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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