Sharepoint .aspx page - asp.net

I have been told I will be let go at the end of the week if I can't figure this out by the end of the day. I need help in figuring out this issue. I am trying to create a .aspx page for a sharepoint site. This is the code I have...
<script runat="server">
Sub submit(Source As Object, e As EventArgs)
button1.Text="You clicked me!"
End Sub
</script>
<!DOCTYPE html>
<html>
<body>
<form runat="server">
<asp:Button id="button1" Text="Click me!" runat="server" OnClick="submit" />
</form>
</body>
</html>
I keep getting error messages every time I load the page. I ripped this code off the internet and when I load the page it says:
Server Error in '/' Application.
--------------------------------------------------------------------------------
Parser Error
Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.
Parser Error Message: Code blocks are not allowed in this file.
Source Error:
Line 3: button1.Text="You clicked me!"
Line 4: End Sub
Line 5: </script>
Line 6:
Line 7: <!DOCTYPE html>
Please help me. Why am I getting this message?

SharePoint doesn't allow inline code in .aspx files by default. You'll need to change that setting in the web.config if you want to do this, but it's not recommended. See the link #Mark posted in the comments.
As an alternative, you can create a webpart, and add that to the page. See this article for more help on how to do this.

You can allow adding code inside sharepoint pages by adding below lines in the web.config file
<PageParserPaths>
<PageParserPath VirtualPath="/pages/test.aspx" CompilationMode="Always" AllowServerSideScript="true" />
</PageParserPaths>
Where "/pages/test.aspx" is the path of your page , or you can add it as "/_catalogs/masterpage/*" for example to add all masterpages files.

Related

Link javascript file dynamically in asp.net

I want to link to a javascript file in header dynamically
<script src='<%=Global.RootPath%>js/jquery1.4.2.js' type="text/javascript"></script>
<script src='<%=Global.RootPath%>js/jquery.mousewheel-3.0.4.pack.js' type="text/javascript"></script>
but I got an error
The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.
Exception Details: System.Web.HttpException: The Controls collection
cannot be modified because the control contains code blocks (i.e. <%
... %>).
I want to add javascript file dynamically. Please explain why I am getting this error and is there any other method to do this?
Yes this is a common error when you try to render content like that on the header.
You can do alternative thinks like:
Place a literal control, and fully render your script line on code behind.
<asp:Literal runat="server" id="txtScripts" EnableViewState="false" />
and render the full line on code behind as
txtScripts.Text = string.Format(
"<script src='{0}js/jquery1.4.2.js' type=\"text/javascript\"></script>", Global.RootPath);
Or alternative you can create them totally dynamically and added to the header control list.
To include a javascript you can also include at the end of document itslef. If you want to specifically insert at the header then create a literal control and set its text to your script tags and add it to Page.Headers.

Invalid viewstate error when posting back to same page

