Answered by:
Get value from code behind

Question
-
Hi,
I have some custom code that gives me some information. When i click a link there is a new window opening (with javascript) and its calling test.aspx. In this page i want to show some information from my code file (this file is in my windows assymbly). i use the following code in my aspx file
<%@ Register Tagprefix="SPSWC" Namespace="Calls" Assembly="Calls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=c1cc654572c935da7" %>
<%@ Register Tagprefix="SPSWC" Namespace="Microsoft.SharePoint.Portal.WebControls" Assembly="Microsoft.SharePoint.Portal, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %><form runat="server">
<asp:Label id="lblError" style="Z-INDEX: 104; LEFT: 3px; POSITION: absolute; TOP: 6px" runat="server" Width="32px" Height="6px"></asp:Label>
</form>Everything is on the right spot :)
Now what i want is dat lblError is the value from my code file and what i'm trying is this:
Label lblError = New Label();
lblError.Text = "this is a test";
but that doesn't work. Someone?
Thanks!
Tuesday, April 13, 2010 10:02 AM
Answers
-
You can have a public property of the label, therefore, it's accessible to others.
--------------------------SAMPLE CODE--------------------------
public class LongDesc : LayoutsPageBase
{
public LongDesc()
{
}
protected Label lblError = null;
protected void Page_Load(object sender, EventArgs e)
{
this.lblError.Text= "hello word";
}
public Label MyLabel { get { return this.lblError; }}}
--------------------------SAMPLE CODE--------------------------
You can use Cross-Page Posting in ASP.NET Web Pages(http://msdn.microsoft.com/en-us/library/ms178139.aspx) to get a reference to the previous page and then get a reference to the label.
Cogito, ergo sum.- Marked as answer by Aaron Han - MSFT Monday, May 10, 2010 1:42 AM
Thursday, April 15, 2010 2:28 AM -
And how to set the value of Mylabel or lblError into something that's in a different class?
By passing arguments to this page is an option. Using Http Get:Request.QueryString [ "id" ]; // C#
Request.QueryString ( "id" ) ' VB
Cogito, ergo sum.- Marked as answer by Aaron Han - MSFT Monday, May 10, 2010 1:42 AM
Monday, April 19, 2010 3:36 AM
All replies
-
Hi there.
Although this is not really SharePoint related (more generic ASP.NET), you must have a class defined for your page and reference it from your aspx page.
This can be done in your assembly named "Calls", but I would recommend you to have a separate code behind file that is related to your page; test.aspx.cs. This could very well still be in the same name space. In your aspx page, add the Page element with the Inherits attribute.
Hope this helps.
Regards,
Magnus
My blog: InsomniacGeek.comTuesday, April 13, 2010 10:19 AM -
can you help me with an example?
Thx
Tuesday, April 13, 2010 11:50 AM -
can you help me with an example?
Sure,
start Visual Studio 2008. Select File->New Project->Visual C#->Web->ASP.NET Web Application.
In your Solution Explorer, you will find a bunch of files, one of them is called Default.aspx. If you expand the little plus sign, you will see a file called Default.aspx.cs. This is essentially the base that you can use for your example.
Note the link between the aspx page and the code-behind page in the Inherits attribute.
The technique used is ASP.NET and Web Forms, take a look at these guides from ASP.NET
Hope this helps.
Regards,
Magnus
My blog: InsomniacGeek.com- Edited by Magnus Johansson Tuesday, April 13, 2010 12:09 PM added link
Tuesday, April 13, 2010 12:06 PM -
Try this and let me know this works for you or not
<%@ Page Language="C#" MasterPageFile="../application.master" AutoEventWireup="true" %> <%@ Assembly Name="Microsoft.SharePoint.ApplicationPages,Version=12.0.0.0, Culture=neutral,PublicKeyToken=71e9bce111e9429c" %> <%@ Import Namespace="Microsoft.SharePoint.ApplicationPages" %> <%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Register Tagprefix="SPSWC" Namespace="Microsoft.SharePoint.Portal.WebControls" Assembly="Microsoft.SharePoint.Portal, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Import Namespace="Microsoft.SharePoint" %> <script runat="server"> protected void Page_Load(object sender, EventArgs e) { lblError.Text = "Test Message"; } </script> <asp:Content ID="Content2" ContentPlaceHolderId="PlaceHolderPageTitleInTitleArea" runat="server">My Page Name </asp:Content> <asp:Content ID="Content1" ContentPlaceHolderId="PlaceHolderMain" runat="server"> <asp:Label ID="lblError" Text="" style="Z-INDEX: 104; LEFT: 3px; position: static; TOP: 6px" runat="server" Width="32px" Height="6px"></asp:Label> </asp:Content>
Ashish Kanoongo, MCP, MCSD, MCTSTuesday, April 13, 2010 1:29 PM -
yes this works fine but i don't want the code in the same file as mijn asp. My code is in the assembly and it is my own custom code. Everything else works fine but i have a hyperlink to a new window and i want that in that new window my custom parameters comes.Wednesday, April 14, 2010 6:46 AM
-
hi here is your solution:
it's simple ASP.NET page, so you can have your page and code behind class in different assembly. follow these steps
1. create your page by inheriting Microsoft.SharePoint.WebControls.LayoutsPageBase class which is a base class for any SharePoint application page.
2. define your label in that class as below.
public class MyPage : LayoutsPageBase { protected Label lblError = null protected void Page_Load(object sender, EventArgs e) { this.lblErrorMsg.Text= "hello word"; } }
Now your aspx page.1. define page directive as following
<%@ Assembly Name="MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxxx" %> <%@ Page Language="C#" MasterPageFile="~/_layouts/application.master" Inherits="MyNamespcae.MyPage" EnableViewState="true" EnableViewStateMac="false"%>
I have assume that your assembly name is 'MyAssembly' and name space where your class is defined is 'MyNamespace'
above will solve your problem
Notes:
1., To access any control which is defined in an aspx file, create a global variable of same name and type in your class with 'public' or 'protracted' access. this will enable you to access that controls in class file.
2. for event handler for buttons or any other controls you can define name in your aspx file and create handler method in your class with same name, and it will work. if that didn't work then try to add handler in init event of page by overriding that event in your class.
hope this will help you to achieve whatever you want
Regards, Vikas Patel
Decos Software Development Pvt. Ltd. (An ISO 9001:2008 Company)
www.decos.in | www.decos.com- Proposed as answer by Jevgeni Borozna Wednesday, April 14, 2010 7:06 AM
Wednesday, April 14, 2010 7:04 AM -
Sign your assembly with a key.
1. Add a class to your project with name Test.aspx.cs. Your class must inherit from Microsoft.SharePoint.WebControls.LayoutsPageBase
2. Define a class-level variable:
Label lblError;
3. Override the Page_Load method and add your message to the label:
Public override void Page_Load(EventArgs e)
{
lblError.Text = "your message...";
}
4. Add the following directive to your aspx page:
<%@ Assembly Name="Assemblyname, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d216ed9999f673b4" %>
Pay attention that you fill out the correct assembly name and public keytoken.
5. Change the following directive in your aspx page:
<%@ Page Language="C#" MasterPageFile="../application.master" AutoEventWireup="true" Inherits="namespace.yourclassname" %>
Change the Inherits attribute to reflect the full name of your class in the code behind, this is the assembly.classname
Does this help?
Karine BoschWednesday, April 14, 2010 7:15 AM -
i still get an error. when i put this in my aspx file i get an error:
<%@ Page Language="C#" MasterPageFile="../application.master" AutoEventWireup="true" Inherits="namespace.yourclassname" %>
i sure that my namespace.yourclassname are correct.
but in my new class i get the following warnings
base type 'Microsoft.SharePoint.WebControls.UnsecuredLayoutsPageBase' is not CLS-compliant
base type 'Microsoft.SharePoint.WebControls.LayoutsPageBase' is not CLS-compliant
Wednesday, April 14, 2010 9:20 AM -
you can ignore this warnings as they will not give any problem at runtime., can you tell me what error did you get?
does your class is public and the members are protected as I have suggested?
Regards, Vikas Patel
Decos Software Development Pvt. Ltd. (An ISO 9001:2008 Company)
www.decos.in | www.decos.comWednesday, April 14, 2010 9:45 AM -
i created a new class (LongDesc) and here is de code.
public class LongDesc : LayoutsPageBase
{
public LongDesc()
{
}
protected Label lblError = null;
protected void Page_Load(object sender, EventArgs e)
{
this.lblError.Text= "hello word";
}
}
in my aspx file i did this
<%@ Assembly Name="Calls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=c1cc4125483935da7" %>
<%@ Page Language="C#" MasterPageFile="~/_layouts/application.master" Inherits="Calls.LongDesc" EnableViewState="true" EnableViewStateMac="false"%>this returns me a error page from sharepoint
Wednesday, April 14, 2010 9:50 AM -
add one more assembly entry as given below
<%@ Assembly Name="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
means the final page will look like this
<%@ Assembly Name="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Assembly Name="Calls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=c1cc4125483935da7" %> <%@ Page Language="C#" MasterPageFile="~/_layouts/application.master" Inherits="Calls.LongDesc" EnableViewState="true" EnableViewStateMac="false"%> <!-- for additional controls--> <%@ Register TagPrefix="wssuc" TagName="InputFormSection" Src="~/_controltemplates/InputFormSection.ascx" %> <%@ Register TagPrefix="wssuc" TagName="InputFormControl" Src="~/_controltemplates/InputFormControl.ascx" %> <%@ Register TagPrefix="wssuc" TagName="ButtonSection" Src="~/_controltemplates/ButtonSection.ascx" %> <%@ Register TagPrefix="wssawc" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Register TagPrefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <asp:Content ID="Main" contentplaceholderid="PlaceHolderMain" runat="server"> <asp:Label runat="server" ID="lblError" Visible="false" style="color:Red;"/> </asp:Content>
and use your class as it is and let me know the error from 12 hive, you can find default log files at '12\logs' folder
Regards, Vikas Patel
Decos Software Development Pvt. Ltd. (An ISO 9001:2008 Company)
www.decos.in | www.decos.comWednesday, April 14, 2010 11:30 AM -
i think this is the error (found it in the log)
Exception Type: System.Web.HttpException Exception Message: Content controls have to be top-level controls in a content page or a nested master page that references a master page.
after adding this
<%@ Assembly Name="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
i get this error
Exception Type: System.Web.HttpParseException Exception Message: Only Content controls are allowed directly in a content page that contains Content controls.
- Edited by idsis Wednesday, April 14, 2010 11:42 AM
Wednesday, April 14, 2010 11:35 AM -
its because you were not having '<asp:Content>' control in your aspx file, which is required as we are using master page in page directive.
use my page script as given above.. that will work.
Regards, Vikas Patel
Decos Software Development Pvt. Ltd. (An ISO 9001:2008 Company)
www.decos.in | www.decos.comWednesday, April 14, 2010 11:41 AM -
i have a <asp:Content> in my aspx file :sWednesday, April 14, 2010 11:50 AM
-
can you copy both aspx and cs files over here?
Regards, Vikas Patel
Decos Software Development Pvt. Ltd. (An ISO 9001:2008 Company)
www.decos.in | www.decos.comWednesday, April 14, 2010 11:55 AM -
Can you please add following tag under <SafeMode> of your webconfig and then reset IIS and test again your page,
<PageParserPath VirtualPath="/*" CompilationMode="Always" AllowServerSideScript="true" IncludeSubFolders="true" />
Ashish Kanoongo, MCP, MCSD, MCTSWednesday, April 14, 2010 11:56 AM -
i did an iisreset and now it's OK :s thx anyway
But now i need the value of a label from my Calls class in my LongDesc class. Someone?
Wednesday, April 14, 2010 11:57 AM -
You can have a public property of the label, therefore, it's accessible to others.
--------------------------SAMPLE CODE--------------------------
public class LongDesc : LayoutsPageBase
{
public LongDesc()
{
}
protected Label lblError = null;
protected void Page_Load(object sender, EventArgs e)
{
this.lblError.Text= "hello word";
}
public Label MyLabel { get { return this.lblError; }}}
--------------------------SAMPLE CODE--------------------------
You can use Cross-Page Posting in ASP.NET Web Pages(http://msdn.microsoft.com/en-us/library/ms178139.aspx) to get a reference to the previous page and then get a reference to the label.
Cogito, ergo sum.- Marked as answer by Aaron Han - MSFT Monday, May 10, 2010 1:42 AM
Thursday, April 15, 2010 2:28 AM -
And how to set the value of Mylabel or lblError into something that's in a different class?Friday, April 16, 2010 10:00 AM
-
And how to set the value of Mylabel or lblError into something that's in a different class?
By passing arguments to this page is an option. Using Http Get:Request.QueryString [ "id" ]; // C#
Request.QueryString ( "id" ) ' VB
Cogito, ergo sum.- Marked as answer by Aaron Han - MSFT Monday, May 10, 2010 1:42 AM
Monday, April 19, 2010 3:36 AM