【问题标题】:Accessing controls for page layout in code behind在后面的代码中访问页面布局控件
【发布时间】:2011-04-17 19:26:54
【问题描述】:

我正在 SharePoint 中开发一个发布门户。页面布局、母版页是使用 Visual Studio 设计的,我使用 wspbuilder 将页面布局部署到内容数据库中。

我有一个要求,我必须在后面的代码中访问页面布局的控件,并为这些控件分配值或从中获取值。但是,VS intellisense 从不显示我的页面布局中使用的控件。我应该怎么做才能使用后面的代码访问控件?

有什么解决方法吗?

问候, 拉古拉曼.V

【问题讨论】:

    标签: sharepoint layout


    【解决方案1】:

    您必须将用户控件上的 Web 控件设为公开。

    下面是一个快速示例,展示了如何从父页面更改用户控件的文本框:

    WebUserControl1.ascx:

    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="WebApplication1.WebUserControl1" %>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    

    WebUserControl1.ascx.cs:

    using System;
    using System.Web.UI.WebControls;
    
    namespace WebApplication1
    {
        public partial class WebUserControl1 : System.Web.UI.UserControl
        {
            public TextBox UserControlTextBox1
            {
                get { return TextBox1; }
                set { TextBox1 = value; }
            }
    
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
        }
    }
    

    WebForm1.aspx:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
    <%@ Register src="WebUserControl1.ascx" tagname="WebUserControl1" tagprefix="uc1" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">       
            <uc1:WebUserControl1 ID="WebUserControl11" runat="server" />
        </div>
        </form>
    </body>
    </html>
    

    WebForm1.aspx.cs:

    using System;
    
    namespace WebApplication1
    {
        public partial class WebForm1 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                WebUserControl11.UserControlTextBox1.Text = "Your text here...";
            }
        }
    }
    

    【讨论】:

    • @Bernd,当我将 TextBox 更改为受保护而不是公共时,我收到一条错误消息,指出 WebApplication1.WebUserControl1.UserControlTextBox1 由于其保护级别而无法访问。
    • 抱歉 - 我没有正确阅读您的代码。当然,您不能从另一个类访问受保护的属性-我的错误。我的意思是,您可以通过在 WebUserControl1.ascx.cs 中声明受保护的控件来访问代码隐藏中的控件,而不是声明公共属性,例如:受保护的 TextBox TextBox1;
    【解决方案2】:

    我猜你的页面布局和代码隐藏在两个不同的项目中,或者至少在两个不同的位置。您还可以在 SharePoint 中使用与 ASPX 文件并排的“真实”代码隐藏页面,这样您根本不必重新声明控件。

    为此,您可以为 WSP 包创建 Visual Studio 项目作为 “ASP.NET Web 应用程序”,并排创建带有代码隐藏文件的 ASPX 页面并使用 WSP 生成器从包中删除 C# 文件(代码仍编译到程序集中并与它一起部署)。这个技巧很有效,因为 WSP Builder 可以 在 Visual Studio 项目中配置了本地配置文件以删除某些文件 类型。

    这里是本地 WSPBuilder.exe.config 文件:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
     <appSettings>
      <add key="Excludefiletypes" value="cs" />
     </appSettings>
    </configuration>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-05
      • 2012-04-11
      • 2019-02-24
      • 2012-03-18
      • 1970-01-01
      • 2021-09-30
      相关资源
      最近更新 更多