0

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.

trevorc
  • 3,023
  • 4
  • 31
  • 49

1 Answers1

0

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.

DOK
  • 32,337
  • 7
  • 60
  • 92
  • That always gets me. Inded, if I only call that method on first page_load my event can fire. However, because my html controls are created in code behind they have be loaded on every page load. This is kind of where I started. I can't seem to find the right point in time to load my server controls in the middle of my html string controls. – trevorc Nov 09 '10 at 20:56