如何在服务器端集中和重定向 asp:Menu 的 WebForm MenuItem 链接?
How to centralize and redirect WebForm MenuItem links for an asp:Menu on the Server Side?
当我按转到 add_sellinvoice
时,它会将我重定向到另一个页面 add_purchase
发票,我希望它 运行 通过将我重定向到想要的页面来有效地
后面的代码是:
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Menu1_MenuItemClick(object sender, MenuEventArgs e)
{
if(e.Item.Value=="add_client")
{
Response.Redirect("Add_Client.aspx");
}
else if(e.Item.Value=="delete_client")
{
Response.Redirect("Delete_client.aspx");
}
else if (e.Item.Value == "update_client")
{
Response.Redirect("Update_client.aspx");
}
else if (e.Item.Value == "retrieve_client")
{
Response.Redirect("Retreive_clientbyname.aspx");
}
else if(e.Item.Value=="add_invoice")
{
Response.Redirect("add_purchinvoice.aspx");
}
else if (e.Item.Value == "delete_invoice")
{
Response.Redirect("delete_purchinvoice.aspx");
}
else if (e.Item.Value == "update_invoice")
{
Response.Redirect("update_purchinvoice.aspx");
}
else if (e.Item.Value == "retreive_invoice")
{
Response.Redirect("retireve_purchaseinvoice.aspx");
}
else if (e.Item.Value == "add_invoice")
{
Response.Redirect("Add_sellinvoice.aspx");
}
else if (e.Item.Value == "delete_invoice")
{
Response.Redirect("Delete_sellinvoice.aspx");
}
else if (e.Item.Value == "update_invoice")
{
Response.Redirect("Update_sellinvoice.aspx");
}
else if (e.Item.Value == "retrieve_invoice")
{
Response.Redirect("retrieve_sellinvoice.aspx");
}
else if (e.Item.Value == "add_item")
{
Response.Redirect("Add_items.aspx");
}
else if (e.Item.Value == "delete_item")
{
Response.Redirect("Delete_items.aspx");
}
else if (e.Item.Value == "update_item")
{
Response.Redirect("Update_items.aspx");
}
else if (e.Item.Value == "retrieve_item")
{
Response.Redirect("retrieve_items.aspx");
}
else if (e.Item.Value == "add_product")
{
Response.Redirect("Add_product.aspx");
}
else if (e.Item.Value == "delete_pruduct")
{
Response.Redirect("delete_product.aspx");
}
else if (e.Item.Value == "update_product")
{
Response.Redirect("Update_product.aspx");
}
else if (e.Item.Value == "retrieve_product")
{
Response.Redirect("retrieve_product.aspx");
}
else if (e.Item.Value == "add_amount")
{
Response.Redirect("Add_amount.aspx");
}
else if (e.Item.Value == "delete_amount")
{
Response.Redirect("Delete_amount.aspx");
}
else if (e.Item.Value == "update_amount")
{
Response.Redirect("Update_amount.aspx");
}
else if (e.Item.Value == "retrieve_amount")
{
Response.Redirect("retrieve_amount.aspx");
}
else if (e.Item.Value == "add_supplier")
{
Response.Redirect("Add_Supplier.aspx");
}
else if (e.Item.Value == "delete_supplier")
{
Response.Redirect("Delete_supplier.aspx");
}
else if (e.Item.Value == "update_supplier")
{
Response.Redirect("update_supplier.aspx");
}
else if (e.Item.Value == "retrieve_supplier")
{
Response.Redirect("retrieve_supplier.aspx");
}
else if (e.Item.Value == "add_prices")
{
Response.Redirect("add_costprice.aspx");
}
else if (e.Item.Value == "delete_prices")
{
Response.Redirect("delete_costprice.aspx");
}
else if (e.Item.Value == "update_prices")
{
Response.Redirect("Update_costprice.aspx");
}
}
protected void Home_LinkButton_Click(object sender, EventArgs e)
{
Response.Redirect("Home Page.aspx");
}
}
显示我的菜单的 .aspx Webform 代码是:
<table class="menutable">
<tr>
<td class="menutd">
<asp:Menu ID="Menu1" runat="server"
DynamicEnableDefaultPopOutImage="false"
StaticEnableDefaultPopOutImage="false"
Orientation="Horizontal"
StaticMenuItemStyle-CssClass="staticmenustyle"
DynamicMenuItemStyle-CssClass="dynamicmenustyle" OnMenuItemClick="Menu1_MenuItemClick">
<Items>
<asp:MenuItem Text="Client" Value="m1">
<asp:MenuItem Text="Add Client" Value="add_client"></asp:MenuItem>
<asp:MenuItem Text="Delete Client" Value="delete_client"></asp:MenuItem>
<asp:MenuItem Text="Update Client" Value="update_client"></asp:MenuItem>
<asp:MenuItem Text="Retrieve Client" Value="retrieve_client"></asp:MenuItem>
</asp:MenuItem>
<asp:MenuItem Text="Invoices" Value="m2">
<asp:MenuItem Text="sellinvoice" Value="x1">
<asp:MenuItem Text="Add Invoice" Value="add_invoice"></asp:MenuItem>
<asp:MenuItem Text="Delete Invoice" Value="delete_invoice"></asp:MenuItem>
<asp:MenuItem Text="Update Invoice" Value="update_invoice"></asp:MenuItem>
<asp:MenuItem Text="Retrieve Invoice" Value="retrieve_invoice"></asp:MenuItem>
</asp:MenuItem>
<asp:MenuItem Text="purchaseinvoice" Value="x2">
<asp:MenuItem Text="Add Invoice" Value="add_invoice"></asp:MenuItem>
<asp:MenuItem Text="Delete Invoice" Value="delete_invoice"></asp:MenuItem>
<asp:MenuItem Text="Update Invoice" Value="update_invoice"></asp:MenuItem>
<asp:MenuItem Text="Retrieve Invoice" Value="retrieve_invoice">
</asp:MenuItem>
</asp:MenuItem>
</asp:MenuItem>
<asp:MenuItem Text="Items" Value="m3">
<asp:MenuItem Text="Add Item" Value="add_item"></asp:MenuItem>
<asp:MenuItem Text="Delete Item" Value="delete_item"></asp:MenuItem>
<asp:MenuItem Text="Update Item" Value="update_item"></asp:MenuItem>
<asp:MenuItem Text="Retrieve Item" Value="retrieve_item"></asp:MenuItem>
</asp:MenuItem>
<asp:MenuItem Text="Product" Value="m4">
<asp:MenuItem Text="Add Product" Value="add_product"></asp:MenuItem>
<asp:MenuItem Text="Delete Product" Value="delete_product"></asp:MenuItem>
<asp:MenuItem Text="Update Product" Value="update_product"></asp:MenuItem>
<asp:MenuItem Text="Retrieve Product" Value="retrieve_product">
</asp:MenuItem>
</asp:MenuItem>
<asp:MenuItem Text="Amount" Value="m5">
<asp:MenuItem Text="Add Amount" Value="add_amount"></asp:MenuItem>
<asp:MenuItem Text="Delete Amount" Value="delete_amount"></asp:MenuItem>
<asp:MenuItem Text="Update Amount" Value="update_amount"></asp:MenuItem>
<asp:MenuItem Text="Retrieve Amount" Value="retrieve_amount">
</asp:MenuItem>
</asp:MenuItem>
<asp:MenuItem Text="Supplier" Value="m6">
<asp:MenuItem Text="Add Supplier" Value="add_supplier"></asp:MenuItem>
<asp:MenuItem Text="Delete Supplier" Value="delete_supplier"></asp:MenuItem>
<asp:MenuItem Text="Update Supplier" Value="update_supplier"></asp:MenuItem>
<asp:MenuItem Text="Retrieve Supplier" Value="retrieve_supplier">
</asp:MenuItem>
</asp:MenuItem>
<asp:MenuItem Text="Costs" Value="m7">
<asp:MenuItem Text="Add Prices" Value="add_prices"></asp:MenuItem>
<asp:MenuItem Text="Delete Prices" Value="delete_prices"></asp:MenuItem>
<asp:MenuItem Text="Update Prices" Value="update_prices"></asp:MenuItem>
<asp:MenuItem Text="Retrieve Prices" Value="retrieve_prices"></asp:MenuItem>
</asp:MenuItem>
</Items>
</asp:Menu>
</td>
</tr>
您看到此问题的原因是您有两个具有相同值的菜单项 add_invoice
,并且您没有检查父菜单项的上下文。 big if
语句将找到具有此值的第一个分支并忽略第二个分支。
<asp:MenuItem Text="sellinvoice" Value="x1">
<asp:MenuItem Text="Add Invoice" Value="add_invoice">
和
<asp:MenuItem Text="purchaseinvoice" Value="x2">
<asp:MenuItem Text="Add Invoice" Value="add_invoice">
因此您可以更改两个菜单项的值,使它们是唯一的(例如 sell_add_invoice
和 purchase_add_invoice
)。
但是,请问为什么要这样集中点击?它会导致额外的工作,即浏览器发送到服务器,服务器解码路由,然后发出 302
让浏览器导航到其他地方。带有 NavigateUrl
的 MenuItem 只是发出一个 <a href="...">
link,这将把用户直接带到预期的页面。
在一个简单的场景中,您可以直接 link 菜单项(即使它们是嵌套的),方法是将 NavigateUrl
属性 应用于每个 MenuItem
:
<asp:Menu ID="Menu1" runat="server"
DynamicEnableDefaultPopOutImage="false"
StaticEnableDefaultPopOutImage="false"
Orientation="Horizontal"
StaticMenuItemStyle-CssClass="staticmenustyle"
DynamicMenuItemStyle-CssClass="dynamicmenustyle"> <%--No MenuItemClick--%>
<Items>
<asp:MenuItem Text="Client" NavigateUrl="foo.aspx">
<asp:MenuItem Text="Add Client" NavigateUrl="bar.aspx">
<asp:MenuItem Text="Delete Client" NavigateUrl="baz.aspx"></asp:MenuItem>
</asp:MenuItem>
<asp:MenuItem Text="Delete Client" NavigateUrl="foo2.aspx"></asp:MenuItem>
</asp:MenuItem>
</Items>
</asp:Menu>
编辑
如果您绝对必须使用集中式服务器端 link 处理程序,我建议您更改后面的代码以使用字典来进行映射:
private static readonly IDictionary<string, string> MenuLinks =
new Dictionary<string, string>
{
{"add_client", "Add_Client.aspx"},
{"delete_client", "Delete_client.aspx"},
{"add_invoice", "add_purchinvoice.aspx"},
{"add_invoice", "Add_sellinvoice.aspx"}, // *** Error!
// ...
};
protected void Menu1_MenuItemClick(object sender, MenuEventArgs e)
{
string redirectLink;
if (MenuLinks.TryGetValue(e.Item.Value, out redirectLink))
{
// Avoid the thread abort exception of a response.redirect by making sure this is the last action in the page lifecycle
Response.Redirect(redirectLink, false);
}
else
{
txtErrorMessage.Text = "Please select a Link";
}
}
它不仅会为你省去很多乱七八糟的 if / switch 语句,而且当检测到重复键时你还会遇到一个硬错误:
An item with the same key has already been added.
当我按转到 add_sellinvoice
时,它会将我重定向到另一个页面 add_purchase
发票,我希望它 运行 通过将我重定向到想要的页面来有效地
后面的代码是:
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Menu1_MenuItemClick(object sender, MenuEventArgs e)
{
if(e.Item.Value=="add_client")
{
Response.Redirect("Add_Client.aspx");
}
else if(e.Item.Value=="delete_client")
{
Response.Redirect("Delete_client.aspx");
}
else if (e.Item.Value == "update_client")
{
Response.Redirect("Update_client.aspx");
}
else if (e.Item.Value == "retrieve_client")
{
Response.Redirect("Retreive_clientbyname.aspx");
}
else if(e.Item.Value=="add_invoice")
{
Response.Redirect("add_purchinvoice.aspx");
}
else if (e.Item.Value == "delete_invoice")
{
Response.Redirect("delete_purchinvoice.aspx");
}
else if (e.Item.Value == "update_invoice")
{
Response.Redirect("update_purchinvoice.aspx");
}
else if (e.Item.Value == "retreive_invoice")
{
Response.Redirect("retireve_purchaseinvoice.aspx");
}
else if (e.Item.Value == "add_invoice")
{
Response.Redirect("Add_sellinvoice.aspx");
}
else if (e.Item.Value == "delete_invoice")
{
Response.Redirect("Delete_sellinvoice.aspx");
}
else if (e.Item.Value == "update_invoice")
{
Response.Redirect("Update_sellinvoice.aspx");
}
else if (e.Item.Value == "retrieve_invoice")
{
Response.Redirect("retrieve_sellinvoice.aspx");
}
else if (e.Item.Value == "add_item")
{
Response.Redirect("Add_items.aspx");
}
else if (e.Item.Value == "delete_item")
{
Response.Redirect("Delete_items.aspx");
}
else if (e.Item.Value == "update_item")
{
Response.Redirect("Update_items.aspx");
}
else if (e.Item.Value == "retrieve_item")
{
Response.Redirect("retrieve_items.aspx");
}
else if (e.Item.Value == "add_product")
{
Response.Redirect("Add_product.aspx");
}
else if (e.Item.Value == "delete_pruduct")
{
Response.Redirect("delete_product.aspx");
}
else if (e.Item.Value == "update_product")
{
Response.Redirect("Update_product.aspx");
}
else if (e.Item.Value == "retrieve_product")
{
Response.Redirect("retrieve_product.aspx");
}
else if (e.Item.Value == "add_amount")
{
Response.Redirect("Add_amount.aspx");
}
else if (e.Item.Value == "delete_amount")
{
Response.Redirect("Delete_amount.aspx");
}
else if (e.Item.Value == "update_amount")
{
Response.Redirect("Update_amount.aspx");
}
else if (e.Item.Value == "retrieve_amount")
{
Response.Redirect("retrieve_amount.aspx");
}
else if (e.Item.Value == "add_supplier")
{
Response.Redirect("Add_Supplier.aspx");
}
else if (e.Item.Value == "delete_supplier")
{
Response.Redirect("Delete_supplier.aspx");
}
else if (e.Item.Value == "update_supplier")
{
Response.Redirect("update_supplier.aspx");
}
else if (e.Item.Value == "retrieve_supplier")
{
Response.Redirect("retrieve_supplier.aspx");
}
else if (e.Item.Value == "add_prices")
{
Response.Redirect("add_costprice.aspx");
}
else if (e.Item.Value == "delete_prices")
{
Response.Redirect("delete_costprice.aspx");
}
else if (e.Item.Value == "update_prices")
{
Response.Redirect("Update_costprice.aspx");
}
}
protected void Home_LinkButton_Click(object sender, EventArgs e)
{
Response.Redirect("Home Page.aspx");
}
}
显示我的菜单的 .aspx Webform 代码是:
<table class="menutable">
<tr>
<td class="menutd">
<asp:Menu ID="Menu1" runat="server"
DynamicEnableDefaultPopOutImage="false"
StaticEnableDefaultPopOutImage="false"
Orientation="Horizontal"
StaticMenuItemStyle-CssClass="staticmenustyle"
DynamicMenuItemStyle-CssClass="dynamicmenustyle" OnMenuItemClick="Menu1_MenuItemClick">
<Items>
<asp:MenuItem Text="Client" Value="m1">
<asp:MenuItem Text="Add Client" Value="add_client"></asp:MenuItem>
<asp:MenuItem Text="Delete Client" Value="delete_client"></asp:MenuItem>
<asp:MenuItem Text="Update Client" Value="update_client"></asp:MenuItem>
<asp:MenuItem Text="Retrieve Client" Value="retrieve_client"></asp:MenuItem>
</asp:MenuItem>
<asp:MenuItem Text="Invoices" Value="m2">
<asp:MenuItem Text="sellinvoice" Value="x1">
<asp:MenuItem Text="Add Invoice" Value="add_invoice"></asp:MenuItem>
<asp:MenuItem Text="Delete Invoice" Value="delete_invoice"></asp:MenuItem>
<asp:MenuItem Text="Update Invoice" Value="update_invoice"></asp:MenuItem>
<asp:MenuItem Text="Retrieve Invoice" Value="retrieve_invoice"></asp:MenuItem>
</asp:MenuItem>
<asp:MenuItem Text="purchaseinvoice" Value="x2">
<asp:MenuItem Text="Add Invoice" Value="add_invoice"></asp:MenuItem>
<asp:MenuItem Text="Delete Invoice" Value="delete_invoice"></asp:MenuItem>
<asp:MenuItem Text="Update Invoice" Value="update_invoice"></asp:MenuItem>
<asp:MenuItem Text="Retrieve Invoice" Value="retrieve_invoice">
</asp:MenuItem>
</asp:MenuItem>
</asp:MenuItem>
<asp:MenuItem Text="Items" Value="m3">
<asp:MenuItem Text="Add Item" Value="add_item"></asp:MenuItem>
<asp:MenuItem Text="Delete Item" Value="delete_item"></asp:MenuItem>
<asp:MenuItem Text="Update Item" Value="update_item"></asp:MenuItem>
<asp:MenuItem Text="Retrieve Item" Value="retrieve_item"></asp:MenuItem>
</asp:MenuItem>
<asp:MenuItem Text="Product" Value="m4">
<asp:MenuItem Text="Add Product" Value="add_product"></asp:MenuItem>
<asp:MenuItem Text="Delete Product" Value="delete_product"></asp:MenuItem>
<asp:MenuItem Text="Update Product" Value="update_product"></asp:MenuItem>
<asp:MenuItem Text="Retrieve Product" Value="retrieve_product">
</asp:MenuItem>
</asp:MenuItem>
<asp:MenuItem Text="Amount" Value="m5">
<asp:MenuItem Text="Add Amount" Value="add_amount"></asp:MenuItem>
<asp:MenuItem Text="Delete Amount" Value="delete_amount"></asp:MenuItem>
<asp:MenuItem Text="Update Amount" Value="update_amount"></asp:MenuItem>
<asp:MenuItem Text="Retrieve Amount" Value="retrieve_amount">
</asp:MenuItem>
</asp:MenuItem>
<asp:MenuItem Text="Supplier" Value="m6">
<asp:MenuItem Text="Add Supplier" Value="add_supplier"></asp:MenuItem>
<asp:MenuItem Text="Delete Supplier" Value="delete_supplier"></asp:MenuItem>
<asp:MenuItem Text="Update Supplier" Value="update_supplier"></asp:MenuItem>
<asp:MenuItem Text="Retrieve Supplier" Value="retrieve_supplier">
</asp:MenuItem>
</asp:MenuItem>
<asp:MenuItem Text="Costs" Value="m7">
<asp:MenuItem Text="Add Prices" Value="add_prices"></asp:MenuItem>
<asp:MenuItem Text="Delete Prices" Value="delete_prices"></asp:MenuItem>
<asp:MenuItem Text="Update Prices" Value="update_prices"></asp:MenuItem>
<asp:MenuItem Text="Retrieve Prices" Value="retrieve_prices"></asp:MenuItem>
</asp:MenuItem>
</Items>
</asp:Menu>
</td>
</tr>
您看到此问题的原因是您有两个具有相同值的菜单项 add_invoice
,并且您没有检查父菜单项的上下文。 big if
语句将找到具有此值的第一个分支并忽略第二个分支。
<asp:MenuItem Text="sellinvoice" Value="x1">
<asp:MenuItem Text="Add Invoice" Value="add_invoice">
和
<asp:MenuItem Text="purchaseinvoice" Value="x2">
<asp:MenuItem Text="Add Invoice" Value="add_invoice">
因此您可以更改两个菜单项的值,使它们是唯一的(例如 sell_add_invoice
和 purchase_add_invoice
)。
但是,请问为什么要这样集中点击?它会导致额外的工作,即浏览器发送到服务器,服务器解码路由,然后发出 302
让浏览器导航到其他地方。带有 NavigateUrl
的 MenuItem 只是发出一个 <a href="...">
link,这将把用户直接带到预期的页面。
在一个简单的场景中,您可以直接 link 菜单项(即使它们是嵌套的),方法是将 NavigateUrl
属性 应用于每个 MenuItem
:
<asp:Menu ID="Menu1" runat="server"
DynamicEnableDefaultPopOutImage="false"
StaticEnableDefaultPopOutImage="false"
Orientation="Horizontal"
StaticMenuItemStyle-CssClass="staticmenustyle"
DynamicMenuItemStyle-CssClass="dynamicmenustyle"> <%--No MenuItemClick--%>
<Items>
<asp:MenuItem Text="Client" NavigateUrl="foo.aspx">
<asp:MenuItem Text="Add Client" NavigateUrl="bar.aspx">
<asp:MenuItem Text="Delete Client" NavigateUrl="baz.aspx"></asp:MenuItem>
</asp:MenuItem>
<asp:MenuItem Text="Delete Client" NavigateUrl="foo2.aspx"></asp:MenuItem>
</asp:MenuItem>
</Items>
</asp:Menu>
编辑
如果您绝对必须使用集中式服务器端 link 处理程序,我建议您更改后面的代码以使用字典来进行映射:
private static readonly IDictionary<string, string> MenuLinks =
new Dictionary<string, string>
{
{"add_client", "Add_Client.aspx"},
{"delete_client", "Delete_client.aspx"},
{"add_invoice", "add_purchinvoice.aspx"},
{"add_invoice", "Add_sellinvoice.aspx"}, // *** Error!
// ...
};
protected void Menu1_MenuItemClick(object sender, MenuEventArgs e)
{
string redirectLink;
if (MenuLinks.TryGetValue(e.Item.Value, out redirectLink))
{
// Avoid the thread abort exception of a response.redirect by making sure this is the last action in the page lifecycle
Response.Redirect(redirectLink, false);
}
else
{
txtErrorMessage.Text = "Please select a Link";
}
}
它不仅会为你省去很多乱七八糟的 if / switch 语句,而且当检测到重复键时你还会遇到一个硬错误:
An item with the same key has already been added.