none
Why composite control click event not firing? RRS feed

  • Question

  • hi dears.

    i wrote a composite asp.net control named as (WebDbConnection) to manage connection string of my database. i added a button "btnConnect" and created click event for it in "CreateChildControls" method.

     public WebDbConnection(string extraParameters, string configConnectionName, string databaseName,
            bool showDbName, string dialogName, bool isEncrypted,ref Page page)
        {
            _currentPage = page;
            _extraParameters = extraParameters;
            _dialogName = dialogName;
            IsEncrypted = isEncrypted;
            _showDbName = showDbName;
    
            _configConnectionName = configConnectionName;
            LoadDefaultConnectionString(IsEncrypted);
    
            _databaseName = databaseName;
    
    
        }
    protected void BtnConnect_Click(object sender, EventArgs e)
        {
            try
            {
                EnsureChildControls();
                _server = Dbconnection_txtServer.Text;
                _userName = Dbconnection_txtUser.Text;
                _password = Dbconnection_txtPass.Text;
                _databaseName = Dbconnection_txtdbname.Text;
    
                if (Connection.State == ConnectionState.Connecting)
                    return;
    
                Connect();
                if (Connection.State == ConnectionState.Open)
                    this.Controls.Clear();
                else
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "alertbox",
                        "alert('Can not connect to server.')", true);
    
                    Dbconnection_txtServer.Text = _server;
                    Dbconnection_txtUser.Text = _userName;
                    Dbconnection_txtPass.Text = _password;
                    Dbconnection_txtdbname.Text = _databaseName;
                }
    
            }
    
            catch (Exception ex)
            {
                LastError = ex.Message;
                LoadDefaultConnectionString(IsEncrypted);
                Dbconnection_txtServer.Text = _server;
                Dbconnection_txtPass.Text = _password;
                Dbconnection_txtUser.Text = _userName;
            }
        }    
        protected override void CreateChildControls()
        {
            Controls.Clear();
    
            lbl_HeaderName = new Label();
            lbl_HeaderName.ID = "lbl_HeaderName";
            lbl_HeaderName.Text = "Server Credential";
    
            lbl_servername = new Label();
            lbl_servername.ID = "lbl_servername";
            lbl_servername.Text = "Server Name";
    
            Dbconnection_txtServer = new TextBox();
            Dbconnection_txtServer.ID = "Dbconnection_txtServer";
    
            lbl_username = new Label();
            lbl_username.ID = "lbl_username";
            lbl_username.Text = "User Name:";
    
            Dbconnection_txtUser = new TextBox();
            Dbconnection_txtUser.ID = "Dbconnection_txtUser";
    
            lbl_password = new Label();
            lbl_password.ID = "lbl_password";
            lbl_password.Text = "Password:";
    
            Dbconnection_txtPass = new TextBox();
            Dbconnection_txtPass.ID = "Dbconnection_txtPass";
    
            lbl_databasename = new Label();
            lbl_databasename.ID = "lbl_databasename";
            lbl_databasename.Text = "Database Name:";
    
            Dbconnection_txtdbname = new TextBox();
            Dbconnection_txtdbname.ID = "Dbconnection_txtdbname";
    
            btnConnect = new Button();
            btnConnect.ID = "btnConnect";
            btnConnect.Text = "Connect";
            btnConnect.Click += BtnConnect_Click;
    
            btnCancel = new Button();
            btnCancel.ID = "btnCancel";
            btnCancel.Text = "Cancel";
            btnCancel.Click += BtnCancel_Click;
    
    
            //add controls
            Controls.Add(lbl_password);
            Controls.Add(lbl_databasename);
            Controls.Add(lbl_servername);
            Controls.Add(lbl_username);
            Controls.Add(lbl_HeaderName);
            Controls.Add(Dbconnection_txtdbname);
            Controls.Add(Dbconnection_txtPass);
            Controls.Add(Dbconnection_txtServer);
            Controls.Add(Dbconnection_txtUser);
            Controls.Add(btnConnect);
            Controls.Add(btnCancel);
        } 
    void ShowPage()
        {
            EnsureChildControls();
            if (!_currentPage.Form.Controls.Contains(this))
                _currentPage.Form.Controls.Add(this);
            else
            {
                _currentPage.Form.Controls.Remove(this);
                _currentPage.Form.Controls.Add(this);
            }
    
            Dbconnection_txtdbname.Text = _databaseName;
            if (!_showDbName)
            {
                lbl_databasename.Visible = false;
                Dbconnection_txtdbname.Visible = false;
            }
    
            //----------------------
            if (_dialogName.IsEmptyOrNull()) return;
            lbl_HeaderName.Text = "Fill " + _dialogName + " Information";
        }

    The idea is to show composite control when i call "ShowPage" method internally. the issue is when i create composite control in other pages,"ShowPage" work correctly but on the postbackes the "BtnConnect" click event not firing and only postback form occurs!!!

    within couple of hours i found that when i add instance of WebDbConnection class to form controls in page_load event, my control works fine.

    protected void Page_Load(object sender, EventArgs e)
    {
        Page.Form.Controls.Add(WebDbConnection_instance);
    }

    but how to do this adding controls inside my control not on customer page_load event?


    Wednesday, January 23, 2019 3:39 PM

All replies

  • Hi Mohammad Reza Sheikh,

    Since your question is more related to ASP.NET, you could post a new thread in ASP.NET forum for suitable support.

    https://www.asp.net/

    The Visual C# forum discuss and ask questions about the C# programming language, IDE, libraries, samples, and tools.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, January 25, 2019 6:46 AM