来自客户端的火灾服务器事件
我在 Asp.net 页面生命周期方面遇到问题。我正在尝试使用 HtmlTextWriter 和 asp.net LinkButton 创建自定义菜单来触发服务器事件。我无法触发服务器事件,当我单击链接按钮时,我收到“对象引用未设置为对象实例”。这是一些代码。
protected string CreateModuleMenu()
{
var modules = ModuleManager.GetModulesByDeveloperId(Developer.DeveloperID);
StringWriter sw = new StringWriter();
ClientScriptManager cs = Page.ClientScript;
using (HtmlTextWriter writer = new HtmlTextWriter(sw))
{
foreach (var module in modules)
{
writer.RenderBeginTag(HtmlTextWriterTag.Li);
writer.RenderBeginTag(HtmlTextWriterTag.Dl);
writer.RenderBeginTag(HtmlTextWriterTag.Dt);
writer.Write(module.Name);
var files = ModuleManager.GetModuleFilesByModuleId(module.ModuleID);
foreach (var file in files)
{
writer.RenderBeginTag(HtmlTextWriterTag.Dd);
LinkButton lb = new LinkButton();
lb.ID = "mc" + file.ModuleFileID;
lb.Attributes.Add("onclick", cs.GetPostBackEventReference(lb, "LoadControl_Clk"));
lb.Text = file.Name;
Page.RegisterRequiresRaiseEvent(lb);
lb.RenderControl(writer);
writer.RenderEndTag();
}
writer.RenderEndTag();
writer.RenderEndTag();
writer.RenderEndTag();
}
}
return sw.ToString();
这是我的点击事件:
protected void LoadControl_Clk(object sender, EventArgs e)
{
Response.Write("Hello World");
}
最后,这是我在 Page_Load 事件中的内容。注意:我尝试将其移动到 PreRender、PreInt 等。
protected void Page_Load(object sender, EventArgs e)
{
LiteralControl lit = new LiteralControl();
lit.Text = CreateModuleMenu();
phModuleMenu.Controls.Add(lit);
if (DefaultModuleFile == null)
Response.Write("Error.");
else
{
Control ctrl = LoadControl(DefaultModuleFile.Src);
phAdminModules.Controls.Add(ctrl);
}
}
丢失了。谢谢。
I am having trouble with the Asp.net page life cycle. I am trying to create a custom menu using HtmlTextWriter with an asp.net LinkButton to fire a server event. I can not get the server event to fire and I get the 'object reference not set to instance of object' when I click my linkbutton. Here is some code.
protected string CreateModuleMenu()
{
var modules = ModuleManager.GetModulesByDeveloperId(Developer.DeveloperID);
StringWriter sw = new StringWriter();
ClientScriptManager cs = Page.ClientScript;
using (HtmlTextWriter writer = new HtmlTextWriter(sw))
{
foreach (var module in modules)
{
writer.RenderBeginTag(HtmlTextWriterTag.Li);
writer.RenderBeginTag(HtmlTextWriterTag.Dl);
writer.RenderBeginTag(HtmlTextWriterTag.Dt);
writer.Write(module.Name);
var files = ModuleManager.GetModuleFilesByModuleId(module.ModuleID);
foreach (var file in files)
{
writer.RenderBeginTag(HtmlTextWriterTag.Dd);
LinkButton lb = new LinkButton();
lb.ID = "mc" + file.ModuleFileID;
lb.Attributes.Add("onclick", cs.GetPostBackEventReference(lb, "LoadControl_Clk"));
lb.Text = file.Name;
Page.RegisterRequiresRaiseEvent(lb);
lb.RenderControl(writer);
writer.RenderEndTag();
}
writer.RenderEndTag();
writer.RenderEndTag();
writer.RenderEndTag();
}
}
return sw.ToString();
Here is my click event:
protected void LoadControl_Clk(object sender, EventArgs e)
{
Response.Write("Hello World");
}
Finally here is what I have in the Page_Load event. Note: I tried moving this around to PreRender, PreInt, etc.
protected void Page_Load(object sender, EventArgs e)
{
LiteralControl lit = new LiteralControl();
lit.Text = CreateModuleMenu();
phModuleMenu.Controls.Add(lit);
if (DefaultModuleFile == null)
Response.Write("Error.");
else
{
Control ctrl = LoadControl(DefaultModuleFile.Src);
phAdminModules.Controls.Add(ctrl);
}
}
Lost. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许您只需要在第一次调用页面时加载Page_Load中的控件,即if !IsPostBack。
如果您在每次页面加载时加载该控件,您将丢失被触发的事件。
Perhaps you need to only load the control in Page_Load on the first time the page is called, that is, if !IsPostBack.
If you load the control on every page load, you will lose the event being fired.