Quantcast
Channel: .NET Framework Class Libraries forum
Viewing all articles
Browse latest Browse all 8156

Custom Control with Gridview and ITemplate column not firing events

$
0
0

Please help me, I need a click on the button to fire the b_click procedure and it's not working.
Here is my custom control with a gridview and a column added dynamically using a ITemplate class. Thanks


namespace NamespaceServerControlSearch
{
    [ToolboxData("<{0}:ServerControlSearch runat=server></{0}:ServerControlSearch>")]
    public class ServerControlSearch : CompositeControl
    {
        private global::System.Web.UI.WebControls.GridView grid;


        protected override void CreateChildControls()
        {
            Controls.Clear();

            grid = new GridView();
            grid.AutoGenerateColumns = true;
            grid.ID = "grid";
            grid.Width = Unit.Pixel(400);
            grid.Height = Unit.Pixel(100);
            grid.BorderStyle = BorderStyle.Solid;

            DataColumn col = new DataColumn("xxx");
            TemplateField bfield = new TemplateField();
            bfield.HeaderTemplate = new GridViewTemplateSelect(ListItemType.Header, col.ColumnName);
            bfield.ItemTemplate = new GridViewTemplateSelect(ListItemType.Item, col.ColumnName);
            grid.Columns.Add(bfield);

            DataSet ds = new DataSet();
            using (SqlConnection conn  = new SqlConnection("Data Source=(local); Initial Catalog=casproduccion; USER=sa; PASSWORD=Jotsa123"))
            {
                SqlDataAdapter da = new SqlDataAdapter("select codigo, nombre from CAS_USERS cu where codigo like 'F37008%'", conn);
                da.Fill(ds);
            }

            grid.DataSource = ds.Tables[0];
            grid.DataBind();

        }

        protected override void Render(HtmlTextWriter o)
        {
            o.Write("<div style=\"width: 420px; height: 100px; overflow: scroll; border: 1px solid black;\">");
            grid.RenderControl(o);
            o.Write("</div>");
        }

        protected override void RecreateChildControls()
        {
            EnsureChildControls();
        }
    }

    public class GridViewTemplateSelect : Control, ITemplate
    {
        private ListItemType _templateType;
        private string _columnName;

        public GridViewTemplateSelect(ListItemType type, string colname)
        {
            _templateType = type;
            _columnName = colname;
        }

        void ITemplate.InstantiateIn(System.Web.UI.Control container)
        {
            switch (_templateType)
            {
                case ListItemType.Header:
                    Label lbl = new Label();
                    lbl.Text = _columnName;
                    container.Controls.Add(lbl);
                    break;
                case ListItemType.Item:

                    Button b = new Button();
                    b.Text = "test";
                    //b.DataBinding += new EventHandler(b_click);
                    b.Click += new EventHandler(b_click);
                    container.Controls.Add(b);
                    break;
            }

        }

        public void b_click(object sender, EventArgs e)
        {
            Button bb = (Button)sender;
            bb.Text = bb.Text + "A";        //change name to test if it's receiving event
            GridViewRow container = (GridViewRow)bb.NamingContainer;
            string cid = container.Cells[1].Text;

        }
    }
}



Viewing all articles
Browse latest Browse all 8156

Trending Articles