我试图在visual basic中运行非常简单的代码,但是我从其他页面获取了错误的标签文本。
这是页面。为清楚起见,我做了一个截图:
当我点击 sport 时,例如,一个userControl事件名称" LinkClicked2"应该由事件处理程序触发并处理,结果显示文本" Click Clickcted" 。
但我得到" MenuHost.aspx"
页面标签的文字EventForUserControl.aspx:
#!/usr/bin/env python
import os
import zipfile
def zipdir(path, ziph):
# ziph is zipfile handle
for root, dirs, files in os.walk(path):
for file in files:
ziph.write(os.path.join(root, file))
if __name__ == '__main__':
zipf = zipfile.ZipFile('file.zip', 'w', zipfile.ZIP_DEFLATED)
zipdir('tmp/', zipf)
zipf.close()
EventForUserControl.aspx文件背后的代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="EventForUserControl.aspx.cs" Inherits="Demo_UserControl_EventForUserControl2" %>
<%@ Register src="LinkMenu.ascx" tagname="LinkMenu" tagprefix="uc1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<uc1:LinkMenu ID="LinkMenu1" runat="server" OnLinkClicked2="LinkMenu1_OnLinkClicked2" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
</form>
现在它显示了MenuHost页面后面的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Demo_UserControl_EventForUserControl2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void LinkMenu1_OnLinkClicked2(object sender, EventArgs e)
{
this.Label1.Text = "Click Detected";
}
}
&#34; LinkMenu.ascx&#34; 页面:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Demo_UserControl_MenuHost : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Params["Product"] != null)
{
this.lblSelection.Text = "You Selected:";
this.lblSelection.Text += this.Request.Params["Product"];
}
}
}
&#34; LinkMenu.ascx&#34; 页面后面的代码:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="LinkMenu.ascx.cs" Inherits="Demo_UserControl_LinkMenu" %>
<div>
Products:<br />
<asp:HyperLink id="lnkBooks" runat="server"
NavigateUrl="MenuHost.aspx?product=Books"> Books
</asp:HyperLink> <br />
<asp:HyperLink id="lnkToys" runat="server"
NavigateUrl="MenuHost.aspx?product=Toys"> Toys
</asp:HyperLink> <br />
<asp:HyperLink id="lnkSports" runat="server"
NavigateUrl="MenuHost.aspx?product=Sports"> Sports
</asp:HyperLink> <br />
<asp:HyperLink id="lnkFurniture" runat="server"
NavigateUrl="MenuHost.aspx?product=Furniture"> Furniture
</asp:HyperLink>
</div>
在MenuHost的codeBehind中没有LinkMenu.ascx用户控件事件的事件处理程序。
我的问题是为什么我的UserControl事件由&#34; MenuHost&#34;处理页面而不是&#34; EventForUserControl&#34;页?