asp.net 中可重用的 Page_PreRender 函数

发布于 2024-09-17 00:37:12 字数 997 浏览 3 评论 0 原文

我有一个函数将我的链接按钮设置为面板的默认按钮。

protected void Page_PreRender(object sender, EventArgs e)
    {
        string addClickFunctionScript = @"function addClickFunction(id) {
               var b = document.getElementById(id);
               if (b && typeof(b.click) == 'undefined')
                 b.click = function() {
                   var result = true;
                   if (b.onclick) result = b.onclick();
                   if (typeof(result) == 'undefined' || result)
                     eval(b.getAttribute('href'));
                 }
             };";

        string clickScript = String.Format("addClickFunction('{0}');", lbHello.ClientID);

        Page.ClientScript.RegisterStartupScript(this.GetType(), "addClickFunctionScript", addClickFunctionScript, true);
        Page.ClientScript.RegisterStartupScript(this.GetType(), "click_" + lbHello.ClientID, clickScript, true);
    }

这很好用。如何使其可重用于我的应用程序的所有页面。一个页面可以有多个链接按钮和多个面板......任何建议......

I have a function which sets my linkbutton as the default button for a panel.

protected void Page_PreRender(object sender, EventArgs e)
    {
        string addClickFunctionScript = @"function addClickFunction(id) {
               var b = document.getElementById(id);
               if (b && typeof(b.click) == 'undefined')
                 b.click = function() {
                   var result = true;
                   if (b.onclick) result = b.onclick();
                   if (typeof(result) == 'undefined' || result)
                     eval(b.getAttribute('href'));
                 }
             };";

        string clickScript = String.Format("addClickFunction('{0}');", lbHello.ClientID);

        Page.ClientScript.RegisterStartupScript(this.GetType(), "addClickFunctionScript", addClickFunctionScript, true);
        Page.ClientScript.RegisterStartupScript(this.GetType(), "click_" + lbHello.ClientID, clickScript, true);
    }

This works fine. How to make this reusable to all my pages of my application. One page can have multiple linkbuttons and multiple panels.... Any suggestion...

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

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

发布评论

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

