动态内容的最佳实践

本文关键字:最佳 动态 | 更新日期: 2023-09-27 18:14:59

我有相当广泛的经典ASP背景(使用服务器端javascript),我的公司终于(终于)推动在ASP中重新编码一切。Net(使用c#)。我很好地掌握了经典ASP的良好编程实践,并且我通常试图确保我以"正确"的方式编写代码。我一直在读ASP。Net教程和感觉我有一个相当的基础知识的理解。我有很好的纪律,将客户端javascript分离到外部js文件中,在外部css文件的标记之外保持样式,等等。因此,在阅读这些新手教程时,我理解了代码隐藏页面的概念。对我来说,将c#代码与最终将成为页面标记的内容分开是有意义的。让& lt;Asp:button>对象和修改它们的代码隐藏规则非常有意义。

然而,我很难想象如何做一些简单的事情,就像我在经典ASP中做的那样:

<%
   if (condition) {
      %>
         <input type="button" value="click me" onclick="dosomething()" />
      <%
   }
   else {
      %>
         <span>You don't have permission to see the button</span>
      <%
   }
%>

我有一个艰难的时间试图弄清楚我应该如何适应条件的东西,你看到上面的代码隐藏页面。如果我在这两种情况下都显示一个按钮,我将创建一个<asp:button>对象,并相应地在代码隐藏页面中对其进行样式设置——但在上面的示例中,我只在条件为真时显示按钮,在条件为假时显示一个span块。

我知道你不必把所有的c#代码放在代码隐藏页。我可以使用<% %>标签相同的方式,我将做在经典ASP。但是,如果我这样做,那么在我看来,它降低了代码隐藏页面的相关性。例如,我知道您可以使用外部css样式表对页面进行样式化,同时也可以在单个标签上使用内联样式。然而,我认为这是一种糟糕的做法。如果您不知道是在标记中查找还是在css文件中查找影响该元素的相关样式,那么以后就很难调整该元素的样式。

在我看来,这同样适用于您的标记和代码隐藏页面。把两者混在一起是一种必要的罪恶,还是有更好的方法来做我上面展示的?

动态内容的最佳实践

你可以在你的标记中:

<asp:Button .. Visible="False" />
<asp:Label .. Text="You do not have permissions" Visible="False" />

注意可见属性。ASP。. NET web表单是建立在对象模型的思想上的,所以按钮和标签是你可以使用的对象。在后面的代码中,可以有:

protected void Page_Load(object sender, EventArgs e) {
    .
    .
    if (Xcondition = true) {
       Button1.visible=  true;
       Label2.Visible = false;
    }
    else {
       Button1.visible=  false;
       Label2.Visible = true;
    }
}

是完成此操作的传统方法。你只需要弄清楚在生命周期中你需要这样做的地方,因为加载可能不是最好的(例如Init或PreRender事件)。如果您只需要在启动时执行此操作,请执行if (!Page.IsPostBack) { .. }以确保它只运行一次。

我做了一个小的例子,你基本上可以复制/粘贴和混乱一点。

这是aspx代码:

 < body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox runat="server" ID="txtCondition"></asp:TextBox>
        <asp:Button runat="server" Text="Check condition" ID="btnCheckCondition" OnClick="btnCheckCondition_Click" />
        <asp:Button runat="server" Text="Click me" ID="btnSubmit" OnClick="btnSubmit_Click" Visible="false"/>
        <asp:Label runat="server"  ID="lblMsg"></asp:Label>
    </div>
    </form>
</body>

这是背后的代码:(如果你双击btnCheckCondition, click_event方法将在你的代码背后自动生成。

  protected void btnCheckCondition_Click(object sender, EventArgs e)
        {
            if (txtCondition.Text == "Show the button")
            {
                btnSubmit.Visible = true;
                lblMsg.Text = "You are allowed to see the button.";
            }
            else
            {
                lblMsg.Text = "You are NOT allowed to see the button.";
            }
        }

这将检查文本框txtCondition中的输入。如果它等于"显示按钮",则第二个按钮将可见。如果文本是其他内容,按钮将不会出现,并且会出现一个标签,告诉您不允许看到该按钮。

在页面中有一个标签和一个按钮。从代码后面检查条件,并根据条件隐藏或显示控件。您可以使用控件的可见性属性来隐藏或显示它们。还要使用asp.net控件,以便您可以从后面的代码访问它们。

除非您有很好的理由不使用Visible属性。(在ASP。在asp.net Web Forms中,如果你动态地改变组件树的结构,而不是使用重复器控件,你就是在自找麻烦。)我所做的是:

Page.aspx

<button type='button' Visible='<%# condition %>' runat='server'>
    Click Me!
</button>
<span Visible='<%# !condition %>' runat='server'>
    No button for you!
</span>

Page.aspx.cs

protected void Page_Load() {
    if (!IsPostBack) DataBind(); // evaluates <%# ... %> expressions
}

(我的偏好是让控件从代码后面"拉"数据,而不是把它推到那里,并在可能的情况下使用匿名和普通的HTML控件,而不是它们的较重的对等物。)在页面呈现之前设置Visible的任何方法都可以工作。

首先以简单的方式帮助您,为了能够识别condition,您需要定义它并使其在代码后面公开。例如:

<%if (condition) {%>
    <input type="button" value="click me" onclick="dosomething()" />
<%}else { %>
    <span>You don't have permission to see the button</span>
<% } %>    

后面的代码
public partial class OnePage : System.Web.UI.Page
{
    public bool condition = false;
    protected void Page_Load(object sender, EventArgs e)
    {
        // here change the condition
    }
}

函数调用的第二种情况

<%if (fCheckAuth()) {%>
    <input type="button" value="click me" onclick="dosomething()" />
<%}else { %>
    <span>You don't have permission to see the button</span>
<% } %>    

后面的代码
public partial class OnePage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    public bool fCheckAuth()
    {
        // return your evaluate
        return false;
    }    
}

更多asp.net表单样式

现在,使用新的asp.net(与传统的asp相比),你也可以这样做(和类似)。

<asp:Panel runat="server" ID="pnlShowA">
    <input type="button" value="click me" onclick="dosomething()" />
</asp:Panel>
<asp:Panel runat="server" ID="pnlShowB">
    <span>You don't have permission to see the button</span>
</asp:Panel>

使用一个asp:Panel来扭曲你的全部内容,并在你打开和关闭它的代码。

public partial class OnePage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (condition)
        {
            pnlShowA.Visible = true;
            pnlShowB.Visible = false;
        }
        else
        {
            pnlShowA.Visible = false;
            pnlShowB.Visible = true;
        }    
    }
}

虽然问题不是关于使用ASP。. NET Web Forms与ASP。净MVC。我觉得有必要指出,使用ASP可能更有益。asp.net MVC而不是ASP。. NET Web Forms在您的场景中。

我这样说是因为经典ASP和ASP。. NET MVC内联样式是类似的。在大多数情况下,您可以将ASP (<% %>)转换为Razor(一种带有HTML的内联服务器端代码),只需对底层逻辑进行少量修改。我不知道你怎么想,但我写的代码越少越好。例如,上面的代码将转换为以下Razor语法:

   @if (condition) {
         <input type="button" value="click me" onclick="dosomething()" />
   }
   else {
         <span>You don't have permission to see the button</span>
   }

要回答你的问题,我将禁用服务器端的按钮。在客户端上禁用。如果按钮被禁用,则使用相应的文本启用Label。

//Code-behind
   bool correctPermission = true;
   submit.Enabled = correctPermission;
   noPermissionMessage.Enabled = !correctPermission;
//Client Code

<asp:Button ID="submit" runat="server" Text="Click Me" />
<asp:Label ID="noPermissionMessage" runat="server" Text="You don't have permission to see the button" Enabled="false" />