簡體   English   中英

在行命令中找不到編輯模板控件復選框

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

我試圖獲取編輯模板的控件,該控件是行命令事件中的復選框,但我無法獲取它,但我正在獲取行索引中的標簽控件。

我嘗試了下面的代碼來獲取控件:

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

如果我在任何地方錯了,請糾正我。

提前致謝!

在您的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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM