Web 表单中的选项卡

发布于 2025-01-05 18:01:04 字数 734 浏览 3 评论 0原文

我试图在运行时创建一组选项卡,它们一度工作正常 - 但现在它们似乎只是显示先前呈现的选项卡下方的所有选项卡的内容。尽管看起来很愚蠢,但据我所知,自从代码运行以来,代码没有任何变化,所以我对发生的事情有点困惑......

为了这个例子的目的,我不会使用一个循环,尽管这似乎无关紧要,因为循环似乎不是问题。

<asp:Panel runat="server" ID="pnlTabs">
    <ul runat="server" id="ulSections" />
</asp:Panel>

我得出的结论是我的代码一定有问题,所以这里是 C#:

HtmlGenericControl liTab = new HtmlGenericControl("li");                        
HtmlGenericControl anTab = new HtmlGenericControl("a");
anTab.Attributes.Add("href", "#Tab1");
anTab.InnerText = Tab1;
liTab.Controls.Add(anTab);
ulSections.Controls.Add(liTab);

var pnl = new Panel();
pnl.ID = Tab1;
pnlTabs.Controls.Add(pnl);

我将控件和不在“pnl”上的内容放在一起。谁能告诉我我的错误是什么?

I'm trying to create a set of tabs at runtime and they were, for a time, working correctly- however now they just seem to display the contents of all tabs underneath the previously rendered tab. As silly as it seems, as far as I can tell, in terms of code nothing has changed since the code was working so I am a little confused as to what is going on...

For the purpose of this example I will not use a loop, although this seems irrespective as the loop doesn't appear to be the problem.

<asp:Panel runat="server" ID="pnlTabs">
    <ul runat="server" id="ulSections" />
</asp:Panel>

I've come to the conclusion that my code must be at fault so here is the C#:

HtmlGenericControl liTab = new HtmlGenericControl("li");                        
HtmlGenericControl anTab = new HtmlGenericControl("a");
anTab.Attributes.Add("href", "#Tab1");
anTab.InnerText = Tab1;
liTab.Controls.Add(anTab);
ulSections.Controls.Add(liTab);

var pnl = new Panel();
pnl.ID = Tab1;
pnlTabs.Controls.Add(pnl);

I place my controls and what not on the "pnl". Can anyone please tell me what my mistake is?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

清引 2025-01-12 18:01:04
//THIS ADDS THE <A> to the <LI>
liTab.Controls.Add(anTab);

//THIS ADDS THE <LI> to the <UL>
ulSections.Controls.Add(liTab);

//THIS CODE Appear to have nothing to do with anything.
var pnl = new Panel();
pnl.ID = Tab1;
pnlTabs.Controls.Add(pnl);

当您查看源代码时,您能否确认 HTML 是否在 html 中呈现。

这段代码在您的代码后面的什么位置...在按钮单击、页面加载、初始化时?

//THIS ADDS THE <A> to the <LI>
liTab.Controls.Add(anTab);

//THIS ADDS THE <LI> to the <UL>
ulSections.Controls.Add(liTab);

//THIS CODE Appear to have nothing to do with anything.
var pnl = new Panel();
pnl.ID = Tab1;
pnlTabs.Controls.Add(pnl);

Can you confirm if the HTML is even being rendered in the html when you View Source.

Where in your code behind is this code... in a button click, on Page Load, On Init ?

独享拥抱 2025-01-12 18:01:04

我发现问题是什么,基本上是“anTab”的 InnerText 不能与“anTab”的 href 属性相同,因为这会导致混乱并导致所有选项卡的内容仅出现在第一个(选定的)中选项卡,而其余选项卡不起作用。

例如,以下代码可以正常工作,因为“anTab”的 InnerText 是“Tab 1”,并且 href 正在查找 ID 为“Tab1”(面板的 ID)的控件。

HtmlGenericControl liTab = new HtmlGenericControl("li");                        
HtmlGenericControl anTab = new HtmlGenericControl("a");
anTab.Attributes.Add("href", "#Tab1");
anTab.InnerText = "Tab 1";
liTab.Controls.Add(anTab);
ulSections.Controls.Add(liTab);

var pnl = new Panel();
pnl.ID = "Tab1";
pnlTabs.Controls.Add(pnl);

下面我们可以看到“anTab”的 InnerText 与我们希望用作选项卡的面板的 href 和 ID 相同,这会导致混乱,因为 href 现在假设您希望“选项卡单击”指向它本身而不是实际的面板。

HtmlGenericControl liTab = new HtmlGenericControl("li");                        
HtmlGenericControl anTab = new HtmlGenericControl("a");
anTab.Attributes.Add("href", "#Tab1");
anTab.InnerText = "Tab1";
liTab.Controls.Add(anTab);
ulSections.Controls.Add(liTab);

var pnl = new Panel();
pnl.ID = "Tab1";
pnlTabs.Controls.Add(pnl);

I discovered what the problem was, it was basically that the InnerText of "anTab" cannot be the same as the href attribute of "anTab" as this causes confusion and results in the content from all tabs to appear only in the first (selected) tab, while the remaining tabs don't function.

For example, the following code works correctly as the InnerText of "anTab" is "Tab 1" and the href is looking for a control with the ID "Tab1", which is the ID of the panel.

HtmlGenericControl liTab = new HtmlGenericControl("li");                        
HtmlGenericControl anTab = new HtmlGenericControl("a");
anTab.Attributes.Add("href", "#Tab1");
anTab.InnerText = "Tab 1";
liTab.Controls.Add(anTab);
ulSections.Controls.Add(liTab);

var pnl = new Panel();
pnl.ID = "Tab1";
pnlTabs.Controls.Add(pnl);

Below we can see that the InnerText of "anTab" is the same as the href and the ID of the panel we wish to use as the tab, this causes the confusion as the href now assumes you want the "tab click" to point to itself instead of the actual panel.

HtmlGenericControl liTab = new HtmlGenericControl("li");                        
HtmlGenericControl anTab = new HtmlGenericControl("a");
anTab.Attributes.Add("href", "#Tab1");
anTab.InnerText = "Tab1";
liTab.Controls.Add(anTab);
ulSections.Controls.Add(liTab);

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