views:

28

answers:

2

Hello All,

I am developing a publishing portal in SharePoint. The page layouts, master pages are designed using Visual Studio and I deploy the page layouts into the content database using wspbuilder.

I have a requirement wherein I have to access the controls of the page layout in code behind and assign or get values to/from the controls. But, the VS intellisense never shows the controls used in my page layout. What should I do in order to access the controls using code behind?

Is there any workaround for this?

Regards, Raghuraman.V

A: 

You have to make the web controls on the user control public.

Here's a quick example showing how to change a user control's textbox from the parent page:

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"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<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...";
        }
    }
}
mhughes
Well, actually protected will be enough...
Bernd
@Bernd, When I change the TextBox to protected instead of public, I get an error saying WebApplication1.WebUserControl1.UserControlTextBox1 is inaccessible due to its protection level.
mhughes
Sorry - I did not read your code properly. Of course you cannot access a protected property from another class - my mistake. What I meant was instead of declaring a public property you can access the control in the code-behind by declaring a protected control in WebUserControl1.ascx.cs such as: protected TextBox TextBox1;
Bernd
A: 

I guess that you have the page layouts and the code-behind in two different projects or at least in two different locations. You can also use 'real' code-behind pages in SharePoint that are side-by-side with the ASPX file so that you do not have to re-declare the controls at all.

To do so you can create the Visual Studio project for the WSP package as an “ASP.NET Web Application”, create ASPX pages with code-behind files side-by-side and use WSP Builder to remove the C# files from the package (the code is still compiled into the assembly and deploy with it). The trick works because WSP Builder can be configured with a local configuration file within the Visual Studio project to remove certain file types.

Here the local WSPBuilder.exe.config file:

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