Custom control does not exist exception in SharePoint - asp.net

I'm trying to add a custom web UserControl to my SharePoint application. I have added my UserControl to a SharePoint WebPart and now i'm trying to load that WebPart in my page.
But when i add the WebPart i get the following exception:
The file /usercontrols/Test3Report.ascx does not exist.
But i'm 100% sure that both the file and folder exists. The path to the usercontrol also seems fine to me. This is how i try to load my control:
protected string UserControlPath = #"~/usercontrols/";
protected string UserControlName = #"Test3Report.ascx";
private Test3Report mycontrol;
protected override void CreateChildControls()
{
try
{
mycontrol = (Test3Report)this.Page.LoadControl(UserControlPath + UserControlName);
Controls.Add(mycontrol);
}
catch (Exception CreateChildControls_Exception)
{
exceptions += "CreateChildControls_Exception: " + CreateChildControls_Exception.Message;
}
finally
{
base.CreateChildControls();
}
}
This seems right to me. I have no idea why it is complaining that i cannot find the Test3Report.ascx.
I've read something about the TrustLevel not set to full or something. Could that be it? Or any other ideas?

Try adding your new custom control by right-clicking on your solution in solution explorer in VS2010 -> add -> new item , after choosing sharepoint2010 from the list on the right, choose "User control".
This way your control will be automatically created and later deployed in the mapped "~/_ControlTemplates/PROJECTNAME/UserControlNAME.ascx" folder which can be referenced to later.
Also make sure that the user control is included in the package by opening the ".package" file in "Package" folder under solution explorer and checking that the user control is listed under "Items in Package".
Tip: If possible for your use-case, instead of adding the custom control programmatically in CreateChildControls() you can put your destination page in design mode and then add your custom control by dragging and dropping the .ascx file from the solution explorer into you page.

Related

Adding User Control instead of EditForm.aspx in designer

Hello I am trying to add My user control instead of EditForm.aspx(using sharepoint 2007) I have added a new aspx page and registered my dll. But now I dont know how to proceed.
Also In my project I have one cs file which has following code.
public class SkillEditReg : WebPart
{
private Control _MyUserControl;
protected override void CreateChildControls()
{
base.CreateChildControls();
_MyUserControl =
this.Page.LoadControl(#"~/_controltemplates/MySkill/EditSkill.ascx");
this.Controls.Add(_MyUserControl);
}
An I tried to register it as feature I refered below link
http://fusionovation.com/post/2008/09/18/how-to-add-a-custom-user-control-to-a-sharepoint-page.aspx
I had put guid id in feature id.. and publictoken of assembly in element id.
And in contolId in designer I had put The usercontrol name..But nothing works..
Please Help
It was so easy..All i did is hide the webpart of editform.aspx and went to the same page through browser it was emty then i edited the url and after EditForm.aspx? I added toolpaneview=2. It opened in edit mode then i added my dll :)

Silverlight page navigation

I'm using silverlight content within a aspx page.i have created silverlight page is in a separate silverlight project and i have added that project to my normal asp.net application ClientBin.i need to redirect to a aspx page on my asp.net project from a silverlight page button click.how can i achive this?
I think you have one of two options. In your view model for that silverlight control, during the initialization, Bind the navigate URI for a hyperlink button to the desired URI you want to navigate to. Option 2 (a lot smoother): On the click method, Invoke a javascript method on the page that hosts the silverlight object. That method would then do some sort of smooth jquery transition or just a simple navigation for you.
Option 1: <HyperlinkButton NavigateUri="{Binding DesiredURL}" TargetName="_blank" />
For option 2, remember to include:
using System.Windows.Browser;
Option 2:
public void OnFancyNavigate(string _destination)
{
//call the browser method/jquery method (I used constants to centralize the names of the respective browser methods
try
{
HtmlWindow window = HtmlPage.Window;
window.Invoke(Constants.TBrowserMethods.BM_FANCYNAVIGATE, new object[] { _destination});
}
catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.ToString()); }
}
Lastly, define the javascript method in the aspx/html/.js file that hosts the xap content:
function fancyNavigate(_destination) {
//some fancy jquery or just the traditional document.location change here
}
C# will locate the javascript method when invoked from your code, and you should be good to go

Invalid viewstate information may be corrupted

I keep getting the following error message when I run my vb.net web app:
The state information is invalid for this page and might be corrupted.
View full message here.
After a good search, I came across this Microsoft page, which describes the problem exactly. The likely cause seems to be "scenario 2":
Scenario 2: You modify your pages, which causes the shadow, copied files in the
Temporary ASP.NET files folder to be regenerated. A user has a copy of
the page that was requested before this change, and the user posts the
page after the files in that folder were regenerated.
But strangely- despite saying there is a hotfix, does not actually give the link to it.
Can anyone suggest a fix?
UPDATED: I appear to have prevented this error from happening by using EnableEventValidation="False" in the Page node of the markup as recommended here. More of a work-around than a fix.
It is not recommended to disable EnableEventValidation as explained in Page.EnableEventValidation Property.
I got this issue before and came over it by deleting all files within ASP.NET Temporary Folder.
Folder Path:
.NET 2 : C:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files
.NET 4: C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files
IN one of my project, I was getting this error in Mozilla Firefox only that whenever i clicked a button or link.
It is because of the fact that firefox caches the form fields. Two ways to encounter this problem.
Write following snipet in your cs file
protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
if (Request.Browser.MSDomVersion.Major == 0) // If it is Non IE Browser
{
Response.Cache.SetNoStore();
}
}
In page load, write following statemet
Response.Cache.SetNoStore();
Now error has been removed and you can sleep with satisfaction.
http://yourtahir.wordpress.com/2008/06/26/the-state-information-is-invalid-for-this-page-and-might-be-corrupted/
Might be overdate, but this cured my viewstate problems with ajax calls to page control.
Custom CompressedViewState :
add this code :
private ObjectStateFormatter _formatter = new ObjectStateFormatter();
protected override void
SavePageStateToPersistenceMedium(object viewState)
{
MemoryStream ms = new MemoryStream();
_formatter.Serialize(ms, viewState);
byte[] viewStateArray = ms.ToArray();
ClientScript.RegisterHiddenField("__COMPRESSEDVIEWSTATE",
Convert.ToBase64String(
CompressViewState.Compress(viewStateArray)));
}
protected override object LoadPageStateFromPersistenceMedium()
{
string vsString = Request.Form["__COMPRESSEDVIEWSTATE"];
byte[] bytes = Convert.FromBase64String(vsString);
bytes = CompressViewState.Decompress(bytes);
return _formatter.Deserialize(
Convert.ToBase64String(bytes));
}

