我正在開發SharePoint中的發佈門戶。頁面佈局,母版頁使用Visual Studio設計,我使用wspbuilder將頁面佈局部署到內容數據庫中。在後面的代碼中訪問頁面佈局的控件
我有一個要求,其中我必須訪問後面的代碼中的頁面佈局的控件,並分配或獲取控件的值。但是,VS智能感知從不顯示我的頁面佈局中使用的控件。我應該怎麼做才能使用後面的代碼訪問控件?
有沒有解決方法?
問候, Raghuraman.V
我正在開發SharePoint中的發佈門戶。頁面佈局,母版頁使用Visual Studio設計,我使用wspbuilder將頁面佈局部署到內容數據庫中。在後面的代碼中訪問頁面佈局的控件
我有一個要求,其中我必須訪問後面的代碼中的頁面佈局的控件,並分配或獲取控件的值。但是,VS智能感知從不顯示我的頁面佈局中使用的控件。我應該怎麼做才能使用後面的代碼訪問控件?
有沒有解決方法?
問候, Raghuraman.V
我猜你在頁面佈局和代碼隱藏在兩個不同的項目,或者至少在兩個不同的位置。您還可以在SharePoint中使用與ASPX文件並排的「真實」代碼隱藏頁面,這樣您就不必重新聲明控件了。
要做到這一點,你可以創建爲WSP封裝爲 「ASP.NET Web應用程序」 Visual Studio項目,創建代碼隱藏文件並排側,並使用WSP 拆除。ASPX頁面來自包的C#文件(代碼仍然編譯到程序集中並與其一起部署)。這個技巧是可行的,因爲WSP Builder可以使用Visual Studio項目中的本地配置文件配置 以刪除某些文件 類型。
這裏,本地WSPBuilder.exe.config文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="Excludefiletypes" value="cs" />
</appSettings>
</configuration>
你必須讓用戶控件公開在網絡控制。
這裏展示瞭如何從父頁面更改用戶控件的文本框一個簡單的例子:
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 2010-09-27 19:25:26
@Bernd,當我改變文本框來保護,而不是公衆,我得到一個錯誤說WebApplication1.WebUserControl1.UserControlTextBox1由於其保護級別而無法訪問。 – MattHughesATL 2010-09-28 17:27:11
對不起 - 我沒有正確閱讀你的代碼。當然,你不能從另一個班級訪問受保護的資產 - 我的錯誤。我的意思是通過在WebUserControl1.ascx.cs中聲明受保護的控件,而不是聲明一個公共屬性來訪問代碼隱藏控件,例如:protected TextBox TextBox1; – Bernd 2010-09-29 07:27:55