为什么复合控件点击事件不触发?
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" click 事件未触发并且仅出现回发表单!!!
几个小时后,我发现当我添加 WebDbConnection class 实例以在 page_load 事件中形成控件时,我的控件工作正常。
protected void Page_Load(object sender, EventArgs e)
{
Page.Form.Controls.Add(WebDbConnection_instance);
}
但是如何在我的控件中添加控件而不是在客户 page_load 事件中添加控件?
经过 10 天的谷歌搜索和测试一些方法后,我找到了一个解决方案来拦截我发送给用户的 class 对象和 HTML 标签背后的代码。
因此,我没有使用复合控件,而是使用 HTTPModule "myModule1" 来捕获 Ajax 请求,然后创建我需要的 HTML 标记并将其发送给用户。
有关详细信息,请参阅此 post:How to search and replace the requestContext.Response in a IHttpModule?
噩梦没有结束!!!
使用上述方法后,您应该在 web.config 文件(<system.webserver>
标记)中注册 HTTPModule,因此我想在需要时动态注册 "myModule1"。为了实现这一点,我在 "myModule1" class.
顶部使用了以下指令
[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。
我希望这种方法有助于开发人员在自己的 SDK 中创建 Ajax 调用和获取请求。
我编写了一个名为 (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" click 事件未触发并且仅出现回发表单!!!
几个小时后,我发现当我添加 WebDbConnection class 实例以在 page_load 事件中形成控件时,我的控件工作正常。
protected void Page_Load(object sender, EventArgs e)
{
Page.Form.Controls.Add(WebDbConnection_instance);
}
但是如何在我的控件中添加控件而不是在客户 page_load 事件中添加控件?
经过 10 天的谷歌搜索和测试一些方法后,我找到了一个解决方案来拦截我发送给用户的 class 对象和 HTML 标签背后的代码。 因此,我没有使用复合控件,而是使用 HTTPModule "myModule1" 来捕获 Ajax 请求,然后创建我需要的 HTML 标记并将其发送给用户。 有关详细信息,请参阅此 post:How to search and replace the requestContext.Response in a IHttpModule?
噩梦没有结束!!!
使用上述方法后,您应该在 web.config 文件(<system.webserver>
标记)中注册 HTTPModule,因此我想在需要时动态注册 "myModule1"。为了实现这一点,我在 "myModule1" class.
[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。
我希望这种方法有助于开发人员在自己的 SDK 中创建 Ajax 调用和获取请求。