繁体   English   中英

为什么复合控件的点击事件无法触发?

[英]Why composite control click event not firing?

我写了一个名为(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事件上呢?

经过十天的谷歌搜索和测试一些方法后,我找到了一种解决方案,可以拦截发送给用户的类对象和HTML标签后面的代码。 因此,我没有使用复合控件,而是使用HTTPModule“ myModule1”来捕获Ajax请求,然后创建所需的HTML标记并将其发送给用户。 有关详细信息,请参见这篇文章: 如何在IHttpModule中搜索和替换requestContext.Response?

噩梦还没有结束! 经过上述方法,您应该在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中获取请求有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM