ASP.net ASCX 用户控制的基本帮助
尝试构建我自己的用户控件:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="TabMenu.ascx.cs" Inherits="TabMenu" %>
<asp:Panel runat="server" CssClass="tabWrapper">
<div class="tab tabSelected"><a href="artworkHome.aspx">Home</a></div>
<div class="tab"><a href="#">Create New</a></div>
<div class="tab"><a href="#">Help!</a></div>
<div class="clear"></div>
<asp:Literal runat="server" ID="lol"></asp:Literal>
</asp:Panel>
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class TabMenu : System.Web.UI.UserControl
{
public string TabGroup { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
if (this.TabGroup == "Artwork")
{
lol.Text = "LOL!";
}
}
}
这样使用时效果很好:
<CrystalControls:TabMenu runat="server" TabGroup="Artwork" />
除了“LOL!”没有显示。我可能错误地使用了属性等,我希望我清楚我想要做什么?
Trying to build my own user control:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="TabMenu.ascx.cs" Inherits="TabMenu" %>
<asp:Panel runat="server" CssClass="tabWrapper">
<div class="tab tabSelected"><a href="artworkHome.aspx">Home</a></div>
<div class="tab"><a href="#">Create New</a></div>
<div class="tab"><a href="#">Help!</a></div>
<div class="clear"></div>
<asp:Literal runat="server" ID="lol"></asp:Literal>
</asp:Panel>
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class TabMenu : System.Web.UI.UserControl
{
public string TabGroup { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
if (this.TabGroup == "Artwork")
{
lol.Text = "LOL!";
}
}
}
This shows fine when used as such:
<CrystalControls:TabMenu runat="server" TabGroup="Artwork" />
Except that "LOL!" isn't showing. I'm probably using properties incorrectly etc, I hope it's clear what I'm trying to do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的问题是 Page_Load 不是自动连接用户控件的事件,请参阅 autoeventwireup,就我个人而言,无论如何,我讨厌订阅我自己的事件的想法,您应该尽可能覆盖提供的虚拟方法。
尝试这个...
或者甚至这个,如果用户可能这样做控件的属性可以在页面生命周期中更改 TabGroup。
Your problem is that Page_Load is not an event that is automatically wired up for user controls, see autoeventwireup, personally i hate the idea of subscribing to my own events anyway, you should override the provided virtual methods where possible..
Try this...
or even this if its likely that the user of the control could change the TabGroup during the page lifecyle..
我尝试了你的例子,但一切正常,我确实明白了,哈哈!从字面上看。
如果你调试代码,并在检查 Tabgroup == "ArtWork" 时设置断点,你可以检查 TabGroup 的值,也许它还没有设置?
I tried your example, but its all working fine, i do get LOL! in the literal.
If you debug the code, and set a braekpoint on the check if Tabgroup == "ArtWork", you can check the value of TabGroup, maybe it is not set yet ?
您的控件编码正确并且工作正常。我怀疑你没有看到哈哈的原因!文本是由于 HTML 或 CSS 中的某些内容模糊了它。例如,您的某个选项卡上可能有
display:none
。尝试查看页面源代码,看看是否哈哈!里面有文字——我敢打赌是这样。Your control is coded correctly and works fine. I suspect the reason you are not seeing the LOL! text is due to something in your HTML or CSS obscuring it. For instance, you might have
display:none
on one of your tabs. Try looking at the page source and seeing if the LOL! text is in it - I bet it is.