评论(3

荆棘i 2024-09-24 00:37:19

我的建议是使用母版页,或者将代码分解为采用 System.Web.UI.Page 对象作为参数的静态函数。当然,您始终可以使用继承(这会起作用),但是您将失去使用拖放设计时功能来布局页面的能力,因为 VS.NET Web 表单设计器对 ASPX 页面做了很大的破坏,不要直接从 System.Web.UI.Page 或 System.Web.UI.MasterPage 派生。

My recommendation would be to either use a master page, or break the code out into a static function that takes a System.Web.UI.Page object as a parameter. Of course, you could always use inheritance (which will work), but you will lose the ability to layout your page using drag-and-drop design time functionality, since the VS.NET web form designer does a big freakout with ASPX pages that don't derive from System.Web.UI.Page or System.Web.UI.MasterPage directly.

风流物 2024-09-24 00:37:17

您可以创建一个派生自 System.Web.UI.Page 的类(我们称之为 Foo),并将您的方法抽象到可重用的程度。您的所有 ContentPages 都应该派生自 Foo 而不是 System.Web.UI.Page

You could create a class (let's call it Foo) that derives from System.Web.UI.Page and abstract your method to a point where it is reusable. All your ContentPages should derive from Foo instead of System.Web.UI.Page

玩套路吗 2024-09-24 00:37:16

最干净的方法是使用继承自 LinkBut​​ton 的自定义服务器控件。事实上,这似乎符合 来自您的 >之前的问题。您需要做的就是覆盖 OnPreRender 事件并粘贴您的代码,同时将 lbHello.ClientID 更改为 this.ClientID 以引用该控件的特定实例。设置时间不应超过 10 分钟。完成此操作后,您可以在一个页面上使用任意数量的控件,并在应用程序的各个页面中轻松支持它。

按照下面的说明(特别是“创建服务器控件”部分),您可能会发现这篇 MSDN 文章很有帮助: 演练:开发和使用自定义 Web 服务器控件。下面是完成此操作的分步指南:

  1. 在现有解决方案中添加一个新的 ASP.NET Server Control 项目(从解决方案资源管理器中右键单击您的解决方案 -> 添加新项目 -> ASP.NET Server Control)。将其命名为 LinkBut​​tonDefault(当然,您可以随意更改名称)。
  2. ServerControl1.cs 重命名为 LinkBut​​tonDefault.cs
  3. 将文件中的命名空间重命名为 CustomControls
  4. 通过打开 < code>AssemblyInfo.cs 文件(包含在项目的 Properties 文件夹中)。在文件底部添加此行: [ assembly: TagPrefix("CustomControls", "CC")]
  5. LinkBut​​tonDefault.cs 中添加此代码以覆盖 OnPreRender 事件:

代码(注意 this.ClientID 的使用):

    protected override void OnPreRender(EventArgs e)
    {
        string addClickFunctionScript = @"function addClickFunction(id) {
           var b = document.getElementById(id);
           if (b && typeof(b.click) == 'undefined')
             b.click = function() {
               var result = true;
               if (b.onclick) result = b.onclick();
               if (typeof(result) == 'undefined' || result)
                 eval(b.getAttribute('href'));
             }
         };";

        string clickScript = String.Format("addClickFunction('{0}');", this.ClientID);

        Page.ClientScript.RegisterStartupScript(this.GetType(), "addClickFunctionScript", addClickFunctionScript, true);
        Page.ClientScript.RegisterStartupScript(this.GetType(), "click_" + this.ClientID, clickScript, true);

        base.OnPreRender(e);
    }

您可能还想更新以 [ToolboxData(" <{0}: 使用 LinkBut​​tonDefault 而不是 ServerControl1 我强烈建议您阅读上述 MSDN 文章。利用其他功能,例如在需要时向工具箱添加控件

完成这些步骤后,您应该拥有一个类似于以下内容的 LinkBut​​tonDefault.cs 文件:

using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CustomControls
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:LinkButtonDefault runat=server></{0}:LinkButtonDefault>")]
    public class LinkButtonDefault : LinkButton
    {
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Text
        {
            get
            {
                String s = (String)ViewState["Text"];
                return ((s == null) ? "[" + this.ID + "]" : s);
            }

            set
            {
                ViewState["Text"] = value;
            }
        }

        protected override void RenderContents(HtmlTextWriter output)
        {
            output.Write(Text);
        }

        protected override void OnPreRender(EventArgs e)
        {
            string addClickFunctionScript = @"function addClickFunction(id) {
               var b = document.getElementById(id);
               if (b && typeof(b.click) == 'undefined')
                 b.click = function() {
                   var result = true;
                   if (b.onclick) result = b.onclick();
                   if (typeof(result) == 'undefined' || result)
                     eval(b.getAttribute('href'));
                 }
             };";

            string clickScript = String.Format("addClickFunction('{0}');", this.ClientID);

            Page.ClientScript.RegisterStartupScript(this.GetType(), "addClickFunctionScript", addClickFunctionScript, true);
            Page.ClientScript.RegisterStartupScript(this.GetType(), "click_" + this.ClientID, clickScript, true);

            base.OnPreRender(e);
        }
    }
}

现在返回到您的 Web 应用程序。并添加对 CustomControls 项目的引用,您应该能够从“添加引用”的 Project 选项卡执行此操作,因为我建议将上述项目添加到您现有的解决方案中。如果您希望在自己的解决方案中构建上述项目,那么您可以使用浏览选项卡添加对其.dll文件的引用。添加引用后,您就可以使用新的 LinkBut​​tonDefault 控件。

要使用这些控件,您可以在将使用该控件的每个页面上使用 @ Register 指令,或者您可以将其添加到 Web.config 并在整个应用程序中轻松引用它。下面我将向您展示这两种方法。根据您的问题,我认为您需要将其添加到 Web.config 中。请参阅 MSDN 文章,您将在页面中间的“标签前缀”部分下找到此信息。

使用@Register指令:

转到所需的.aspx页面并将Register指令添加到要使用该控件的每个页面的顶部in:

<%@ Register Assembly="CustomControls" Namespace="CustomControls" TagPrefix="CC" %>

在同一页面上,您现在可以使用该控件的多个实例。下面是一个示例:

<p><strong>1st Panel:</strong></p>
<asp:Label runat="server" ID="helloLabel" />
<asp:Panel ID="Panel1" runat="server" DefaultButton="lbHello">
    First name:
    <asp:TextBox runat="server" ID="txtFirstName" />
    <CC:LinkButtonDefault ID="lbHello" runat="server" Text="Click me" OnClick="lbHello_Click"
        OnClientClick="alert('Hello, World!');" />
</asp:Panel>

<p><strong>2nd Panel:</strong></p>
<asp:Label runat="server" ID="fooBarLabel" />
<asp:Panel ID="Panel2" runat="server" DefaultButton="lbFooBar">
    Other:
    <asp:TextBox runat="server" ID="TextBox1" />
    <CC:LinkButtonDefault ID="lbFooBar" runat="server" Text="Click me" OnClick="lbFooBar_Click" />
</asp:Panel>

在后面的代码 (.aspx.cs) 中,您需要添加:

protected void Page_Load(object sender, EventArgs e)
{
    // example of adding onClick programmatically
    lbFooBar.Attributes.Add("onClick", "alert('Foo Bar!');"); 
}

protected void lbHello_Click(object sender, EventArgs e)
{
    helloLabel.Text = String.Format("Hello, {0}", txtFirstName.Text);
}

protected void lbFooBar_Click(object sender, EventArgs e)
{
    fooBarLabel.Text = String.Format("FooBar: {0}", TextBox1.Text);
}

使用 Web.config:

要使用 Web.config,请保持完全相同的标记以及上面示例中使用的代码。请按照下列步骤操作:

  1. 删除 .aspx 标记上使用的 @ Register 指令。
  2. 打开 Web 应用程序的 Web.config 文件。
  3. 找到 ... 部分。
  4. 将以下映射添加到该部分:

映射:

<pages>
  <controls>
    <add assembly="CustomControls" namespace="CustomControls" tagPrefix="CC" />
  </controls>
</pages>

重新编译,一切都应该成功构建。完成此操作后,您不再需要在每个单独的页面上指定 @ Register 指令。

如果您遇到困难并有任何疑问,请告诉我。请仔细阅读上面的所有内容,因为这是一篇包含大量代码的长文章。

The cleanest way would be to use a custom server control that inherits from LinkButton. In fact this seems to be in line with the blog post from your earlier question. All you need to do is override the OnPreRender event and paste the code you have while changing lbHello.ClientID to this.ClientID to refer to the specific instance of that control. It should not take more than 10 minutes to set this up. Once this is done, you can use as many of the controls as you want on one page and easily support it throughout your application's various pages.

You might find this MSDN article helpful when following my instructions below, specifically the "Creating the Server Control" section: Walkthrough: Developing and Using a Custom Web Server Control. Here's a step by step guide to accomplishing this:

  1. In your existing solution add a new ASP.NET Server Control project (right click on your solution from the Solution Explorer -> Add New Project -> ASP.NET Server Control). Name it LinkButtonDefault (you're free to change the name, of course).
  2. Rename ServerControl1.cs to LinkButtonDefault.cs
  3. Rename the namespace in the file to CustomControls
  4. Perform steps 12-14 in the MSDN article by opening the AssemblyInfo.cs file (contained in the Properties folder of the project). Add this line at the bottom of the file: [assembly: TagPrefix("CustomControls", "CC")]
  5. In LinkButtonDefault.cs add this code to override the OnPreRender event:

Code (notice the use of this.ClientID):

    protected override void OnPreRender(EventArgs e)
    {
        string addClickFunctionScript = @"function addClickFunction(id) {
           var b = document.getElementById(id);
           if (b && typeof(b.click) == 'undefined')
             b.click = function() {
               var result = true;
               if (b.onclick) result = b.onclick();
               if (typeof(result) == 'undefined' || result)
                 eval(b.getAttribute('href'));
             }
         };";

        string clickScript = String.Format("addClickFunction('{0}');", this.ClientID);

        Page.ClientScript.RegisterStartupScript(this.GetType(), "addClickFunctionScript", addClickFunctionScript, true);
        Page.ClientScript.RegisterStartupScript(this.GetType(), "click_" + this.ClientID, clickScript, true);

        base.OnPreRender(e);
    }

You may also want to update the generated attribute code above the class declaration that starts with [ToolboxData("<{0}: to use LinkButtonDefault instead of ServerControl1. That's it for the new Server Control project. I highly recommend reading the aforementioned MSDN article to take advantage of other capabilities, such as adding controls to the toolbox if you have a need to do so.

After completing these steps you should have a LinkButtonDefault.cs file that resembles this:

using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CustomControls
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:LinkButtonDefault runat=server></{0}:LinkButtonDefault>")]
    public class LinkButtonDefault : LinkButton
    {
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Text
        {
            get
            {
                String s = (String)ViewState["Text"];
                return ((s == null) ? "[" + this.ID + "]" : s);
            }

            set
            {
                ViewState["Text"] = value;
            }
        }

        protected override void RenderContents(HtmlTextWriter output)
        {
            output.Write(Text);
        }

        protected override void OnPreRender(EventArgs e)
        {
            string addClickFunctionScript = @"function addClickFunction(id) {
               var b = document.getElementById(id);
               if (b && typeof(b.click) == 'undefined')
                 b.click = function() {
                   var result = true;
                   if (b.onclick) result = b.onclick();
                   if (typeof(result) == 'undefined' || result)
                     eval(b.getAttribute('href'));
                 }
             };";

            string clickScript = String.Format("addClickFunction('{0}');", this.ClientID);

            Page.ClientScript.RegisterStartupScript(this.GetType(), "addClickFunctionScript", addClickFunctionScript, true);
            Page.ClientScript.RegisterStartupScript(this.GetType(), "click_" + this.ClientID, clickScript, true);

            base.OnPreRender(e);
        }
    }
}

Now return to your web application and add a reference to the CustomControls project. You should be able to do this from the Add Reference's Project tab since I suggested adding the above project to your existing solution. If you want you could've built the above project in its own solution then you would add a reference to it's .dll file by using the Browse tab. Once a reference has been added you are ready to use the new LinkButtonDefault control.

To use the controls you can use the @ Register directive on each page the control will be used, or you can add it to the Web.config and gain easy reference to it throughout your application. I will show you both methods below. Based on your question I think you'll want to add it to the Web.config. Refer to the MSDN article and you will find this information half way down the page under "The Tag Prefix" section.

Using @ Register directive:

Go to your desired .aspx page and add the Register directive to the top of each page you want to use the control in:

<%@ Register Assembly="CustomControls" Namespace="CustomControls" TagPrefix="CC" %>

On the same page, you may now use multiple instances of the control. Here's an example:

<p><strong>1st Panel:</strong></p>
<asp:Label runat="server" ID="helloLabel" />
<asp:Panel ID="Panel1" runat="server" DefaultButton="lbHello">
    First name:
    <asp:TextBox runat="server" ID="txtFirstName" />
    <CC:LinkButtonDefault ID="lbHello" runat="server" Text="Click me" OnClick="lbHello_Click"
        OnClientClick="alert('Hello, World!');" />
</asp:Panel>

<p><strong>2nd Panel:</strong></p>
<asp:Label runat="server" ID="fooBarLabel" />
<asp:Panel ID="Panel2" runat="server" DefaultButton="lbFooBar">
    Other:
    <asp:TextBox runat="server" ID="TextBox1" />
    <CC:LinkButtonDefault ID="lbFooBar" runat="server" Text="Click me" OnClick="lbFooBar_Click" />
</asp:Panel>

In the code behind (.aspx.cs) you would need to add:

protected void Page_Load(object sender, EventArgs e)
{
    // example of adding onClick programmatically
    lbFooBar.Attributes.Add("onClick", "alert('Foo Bar!');"); 
}

protected void lbHello_Click(object sender, EventArgs e)
{
    helloLabel.Text = String.Format("Hello, {0}", txtFirstName.Text);
}

protected void lbFooBar_Click(object sender, EventArgs e)
{
    fooBarLabel.Text = String.Format("FooBar: {0}", TextBox1.Text);
}

Using Web.config:

To use the Web.config keep the exact same markup and code used in the above example. Follow these steps:

  1. Remove the @ Register directive used on the .aspx markup.
  2. Open the Web.config file for your web application.
  3. Locate the <system.web>...</system.web> section.
  4. Add the following mapping to that section:

Mapping:

<pages>
  <controls>
    <add assembly="CustomControls" namespace="CustomControls" tagPrefix="CC" />
  </controls>
</pages>

Recompile and everything should build successfully. With this in place you no longer need to specify the @ Register directive on each individual page.

If you get stuck and have any questions let me know. Just read over everything above carefully since it's a long post with lots of code.

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