I'm having some issues with an Invalid Viewstate error and I can understand why it's happening but I don't know how to fix it.
I have a page which is similar to this /story/?id=123 but I'm using a different page to Server.Transfer to this page.
So I've set up /info to Server.TransferRequest("/story/?id=123") and it works fine until the page does a postback to itself.
We have a login form on this page which simply reloads the page but when it does it seems to add /?id=123 onto the end of the URL so it ends up like this /info/?id=123 thus causing an Invalid Viewstate error.
I've already tried adding EnableViewStateMac="false" - this fixes the error but it doesn't log the user in as expected so it does not give the required result.
So my questions are:
Is there a better way to redirect to my page other than Server.TransferRequest but still keeping the nice URL? - I don't want to Response.Redirect if I can avoid it.
If not, is there an easy way to fix this error that doesn't require me adding EnableViewStateMac="false"?
http://support.microsoft.com/kb/316920
I believe that article will explain why you are having the problem and it gives a solution to fix it.
I know you don't want to use Response.Redirect, but I think that would also solve the problem.
PRB: "View State Is Invalid" Error Message When You Use Server.Transfer
This article was previously published under Q316920
Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer
offers support. Therefore, this article is offered "as is" and will no
longer be updated.
SYMPTOMS
When you use HttpServerUtility.Transfer("page name", true), you
receive the following error message:
The View State is invalid for this page and might be corrupted
CAUSE
This problem occurs because the EnableViewStateMac attribute of the
<pages> element is set to true by default. When this attribute is
set to true, ASP.NET runs a message authentication check (MAC) on the
view state of the page when the page is posted back from the client.
This check determines if the view state of the page was modified on
the client. For security purposes, it is recommended that you keep
this attribute set to true.
When you call the Server.Transfer method and set the second
parameter to true, you preserve the QueryString and the Form
collections. One of the form fields is the hidden __VIEWSTATE form
field, which holds the view state for the page. The view state message
authentication check fails because the message authentication check
only checks each page. Therefore, the view state from the page that
calls Server.Transfer is not valid on the destination page.
View state is page scoped and is valid for that page only. View state
should not be transferred across pages.
RESOLUTION
To resolve this problem, use one of the following methods.
Resolution 1
Transfer values between pages to pass your server control values to
the other pages. For more information, refer to the following MSDN
documentation: Passing Server Control Values Between
Pages
This requires that you create public properties for each property of a
control that you want to access from the destination page.
If you have many controls, and if you want to access the properties of
these controls from another page, you can also declare those controls
as public variables. For example:
Page1.aspx
Public Class Page1
Public WithEvents TextBox1 As System.Web.UI.WebControls.TextBox
'Insert your code here.
End Class
Page2.aspx
Dim sourcePage As Page1
sourcePage = CType(Context.Handler, WebForm1)
Response.Write(sourcePage.TextBox1.Text)
Resolution 2
Do not pass the second parameter (which is false by default) when
you call Server.Transfer. For example:
Server.Transfer("<page name>")
This code does not send the QueryString and the Form fields to the
page that is called. When no data is transferred, ASP.NET does not run
the message authentication check.
MORE INFORMATION
Steps to Reproduce the Behavior
Create an .aspx page named WebForm1.aspx that transfers execution to another page. Add the following code to WebForm1.aspx:
<%# Page language="vb" AutoEventWireup="true" %>
<html>
<body>
<form id="WebForm1" method="post" runat="server">
<asp:TextBox id="txtName" runat="server">Your Name</asp:TextBox><br>
<asp:Button id="Button1" runat="server" Text="Submit" OnClick="Button1_Click"></asp:Button>
</form>
</body>
</html>
<script runat=server>
Sub Button1_Click(sender As Object, e As System.EventArgs)
Server.Transfer("WebForm2.aspx",true)
End Sub
</script>
Create another .aspx page named WebForm2.aspx, and then add the following code:
<%# Page language="vb" AutoEventWireup="true" %>
<html>
<body>
<form id="WebForm2" method="post" runat="server">
<asp:Label id="lblName" runat="server" >Web Form 2</asp:Label>
</form>
</body>
</html>
<script runat=server>
Sub Page_Load(sender As Object, e As EventArgs)
Dim thisPage As System.Web.UI.Page
Dim nameTextBox As TextBox
thisPage = CType(Context.Handler, System.Web.UI.Page)
nameTextBox = CType(thisPage.FindControl("txtName"), System.Web.UI.Control)
lblName.Text = "Your name is '" & nameTextBox.Text & "'."
End Sub
</script>
Open WebForm1.aspx in your browser, and then click Submit.

DevExpress CSS "dx:"

I am a new to CSS world, so please help me out to figure it out.
I have tried to use a sample css from DevExpress ASP MVC demo and I have received a below error msg. I have two questions regarding this error msg.
What do I need add to resolve this error?
What deos "dx:" means?
By the way, I am getting "Unrecognized tag prefix or device filter 'dx' in visual studio 2010.
Parser Error
Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.
Parser Error Message: Unknown server tag 'dx:Head'.
Source Error:
Line 14: <title></title>
Line 15: <asp:ContentPlaceHolder id="CustomTopHeadHolder" runat="server" />
Line 16: <dx:Head ID="Head" runat="server" />
Line 17: <asp:ContentPlaceHolder id="CustomHeadHolder" runat="server" />
The "dx" is just a tag to tell it is a DevExpress Command. For the error message try registering the tags you are going to use.
For example if you are planning to use a ASPxNavBar then you will have to add this
<%# Register Assembly="DevExpress.Web.v10.2, Version=10.2.6.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
Namespace="DevExpress.Web.ASPxNavBar" TagPrefix="dx" %>
Note this is for the Version 10.2.6. You will have to add the appropriate one for your project
Hope this helps.
Looks like a namespace prefix. Have a look at this question for guidance.

asp:linkbutton not functioning in production

I've got an asp:linkbutton as follows:
<asp:LinkButton ID="lb_new" runat="server" ForeColor="White">New Item</asp:LinkButton>
Protected Sub lb_new_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lb_new.Click
ViewState("ItemID") = "0"
Dim myURL As String
myURL = String.Format("D002.aspx?e={0}&i={1}", ViewState("EventID"), ViewState("ItemID"))
Response.Redirect(myURL)
End Sub
Up until recently, it has functioned as it should. But for some reason, it has stopped working in production. As far as I can tell, it's not connecting to it's code-behind at all (I tried modifying it to simply change the text in one of the text boxes ont he page, and it fails that as well). Still works if I run the website through visual studio. But as soon I publish to our production server, it no longer works.
I'm stumped -- and still fiddling with it.
If anyone has experienced this, please share. Have been on this for a couple hours now, and am out of ideas.
Thank you!
UPDATE
The event handler has been suggested as missing by a couple of folks. This is actually handled in the code-behind by the 'Handles' clause (...Handles lb_new.Click).
Manually deleted the items in the production folder, then re-published. No joy.
Verified the files in the production folder are the new ones.
I created a brand new linkbutton -- it fails to connect to it's code-behind as well
I added an Onclick= to the mark-up. This shouldn't be necessary, considering the Handles clause in the code-behind. Regardless, the click still fails.
...still plugging away at it
UPDATE2
Removed the required field validators on the page, and it works. This does not make sense to me, because I had other controls on the page causing postbacks, and they still worked the whole time. Also, I had the fields that were being validated filled-in, so no reason (I can think of) that the validators would have been preventing the postback.
Now I just have to figure out how to do validation on the page without the required field validators.
...confused... :-)
Check the __EVENTTARGET and __EVENTARGUMENT variables on the post; these should match the values from the button that triggered the postback. That's at least the first clue...
Did you upgrade some third party DLL or upgrade from .NET 3.5 to .NET 4.0 or something like tha too?
HTH.
I was having this same problem and this thread pointed me to the answer (for me at least!). Just set the CausesValidation property of the linkbutton to false and the click event will fire ignoring the state of any validators on the page. I'm not doing anything in the click event or postback that requires any validation so it's fine for me to ignore it. If the same is true for you, this could well be your solution.
I think you need to define its "Click" event.
<asp:LinkButton ID="lb_new" runat="server" ForeColor="White" OnClick="lb_new_Click">New Item</asp:LinkButton>
(Edited)
For VB.NET: (Example From MSDN:http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.linkbutton.onclick(v=VS.90).aspx)
<%# Page Language="VB" AutoEventWireup="True" %>
<!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>
<title>LinkButton Example</title>
<script language="VB" runat="server">
Sub LinkButton_Click(sender As Object, e As EventArgs)
Label1.Text = "You clicked the link button"
End Sub
</script>
</head>
<body>
<form id="form1" runat="server">
<h3>LinkButton Example</h3>
<asp:LinkButton id="LinkButton1"
Text="Click Me"
Font-Names="Verdana"
Font-Size="14pt"
OnClick="LinkButton_Click"
runat="server"/>
<br />
<asp:Label id="Label1" runat="server" />
</form>
</body>
</html>
Validation controls were preventing a postback -- or at least, removing those controls seems to have solved the problem This does not make sense to me, because I had other controls on the page causing postbacks, and they still worked the whole time. Also, I had the fields that were being validated filled-in, so no reason (I can think of) that the validators would have been preventing the postback. Anyway, thanks to everyone for all the ideas.

Usercontrol access Javascript from a Content's Page's Master Page

Hello all I have a problem. I have a masterpage that all of my Content pages inherit from. Within this masterpage I have a script tag pointing to the Javascript file folder ~/Scripts/validation.js
On my content pages I use different usercontrols that require the use of many of the functions within the validation.js file however if I dont put the <script> tag and the Javascript functions within a contentholder on the contentpage the usercontrols do not see these functions and I get errors like OnNameValidation is not defined.
Of course I can copy the Javascript code into all of the pages but that's 30+ pages and a maintenance nightmare if I find a bug in one of the Javascript functions.
So the question (if you haven't already figured out from my long dissertation) is how can I declare the script tag with the path to the validation.js file so that contentpages and their usercontrols etc. can access the functions/code.
What you are trying to do should work, so I suspect that the path to your javascript file is wrong (without seeing your html code I can only assume). Keep in mind that you can only reference the javascript file like this: "~/Scripts/validation.js" if you have the link in a HEAD runat="server" tag. Without the runat="server" it won't find the file. You would have to do something like "../scripts/validation.js"
As a test I would try to call your javascript function in the masterpage, so you can rule out a bad file reference.
I picked up this tip from ScottGu here.
Add this to you user control which enables Intellisense in user controls but always evaluates false:
<% if (false) { %>
<script src="/Scripts/validation.js" type="text/javascript"></script>
<% } %>
I am currently doing this in my site by going to the Source code on the master page and putting the following in the head and outside of the ContentPlaceHolder.
<head>
<asp:ContentPlaceHolder ID="HeadContent" runat="server">
</asp:ContentPlaceHolder>
<script src="CP.js" type="text/javascript"></script>
</head>
The path you are assigning for your js file is probably not matching in all the pages.
script src="../JavaScript/Scriptaculous/scriptaculous.js"
It should have something like above this if you have separate folder for Scripts, MasterPages, Pages & Controls.

Resources