在GridView中从动态添加的模板字段中找到id的控件

时间:2012-04-09 06:48:36

标签: asp.net gridview

您好我通过实施ITemplate接口动态地将模板字段添加到gridview。 模板字段包含一些控件,如标签和文本框。我如何在行数据绑定事件中获得这些控件。

当我从aspx页面添加templatefield时,我正常做gridviewrow.findcontrol(“id”)时无法得到。

我添加模板字段的方式就像这样

public class CustomGridViewColumn : ITemplate { ListItemType _liType; string _columnName;

        public CustomGridViewColumn(ListItemType type, string column)
        {
            _liType = type;
            _columnName = column;

        }
        void ITemplate.InstantiateIn(System.Web.UI.Control container)
        {
            switch (_liType)
            {
                case ListItemType.Header:
                    Label lblHeader = new Label();
                    lblHeader.Text = _columnName;
                    container.Controls.Add(lblHeader);
                    break;
                case ListItemType.Item:
                    Label lblItem = new Label();
                    lblItem.DataBinding += new EventHandler(lbl_DataBinding);
                    lblItem.ID = "lbl" + _columnName;
                    lblItem.ClientIDMode = ClientIDMode.Predictable;
                    container.Controls.Add(lblItem);

                    DropDownList ddl = new DropDownList();

                    ddl.DataBinding += new EventHandler(ddl_DataBinding);
                    ddl.ID = "ddl" + _columnName;
                    ddl.Visible = false;
                    container.Controls.Add(ddl);
                    break;


            }
        }

}

现在我想访问我使用此代码添加的标签和下拉列表。 当我做gridviewrow.findcontrol(“id”)我没有得到它们。 任何人都可以帮助我。

当我浏览所有行并尝试查找时,我正在打算 当我选择它时,我有一个复选框,所有标签应该是diappear和ddls dhould出现 为此我正在使用以下代码。

protected void chkEdit_CheckedChanged(object sender, EventArgs e) { CheckBox chkEditTest = (CheckBox)sender; GridViewRow grow = (GridViewRow)chkEditTest.NamingContainer; DropDownList ddl = (DropDownList)grow.FindControl("ddl"); Label lbl= (Label)grow.FindControl("lbl"); }

当我这样做时,我无法获得控件。 似乎控制在回发上是令人失望的..

2 个答案:

答案 0 :(得分:0)

这是我提出的,我可以在后面的代码中获得控件引用。

    public class CustomGridViewColumn : ITemplate
    {
    ListItemType _liType; string _columnName;

    public CustomGridViewColumn(ListItemType type, string column)
    {
        _liType = type;
        _columnName = column;

    }

    void ITemplate.InstantiateIn(Control container)
    {
        switch (_liType)
        {
            case ListItemType.Header:
                Label lblHeader = new Label();
                lblHeader.Text = _columnName;
                container.Controls.Add(lblHeader);
                break;
            case ListItemType.Item:
                Label lblItem = new Label();
                lblItem.DataBinding += new EventHandler(lblItem_DataBinding);
                lblItem.ID = "lbl" + _columnName;
                lblItem.ClientIDMode = ClientIDMode.Predictable;
                container.Controls.Add(lblItem);

                DropDownList ddl = new DropDownList();

                ddl.DataBinding += new EventHandler(ddl_DataBinding);
                ddl.ID = "ddl" + _columnName;
                ddl.Visible = false;
                ddl.DataSource = new string[] { "Hello", "World" };
                container.Controls.Add(ddl);
                break;


        }
    }

    void ddl_DataBinding(object sender, EventArgs e)
    {

    }

    void lblItem_DataBinding(object sender, EventArgs e)
    {

    }

}

 protected void Page_Load(object sender, EventArgs e)
 {

    DataTable dt = new DataTable();
    dt.Columns.Add("Name");
    DataRow oItem = dt.NewRow();
    oItem[0] = "Deepu";
    dt.Rows.Add(oItem);
    oItem = dt.NewRow();
    oItem[0] = "MI";
    dt.Rows.Add(oItem);
    GridView gv = new GridView();
    gv.ID = "myGridView";
    gv.AutoGenerateColumns = false;
    BoundField nameColumn = new BoundField();
    nameColumn.DataField = "Name";
    nameColumn.HeaderText = "Name";
    gv.Columns.Add(nameColumn);

    TemplateField TmpCol = new TemplateField();
    TmpCol.HeaderText = "Template Column";
    gv.Columns.Add(TmpCol);
    TmpCol.ItemTemplate = new CustomGridViewColumn(ListItemType.Item, "TEST");
    gv.DataSource = dt;
    gv.DataBind();
    Form.Controls.Add(gv);


}
protected void Button1_Click(object sender, EventArgs e)
{
    GridView gv = Form.FindControl("myGridView") as GridView;
    foreach (GridViewRow item in gv.Rows)
    {
        var ddl = item.FindControl("ddlTest") as DropDownList;
        if (ddl != null)
        {
            ddl.Visible = true;
        }

        var lbl = item.FindControl("lbl") as Label;
        if (lbl != null)
        {
            lbl.Text = "hello";
        }
    }
}

<form id="form1" runat="server">


<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />


</form>

由于

迪普

答案 1 :(得分:-2)

您可以尝试使用GRIDVIEW控件的行索引

var rowIndex = int.Parse(e.CommandArgument)
GridView1.Rows[rowIndex].FindControl("id")

另请参阅

http://forums.asp.net/t/998368.aspx/1

http://www.codeproject.com/Articles/12021/Accessing-the-different-controls-inside-a-GridView

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewrow.aspx

由于

迪普