How to create ASPX Page Dynamically

i have two controls on asp.net page , textbox and button and i want to write new page into textbox then when i hit the button i want to create new asp.net web page with textbox text. what do you want me to prefer to do this? is there any step by step tutorial or any code you have done before? thanks for asnwers.
ok more detail about what i want.
textbox control text is "contact" and i click my button control. button take textbox text "contact" and create new web form page which name is "contact.aspx and its code page contact.cs" . thats it. just create new web form page under root directory with button click.
maybe there is a single code line like
Page pg =new Page();
pg.create();
You can use File.WriteAllText to write the contents of the textbox to any location that the application pool user has permissions to write to.
From your edit it appears that you are looking for a CMS/wiki kind of functionality. There is nothing like that built into the .NET framework.
I suggest you look for wiki software.
No need to worry about big codes etc
You can create it with 2 simple steps explained in my blog
Create one empty page then copy it using Files concept. Save it as what ever name you want.
Try to change the content inside newly created page to Run smoothly
Please visit my blog for example solution
Click here for Solution Post
You can use the Page.ParseControl method.
Beware, it does not support everything you can do with a real ASPX or ASHX file, but it works for simple things. An example is available here: Using Page.ParseControl to add new control from Control Tag
There is very simple way to do that. It is working on principe to Copy one your Page, you should put Page with controls that you want to use on your other pages. So first you need using System.IO; then you need this code
protected void Button1_Click(object sender, EventArgs e)
{
try
{
File.Copy(Server.MapPath("") + "\\Submit.aspx", (Server.MapPath("") + "\\" + TextBox1.Text + ".aspx"));
File.Copy(Server.MapPath("") + "\\Submit.aspx.cs", (Server.MapPath("")+ "\\" + TextBox1.Text + ".aspx.cs"));
Response.Redirect(TextBox1.Text + ".aspx");
}
catch
{
Response.Write("<script>window.alert('This page is taken. Please change name!')</script>");
}
}
As you can see, The page you are coping is called Submit, and new Name of your Page is called like textbox1.text.
I hope that this helped you and anyone else.
enter link description here
Before some time I saw a logical question in a web site in this a person give the problem as like this
“I have one page called First.aspx in my web application, in this page; I have a simple asp.net button control. And I want is, on the click event of that button control, create (render) a new dynamic "page" that can be opened in a new windows or tab”
.
Here we give An Example here to creating a new .aspx page at run time. We also give the option to give page name. The user can give as he/she like. Like Google blogging we make new page at runtime.The new page is not in the website, this page create needs to be created at runtime and needs to be dynamic.

Passing value through querstring without showing the destination page in the URL with ASP.NET web form

I have a small web app being written in ASP.NET, VB.NET , .NET 3.5
I pass a value from default.aspx to demo.aspx using query string. When Go button is clicked the URL will be like this : localhost/demo.aspx?id=4
But I want URL to be sth like this when Go button is clicked : localhost/?id=4 and the id value is passed to demo.aspx so the expected result can be shown. How to get this done if possible without using the routing technique.
This will work only if the web server considers demo.aspx to be the default document. When you load a page without stating the page name (such as you localhost/?id=4 example), the web server will locate a file with a pre-defined name and use it as the default document. Often this file is called default.aspx (or .asp, .htm, .html, .php or something like that). So if you want to load any other page you will need to either state the name, or use URL rewriting techniques.
Modify your go button to link button and set herf = "?id=[id]"
Modify Page_Load event of Default.aspx by using the following code.
public Page_Load(object sender,EventArg e)
{
if(!string.IsNullOrEmpty(Request["id"]))
{
// Using Server.Transfer() function to call Demo.aspx page.
// Client still see "localhost/?id=[id]" at address bar.
Server.Transfer("Demo.aspx?id=" + Request["id"]);
}
}
Something like this?
protected void btnGo_Click (Object sender, EventArgs e)
{
Response.Redirect("localhost/?" + Request.QueryString);
}
Not sure when you want the url to change? Not sure what the routing technique is either. Sorry.
if you want it to work with postbacks, you still need to make sure the webserver is configured with a default document like default.aspx.
in 3.5 sp1 MS finally allows us to set the action attribute of the form.
so on page init you can explicitly set it now
Form1.Action = "/mydirectory/"
Caveat: Some versions of IIS do not allow posting to a directory. (XP IIS for example)

Resources