简体   繁体   English

如何显示和隐藏html字段集以及如何从Asp.Net代码后面设置图例文本

[英]How to show and hide html fieldset And also Set legend text from Asp.Net Code behind

I have a filedset and legend inside that with "some text" and inside this fieldset i have a GRID 我有一个文件集和图例,里面有“一些文本”,在这个字段集中,我有一个GRID

I have 2 questions 我有两个问题

  1. How to show ? 如何显示? hide html filedset from code behind, i tried the following way to show and hide the field set 从后面的代码隐藏html filedset,我尝试了以下方法来显示和隐藏字段集

    a) set runat="server"--but it didnt work b) i pasted the fieldset inside an asp.net panel and tried to show/hide the panel, it also didnt work a)设置runat =“ server”-但没有用b)我将字段集粘贴到了asp.net面板中,并试图显示/隐藏面板,但它也没有用

  2. How to set text for legend from code behind , ie I want to set "some text" + Value_Form_Code Behind AS leged text 如何从后面的代码中为图例设置文本,即我想在“ AS文本”后面设置“某些文本” + Value_Form_Code

Note :I am using "Rad Ajax Manager" and Rad Ajax LoadingPanel 注意:我正在使用“ Rad Ajax Manager”和Rad Ajax LoadingPanel

<asp:Panel ID="Panel1" runat="server" >                
<fieldlset>
<legend><asp:Label id="Label1" runat="server" /></legend>
</fieldset>
</asp:Panel>

How to show /hide html filedset from code behind ? 如何从后面的代码显示/ hide html filedset?

Panel1.Visible = true; // or false

How to set text for legend from code behind , ie I want to set "some text" + Value_Form_Code Behind AS legend text ? 如何从后面的代码中为图例设置文本,即我想在AS图例文本后设置“某些文本” + Value_Form_Code?

Label1.Text = String.Format("some text {0}",Value_Form_Code);

it should also be possible to add an ID and runat="server" to your fieldset and control visibility through code-behind. 还应该可以向您的字段集添加IDrunat="server" ,并通过代码隐藏控制可见性。 Just remember to write 'ID' in upper letters. 只要记住要用大写字母写“ ID”即可。

<fieldset ID="myFieldset" runat="server">

You won't be able to control the legend text, unless you give it an ID and runat itself. 您将无法控制图例文本,除非您为其指定ID并自行运行。 But visibility is absolutely possible. 但是可见性绝对是可能的。

The upside of this approach is: no needless html markup (Panel would be extra div). 这种方法的好处是:没有不必要的html标记(面板将是额外的div)。 The downside: fieldsets are not really asp-controls, so some things might give you exceptions, so use carefully. 缺点:字段集并不是真正的asp控件,因此某些事情可能会给您带来异常,因此请谨慎使用。

I use this approach only when I want to prevent controls from rendering at all in certain cases (visibility does that). 仅在某些情况下我想完全禁止呈现控件时才使用这种方法(可见性就是这样做)。

1: I think you should put your fieldset inside an asp:panel and then hide/show the panel from your code-behind. 1:我认为您应该将字段集放在asp:panel内,然后从代码隐藏/隐藏面板。 This will automatically hide/show your fieldset. 这将自动隐藏/显示您的字段集。

2: As far as setting the legend text is concerned, just set the legend with runat="server" and set the code from codebehind. 2:就设置图例文本而言,只需使用runat =“ server”设置图例并从代码隐藏中设置代码。

When you set the 'GroupingText' property of the asp:panel control then It will render as the 'fieldset' tag in HTML and whatever set in the 'GroupingText' property value is rendered as the <legend> tag. 当您设置asp:panel控件的'GroupingText'属性时,它将在HTML中呈现为'fieldset'标签,而在'GroupingText'属性值中设置的任何内容都将呈现为<legend>标签。

I think following code will help you as per your requirement. 我认为以下代码将根据您的要求为您提供帮助。

For Design side, 对于设计方面,

<asp:Panel runat="server" ID="Panel1" GroupingText="This is legend">
       <h4>Your Content Goes Here</h4>
    </asp:Panel>
    <br />
    <asp:Button ID="btnHidePanel" runat="server" Text ="Hide FieldSet" onclick="btnHidePanel_Click" />
    <asp:Button ID="btnShowPanel" runat="server" Text ="Show FieldSet" onclick="btnShowPanel_Click" Visible="false" />

For Code-behind try this, 对于隐藏的代码,请尝试此操作,

protected void btnHidePanel_Click(object sender, EventArgs e)
        {
            Panel1.Visible = false;
            btnHidePanel.Visible = false;
            btnShowPanel.Visible = true;
        }

        protected void btnShowPanel_Click(object sender, EventArgs e)
        {
            Panel1.Visible = true;
            Panel1.GroupingText = "This Legend Text Has been Changed";
            btnHidePanel.Visible = true;
            btnShowPanel.Visible = false;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM