为什么复合控件单击事件不触发?

问题描述 投票:0回答:1

我写命名为(WebDbConnection)复合asp.net控制来管理我的数据库连接字符串。我在“的CreateChildControls”方法添加一个按钮“btnConnect的”创造click事件吧。

   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的”单击事件不触发,只有回传的形式出现postbackes!

几个小时内,我发现,当我添加WebDbConnection类的实例,形成在Page_Load事件中,我控制做工精细的控制。

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

但怎么做我不控制客户的Page_Load事件这里面添加控件?

c# asp.net custom-controls
1个回答
0
投票

之后谷歌搜索和测试一些方法的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);
       }

所以每当这个对象引用,该“初始化”方法被调用和登记到的web.config发生。

我希望这种方法是有帮助开发者创建Ajax调用,并得到自身的SDK请求。

© www.soinside.com 2019 - 2024. All rights reserved.