这个问题之所以存在,是因为它确实存在 具有历史意义,但事实并非如此 被认为是一个很好的主题问题 因为是本网站,所以请不要使用 作为证据,你可以问类似的问题 这里的问题。 更多信息:https://stackoverflow.com/faq


总有一些功能在边缘场景中很有用,但正是因为这个原因,大多数人都不知道它们。我要求的是课本上通常没有教过的特性。

你知道的是什么?


当前回答

默认情况下,自定义控件的标记之间的任何内容都被添加为子控件。这可以在AddParsedSubObject()覆盖中截获,用于过滤或额外的解析(例如,LiteralControls中的文本内容):

    protected override void AddParsedSubObject(object obj)
     { var literal = obj as LiteralControl;
       if (literal != null) Controls.Add(parseControl(literal.Text));
       else base.AddParsedSubObject(obj);
     }

...

   <uc:MyControl runat='server'>
     ...this text is parsed as a LiteralControl...
  </uc:MyControl>

其他回答

很多人提到了如何在重新编译时优化代码。最近我发现我可以在aspx页面中完成大部分的开发(后台代码),完全跳过构建步骤。只需保存文件并刷新页面。你所要做的就是把你的代码包装在下面的标签中:

<script runat="server">

   Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
     Response.Write("Look Ma', I didn't even had to build!")
   End Sub

</script>

一旦你完成了,只要移动到代码背后,构建,测试一切工作,瞧!

-D

WebMethods。

你可以使用ASP。NET AJAX回调到ASPX页面中的web方法。你可以用[WebMethod()]和[ScriptMethod()]属性来修饰一个静态方法。例如:

[System.Web.Services.WebMethod()] 
[System.Web.Script.Services.ScriptMethod()] 
public static List<string> GetFruitBeginingWith(string letter)
{
    List<string> products = new List<string>() 
    { 
        "Apple", "Banana", "Blackberry", "Blueberries", "Orange", "Mango", "Melon", "Peach"
    };

    return products.Where(p => p.StartsWith(letter)).ToList();
}

现在,在你的ASPX页面你可以这样做:

<form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />
        <input type="button" value="Get Fruit" onclick="GetFruit('B')" />
    </div>
</form>

并通过JavaScript调用您的服务器端方法使用:

    <script type="text/javascript">
    function GetFruit(l)
    {
        PageMethods.GetFruitBeginingWith(l, OnGetFruitComplete);
    }

    function OnGetFruitComplete(result)
    {
        alert("You got fruit: " + result);
    }
</script>

请求。IsLocal属性:

它指示当前请求是否来自本地计算机。

if( Request.IsLocal )
{
   LoadLocalAdminMailSettings();
}
else
{
   LoadServerAdminMailSettings();
}

ASP的一个鲜为人知和很少使用的特性。网络:

标签的映射

它很少被使用,因为只有在特定的情况下你才会需要它,但当你需要它的时候,它是如此方便。

一些关于这个小功能的文章:

ASP中的标签映射。网 在ASP中使用标签映射。NET 2.0

从上一篇文章来看:

Tag mapping allows you to swap compatible controls at compile time on every page in your web application. A useful example is if you have a stock ASP.NET control, such as a DropDownList, and you want to replace it with a customized control that is derived from DropDownList. This could be a control that has been customized to provide more optimized caching of lookup data. Instead of editing every web form and replacing the built in DropDownLists with your custom version, you can have ASP.NET in effect do it for you by modifying web.config:

<pages>
 <tagMapping>
   <clear />
   <add tagType="System.Web.UI.WebControls.DropDownList"
        mappedTagType="SmartDropDown"/>
  </tagMapping>
</pages>

web.config中appsettings元素的'file'属性。

指定包含自定义应用程序配置设置的外部文件的相对路径。

如果你的应用程序设置很少,需要在不同的环境(prod)上进行修改,这是一个很好的选择。

因为网络的任何变化。配置文件会导致应用程序重新启动,使用单独的文件允许用户修改appSettings部分中的值,而不会导致应用程序重新启动。单独文件的内容与Web中的appSettings部分合并。配置文件。