【问题标题】:unable to find edit template control checkbox in row command无法在行命令中找到编辑模板控制复选框
【发布时间】:2018-03-14 06:05:14
【问题描述】:

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

我尝试了上面的代码来获取控件:

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

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

提前致谢!

【问题讨论】:

  • Windows form application吗?
  • 不,这是一个网站@gdmanandamohon

标签: c# asp.net gridview rowdatabound rowcommand


【解决方案1】:

在您的 RowCommand 事件中,使用 control 类转换为 GridViewRow

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

并在 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
}

【讨论】:

  • 好吧,我已经尝试过您的方式,这与我发布的代码相同,您刚刚在编辑状态下使用了该标签。这将不起作用,因为一旦 Gridview 处于编辑状态,它就无法读取项目模板控件
猜你喜欢
  • 1970-01-01
  • 2015-12-30
  • 1970-01-01
  • 2013-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-17
相关资源
最近更新 更多