【问题标题】:Why composite control click event not firing?为什么复合控件点击事件不触发?
【发布时间】:2019-01-23 15:22:34
【问题描述】:

我编写了一个名为 (WebDbConnection) 的复合 asp.net 控件来管理我的数据库的连接字符串。 我添加了一个按钮“btnConnect”并在“CreateChildControls”方法中为其创建了点击事件。

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

这个想法是当我在内部调用“ShowPage”方法时显示复合控件。 问题是当我在其他页面中创建复合控件时,“ShowPage”工作正常,但在回发时“BtnConnect”点击事件没有触发,只发生回发表单!!!

在几个小时内,我发现当我在 page_load 事件中添加 WebDbConnection 类的实例以形成控件时,我的控件工作正常。

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

但是如何在我的控件中添加控件而不是在客户 page_load 事件上?

【问题讨论】:

    标签: c# asp.net custom-controls


    【解决方案1】:

    经过 10 天的谷歌搜索和一些方法的测试,我找到了一个解决方案,可以拦截我在类对象和 HTML 标记后面发送给用户的代码。 所以我没有使用复合控件,而是使用 HTTPModule "myModule1" 来捕获 Ajax 请求,然后创建我需要的 HTML 标记并将其发送给用户。 详情见此贴:How to search and replace the requestContext.Response in a IHttpModule?

    噩梦还没有结束!!! 在上述方法之后,您应该在 web.config 文件(<system.webserver> 标签)中注册 HTTPModule,所以我想在需要时动态注册“myModule1”。为了实现这一点,我在“myModule1”类的顶部使用了以下指令。

    [assembly: PreApplicationStartMethod(typeof(DreamyTools.DataBase.Web.MyModule1), "Initialize")]
     namespace DataBase.Web
      {
       public class MyModule1 : IHttpModule
       {
         public static void Initialize()
          {
               Microsoft.Web.Infrastructure.DynamicModuleHelper
               .DynamicModuleUtility.RegisterModule(MyModule1);
           }
    

    因此,每当引用此对象时,都会调用“Initialize”方法并注册到 web.config。

    我希望这种方法有助于开发人员创建 Ajax 调用并在自己的 SDK 中获取请求。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-27
      • 1970-01-01
      • 2011-11-19
      相关资源
      最近更新 更多