开发者

asp.net built runtime Menu with offsite link

I hope you can help me. First, I'd like to tell you I am a desktop app guy, which means I mostly develop my apps in desktop. Now I am trying to build some web app but it leads me to am not sure if confusion or just am doing it wrong.

I have a code here that it populates a menu at runtime. Runtime because they the menu items are populated at code behind and the items are fetched in database.

here's the code behind:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        Menus menu = new Menus();
        imgMainLogo.ImageUrl = VARIABLES.MainLogoImage;
        menu.PopulateMenuControl(ref mainmenu, 2);
        menu.PopulateMenuControl(ref footermenu, 9);
    }
    else
    {
        System.Diagnostics.Debug.WriteLine("link: " + footermenu.SelectedValue);
        if (footermenu.SelectedValue != null)
        {
            Response.Redirect(footermenu.SelectedValue, true);
        }
    }
}

and the code in PopulateMenuControl

public void PopulateMenuControl(ref Menu menucontrol, int menuparentid)
{
    //menucontrol.Items.Clear();

    foreach (MenuFields mf in GetMenusByParentID(menuparentid))
    {
        MenuItem menuitem = new MenuItem(mf.MenuName, ReplaceSystemNameLink(mf.Link));
        menucontrol.Items.Add(menuitem);

        foreach (MenuFields cmf in GetMenusByParentID(mf.MenuID))
        {
            MenuItem childmenuitem = new MenuItem(cmf.MenuName, ReplaceSystemNameLink(cmf.Link));
            menuitem.ChildItems.Add(childmenuitem);
        }
    }
}

So Page.IsPostBack is the very basic thing I should learn when doing something in a page. But the problem here is, one of my menu item in "footermenu" has an offsite link, and it should redirect the page into my blog.. but what's happening was, footermenu.SelectedValue is empty once I clicked on the "Blog" link.

What's going on?


UPDATE

I have updated the code still stuck, the SelectedValue is still empty

protected void Page_Load(object sender, EventArgs e)
{
    System.Diagnostics.Debug.WriteLine("Page_Load IsPostBack: " + Page.IsPostBack.ToString());
    if (Page.IsPostBack)
    {
        if(footermenu.SelectedValue != null) 
        {
            System.Diagnostics.Debug.WriteLine("link: " + footermenu.SelectedValue);
        }
    }
}

protected void Page_Ini开发者_开发技巧t(object sender, EventArgs e)
{
    System.Diagnostics.Debug.WriteLine("Page_Init IsPostBack: " + Page.IsPostBack.ToString());
    if (!Page.IsPostBack)
    {
        Menus menu = new Menus();
        imgMainLogo.ImageUrl = VARIABLES.MainLogoImage;
        menu.PopulateMenuControl(ref mainmenu, 2);
        menu.PopulateMenuControl(ref footermenu, 9);
    }
}


You need to learn about the page lifecycle.

With dynamic controls (created and added in code), you need to re-create them on every page load - this is best done in the init event handler.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