locked
HTMLButton in custom Server control, click event not firing... :( RRS feed

  • Question

  • User33725395 posted

    Well, this is first time i am creating a server control...

    Idea is to create databind control that displays data in DataTable on page.

    it has asp HTML controls, HTMLButtons.

    Now, i have registered event handler to this button "NewTopicBtn", but when i click this button,

    event is not fired...

    What is problem here, what am i missing... ?

    Does this have to do with ViewState, in this case how to handle it..

    Server Control COde..

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Text;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    using System.Data;
    
    namespace MySample2
    {
        [DefaultProperty("MyDataSource")]
        [ToolboxData("<{0}:MyCtrl runat=server></{0}:MyCtrl>")]
        public class MyCtrl : WebControl
        {
            private System.Web.UI.HtmlControls.HtmlTable Table;
            private System.Web.UI.HtmlControls.HtmlTableRow [] TRow;
            //private System.Web.UI.HtmlControls.HtmlTableCellCollection [] TRowCellCollc;
    
            private System.Web.UI.HtmlControls.HtmlButton[] EditBtns;
            private System.Web.UI.HtmlControls.HtmlTextArea[] TextBoxs;
    
    
            private System.Web.UI.HtmlControls.HtmlButton NewTopicBtn;
            public event EventHandler BtnNewTopicClick;
    
            public int TextBoxRowsNo { get; set; }
    
            private readonly int DefaultRowNo = 3;
    
            [Bindable(true)]
            [Category("Data")]
            [DefaultValue("")]
            [Localizable(true)]
            public virtual DataTable MyDataSource
            {
                get
                {
                    return (DataTable)ViewState["MyDataSource"];
                }
    
                set
                {
                    ViewState["MyDataSource"] = value;
                }
            }
    
            protected override void CreateChildControls()
            {
                Controls.Clear();
                ClearChildViewState();
                TrackViewState();
                BindDataSource();
    
                ChildControlsCreated = true; 
            }
    
            private void BindDataSource()
            {
                if (MyDataSource != null && MyDataSource.Rows.Count > 0)
                {
                    Table = new System.Web.UI.HtmlControls.HtmlTable();
                    Table.EnableViewState = true;
                    Table.ID = "TopicTable";
                    Table.ClientIDMode = System.Web.UI.ClientIDMode.Predictable;
    
                    
                    NewTopicBtn = new System.Web.UI.HtmlControls.HtmlButton();
                    NewTopicBtn.ViewStateMode = System.Web.UI.ViewStateMode.Enabled;
                    NewTopicBtn.ID = "btnNewTopic";
                    NewTopicBtn.EnableViewState = true;
                    NewTopicBtn.EnableTheming = true;
                    NewTopicBtn.ClientIDMode = System.Web.UI.ClientIDMode.Predictable;
                    NewTopicBtn.CausesValidation = true;
                    NewTopicBtn.Attributes.Add("runat", "server");
                    NewTopicBtn.InnerText = "New Topic";
                    NewTopicBtn.ServerClick += new EventHandler(NewTopicBtn_ServerClick);
                    System.Web.UI.HtmlControls.HtmlTableCell cellNew = new System.Web.UI.HtmlControls.HtmlTableCell();
                    cellNew.Controls.Add(NewTopicBtn);
                    
    
                    TRow = new System.Web.UI.HtmlControls.HtmlTableRow[MyDataSource.Rows.Count+2];
                    TRow[0] = new System.Web.UI.HtmlControls.HtmlTableRow();
                    TRow[0].Cells.Add(cellNew);
                    Table.Rows.Add(TRow[0]);
    
                    EditBtns = new System.Web.UI.HtmlControls.HtmlButton[MyDataSource.Rows.Count];
                    
                    TextBoxs = new System.Web.UI.HtmlControls.HtmlTextArea[MyDataSource.Rows.Count];
                    
                    int RowIndex = 1;
                    int CtrlIndex = 0;
                    foreach (DataRow dataRow in MyDataSource.Rows)
                    {
    
                        TRow[RowIndex] = new System.Web.UI.HtmlControls.HtmlTableRow();
                        if(RowIndex == 1)
                            TRow[RowIndex+1] = new System.Web.UI.HtmlControls.HtmlTableRow();
                        foreach (DataColumn dataColumn in MyDataSource.Columns)
                        {
                            System.Web.UI.HtmlControls.HtmlTableCell cellData = new System.Web.UI.HtmlControls.HtmlTableCell();
                            if (RowIndex == 1)
                            {
                                System.Web.UI.HtmlControls.HtmlTableCell cell = new System.Web.UI.HtmlControls.HtmlTableCell();
                                cell.InnerText = dataColumn.ColumnName;
                                TRow[RowIndex].Cells.Add(cell);
                                
                                cellData.InnerText = dataRow[dataColumn].ToString();
                                TRow[RowIndex+1].Cells.Add(cellData);
                            }
                            else
                            {
                                cellData.InnerText = dataRow[dataColumn].ToString();
                                TRow[RowIndex].Cells.Add(cellData);
                            }
                            
                        }
                        if (RowIndex != 1)
                        {
                            System.Web.UI.HtmlControls.HtmlTableCell cellCtrlbtn = new System.Web.UI.HtmlControls.HtmlTableCell();
                            EditBtns[CtrlIndex] = new System.Web.UI.HtmlControls.HtmlButton();
                            EditBtns[CtrlIndex].InnerText = "Edit";
                            EditBtns[CtrlIndex].ServerClick += new EventHandler(MyCtrl_ServerClick);
                            cellCtrlbtn.Controls.Add(EditBtns[CtrlIndex]);
                            TRow[RowIndex].Cells.Add(cellCtrlbtn);
                            
                            System.Web.UI.HtmlControls.HtmlTableCell cellCtrltxt = new System.Web.UI.HtmlControls.HtmlTableCell();
                            TextBoxs[CtrlIndex] = new System.Web.UI.HtmlControls.HtmlTextArea();
                            TextBoxs[CtrlIndex].Rows = (TextBoxRowsNo == 0) ? DefaultRowNo : TextBoxRowsNo;
                            cellCtrltxt.Controls.Add(TextBoxs[CtrlIndex]);
    
                            TRow[RowIndex].Cells.Add(cellCtrltxt);
                            Table.Rows.Add(TRow[RowIndex++]);
                        }
                        else
                        {
                            Table.Rows.Add(TRow[RowIndex++]);
                            Table.Rows.Add(TRow[RowIndex]);
                        }
                        CtrlIndex++;
                    }
    
                }
            }
    
            void MyCtrl_ServerClick(object sender, EventArgs e)
            {
                //throw new NotImplementedException();
                BtnNewTopicClick(sender, e);
            }
    
            void NewTopicBtn_ServerClick(object sender, EventArgs e)
            {
                throw new NotImplementedException();
            }
    
            protected override void RenderContents(HtmlTextWriter writer)
            {
                if(Table!=null)
                this.Table.RenderControl(writer);
                //this.htmSubmit.RenderControl(writer);
                //this._label.RenderControl(writer);
                //this._table.RenderControl(writer);
                //this._button.RenderControl(writer);
            }
            
        }
    }

    Page using Server control...


    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>
    
    <%@ Register assembly="MySample2" namespace="MySample2" tagprefix="cc1" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                ConnectionString="<%$ ConnectionStrings:ConnectionString1 %>" 
                SelectCommand="Select_Data2" SelectCommandType="StoredProcedure">
            </asp:SqlDataSource>
        </div>
        <cc1:MyCtrl ID="MyCtrl1" runat="server" />
        </form>
    </body>
    </html>
    


    Page cs code...

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    public partial class Default3 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            System.Data.DataTable dt = ((System.Data.DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty)).ToTable();
            MyCtrl1.BtnNewTopicClick += new EventHandler(MyCtrl1_BtnNewTopicClick);
            MyCtrl1.MyDataSource = dt;
    
        }
    
        void MyCtrl1_BtnNewTopicClick(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }
    }
    


     
    Wednesday, June 8, 2011 4:41 AM

