1

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

当我从 aspx 页面添加模板字段时,我无法像往常一样执行 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") 我没有得到它们。谁能帮帮我吗。

当我浏览所有行并尝试查找时,我感到很高兴,但是当我选择它时,我在一行中有一个复选框,所有标签都应该消失并且 ddls 应该出现,我正在使用以下代码。

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

当我这样做时,我无法获得控件。似乎控件在回发时消失了..

4

2 回答 2

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>

谢谢

迪普

于 2012-04-09T08:03:04.440 回答
-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

谢谢

迪普

于 2012-04-09T06:55:04.813 回答