ASP.net ASCX 用户控制的基本帮助

发布于 2024-10-14 15:08:10 字数 1153 浏览 0 评论 0原文

尝试构建我自己的用户控件:

<%@ 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爱*していゐ 2024-10-21 15:08:11

您的问题是 Page_Load 不是自动连接用户控件的事件,请参阅 autoeventwireup,就我个人而言,无论如何,我讨厌订阅我自己的事件的想法,您应该尽可能覆盖提供的虚拟方法。

尝试这个...

protected override void OnLoad(EventArgs e)
{    
    if (this.TabGroup == "Artwork")          
    {              
         lol.Text = "LOL!";          
    } 
    base.OnLoad(e);
}

或者甚至这个,如果用户可能这样做控件的属性可以在页面生命周期中更改 TabGroup。

protected override void OnPreRender(EventArgs e)
{    
    if (this.TabGroup == "Artwork")          
    {              
         lol.Text = "LOL!";          
    } 
    base.OnPreRender(e);
}

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...

protected override void OnLoad(EventArgs e)
{    
    if (this.TabGroup == "Artwork")          
    {              
         lol.Text = "LOL!";          
    } 
    base.OnLoad(e);
}

or even this if its likely that the user of the control could change the TabGroup during the page lifecyle..

protected override void OnPreRender(EventArgs e)
{    
    if (this.TabGroup == "Artwork")          
    {              
         lol.Text = "LOL!";          
    } 
    base.OnPreRender(e);
}
很酷不放纵 2024-10-21 15:08:10

我尝试了你的例子,但一切正常,我确实明白了,哈哈!从字面上看。
如果你调试代码,并在检查 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 ?

胡渣熟男 2024-10-21 15:08:10

您的控件编码正确并且工作正常。我怀疑你没有看到哈哈的原因!文本是由于 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文