Answers

  • User-16411453 posted
    It's not the anwser, but just a server control cleanup first. The event handler looks correct, and it's ok to fork out a module in onInit or createCHildControls.

    protected void NewTopicBtn_ServerClick(object sender, EventArgs e) { throw new NotImplementedException(); } protected override void Render(HtmlTextWriter writer) { RenderContents(writer); }


    Change your CreateChildControls to
    protected overrides void OnInit(object sender, EventArgs e)
            MyBase.OnInit(e)

    Table = new System.Web.UI.HtmlControls.HtmlTable();
                    Table.EnableViewState = true;
                    Table.ID = "TopicTable";
                    Table.ClientIDMode = System.Web.UI.ClientIDMode.Predictable;
    controls.Add(Table);
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 10, 2011 4:47 PM

All replies

  • User-1423995609 posted

    Add this to your Button click event handler in custom server control, you should raise event from Custom Control so it's container can handle it.

    void NewTopicBtn_ServerClick(object sender, EventArgs e)
            {
    if (BtnNewTopicClick != null)
      {
                 BtnNewTopicClick(this, new EventArgs());
      }
            }           
    Wednesday, June 8, 2011 5:02 AM
  • User-16411453 posted
    It's not the anwser, but just a server control cleanup first. The event handler looks correct, and it's ok to fork out a module in onInit or createCHildControls.

    protected void NewTopicBtn_ServerClick(object sender, EventArgs e) { throw new NotImplementedException(); } protected override void Render(HtmlTextWriter writer) { RenderContents(writer); }


    Change your CreateChildControls to
    protected overrides void OnInit(object sender, EventArgs e)
            MyBase.OnInit(e)

    Table = new System.Web.UI.HtmlControls.HtmlTable();
                    Table.EnableViewState = true;
                    Table.ID = "TopicTable";
                    Table.ClientIDMode = System.Web.UI.ClientIDMode.Predictable;
    controls.Add(Table);
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 10, 2011 4:47 PM
  • User-861155893 posted

    Event handlers should be assigned in the Init part of the page lifecycle (though sometimes they do work in other areas as I have seen.

    move:

                    NewTopicBtn.ServerClick += new EventHandler(NewTopicBtn_ServerClick);

    from the BindDataSource method to

    protected void Page_Init(object sender, EventArgs e)
    {
          NewTopicBtn.ServerClick += new EventHandler(NewTopicBtn_ServerClick);
    }
    Friday, June 24, 2011 5:23 AM