asp.net 构建了带有异地链接的运行时菜单
我希望你能帮助我。首先,我想告诉你我是一个桌面应用程序开发者,这意味着我主要在桌面上开发我的应用程序。现在我正在尝试构建一些网络应用程序,但这让我不确定是否感到困惑或只是做错了。
我这里有一个代码,它在运行时填充菜单。运行时,因为菜单项是在代码后面填充的,并且项目是在数据库中获取的。
这是后面的代码:
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);
}
}
}
以及 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);
}
}
}
所以 Page.IsPostBack
是我在页面中执行某些操作时应该学习的非常基本的内容。但这里的问题是,“footermenu”中的一个菜单项有一个异地链接,它应该将页面重定向到我的博客..但发生的情况是,一旦我,footermenu.SelectedValue
就为空了单击“博客”链接。
这是怎么回事?
更新
我已经更新了代码仍然卡住,SelectedValue 仍然为空
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_Init(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); } }
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_Init(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); } }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要了解页面生命周期。
对于动态控件(在代码中创建和添加),您需要在每次页面加载时重新创建它们 - 这最好在
init
事件处理程序中完成。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.