Adding User Control instead of EditForm.aspx in designer - asp.net

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 :)

Related

Register javascript resource on a dynamically added server control

I've created a ASP.Net Server Control, lets call it "WebGrid". The WebGrid control has an embedded javascript resource that it registers with the ScriptManager of whatever page is hosting it.
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
ClientScriptManager scriptManager = this.Page.ClientScript;
scriptManager.RegisterClientScriptResource(typeof(WebGrid), "Atl.Core.Resources.Scripts.WebGrid.js");
}
I have also made sure that the WebGrid.js file is an embedded resource by changing the build action property of the file to "Embedded Resource" and added the resource to the AssemblyInfo.cs of the project as such:
[assembly: WebResource("Atl.Core.Resources.Scripts.WebGrid.js", "text/js")]
Now, when the control is declared in the markup of an aspx page the javascript resource is registered with the ScriptManager and everything works fine. BUT, if I add the control to the Page's ControlCollection dynamically through a button click the javascript does NOT get registered and the control complains cannot find such and such javascript function. Note both the button and the WebGrid are located in different UpdatePanels...
EDIT: Looks like the problem is because I am using the ClientScript vs. ScriptManager see(Differences between ScriptManager and ClientScript when used to execute JS?). So, how do I then register the WebGrid's script with the Page's ScriptManager?
So I figured it out. The answer is pretty simple. In the control PreRender event handler just call the static RegisterClientScriptResource method on the ScriptManager class. I was confused because I thought I needed to reference the physical ScriptManager on the page! Nope. You still need to set the script as an embedded resource and add the WebResource attribute in the Assembly.cs file.
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
ScriptManager.RegisterClientScriptResource(this, typeof(ScriptTest), "Atl.Core.Resources.Scripts.ScriptTest.js");
}

aspx page showing old master page even after changing in page directive

This is either a weird behavior or I am doing something wrong here.
I have an aspx page with an associated Master page. I want to replace this master page with a new one.
The following steps I followed:
I created a new master page and added the same html from the old one.
I replaced MasterPageFile attribute in the page directive.
I thought this should work but it is not. It is still showing the old master page.
Now, when I replace the MasterPageFile from code it works.
public DefaultNew()
{
this.PreInit += new EventHandler(DefaultNew_PreInit);
}
void DefaultNew_PreInit(object sender, EventArgs e)
{
MasterPageFile = "~/_Master/MasterPageNew.Master";
}
I have rebuilt the code, closed Visual Studio and restarted but in Vain.
An ideas? Please help.
Check to see if you have a base class for the page that happens to sets the master page for all pages that derive from it.

Custom control does not exist exception in SharePoint

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.

Access Masterpage control on a basepage using FindControl

I have an ASP.NET website application on .NET 4.0. There is one Masterpage that contains the header and footer for all the aspx pages. The content comes from the individual aspx pages. I have BasePage.cs which all the aspx pages inherit from.
Now to the problem:
I have a HTML Select control on the masterpage, whose value I am trying to retrieve in the BasePage.cs using the below code
string language = ((System.Web.UI.HtmlControls.HtmlSelect)Master.FindControl("cmbLanguage")).Value;
I am using this inside the InitializeCulture method, which would set the Culture info for the website.
protected override void InitializeCulture()
{
string language = ((System.Web.UI.HtmlControls.HtmlSelect)Master.FindControl("cmbLanguage")).Value;
Thread.CurrentThread.CurrentCulture =
CultureInfo.CreateSpecificCulture(language);
Thread.CurrentThread.CurrentUICulture = new
CultureInfo(language);
base.InitializeCulture();
}
While debugging, I can see that the expected value is set in the language variable. The problem is when the page renders, the content inside the ContentPlaceHolder for the aspx page is not being rendered.
I can see that it is the code involving FindControl which is the cause, because if I set the language to a string, everything works as expected.
string language = "de-DE";
What am I doing wrong?
UPDATE:
If there is some content on the ContentPlaceHolder on the MasterPage, then it gets rendered instead of the page ContentPlaceHolder.
InitializeCulture is called before even PreInit in the page life cycle, which means the controls haven't been setup, and the Value of that control is likely coming through as an empty string.
You need to likely change how the culture is read, through a cookie, session value, or some other method. I'm not familiar with doing it, so i don't have a great suggestion or best practice.
As Doozer correctly noted that control is unlikely to have the correct value set up into it at the time of InitializeCulture. I will suggest that you read the value from POST data in the request and back it via some default value. For example,
string language = Request.Form[Master.FindControl("cmbLanguage")).UniqueID];
language = string.IsNullOrWhiteSpace(language) ? "de-DE" : language;
In order to access MasterPage controls its better to use MasterType directive. When used you are going to be able to access master page in a strongly typed way. In this case you will be able to create a property on master page like this:
public string SelectedCulture
{
get
{
return cmbLanguage.Value
}
}
And on page itself you will be able to run code like this:
protected override void InitializeCulture() {
string language = this.Master.SelectedCulture;
}

How to generate a full ASP.Net Webform via Server Controls?

I need to make a full asp.net webform with Server Control. I Wonder how to do this?
There is a class called Page in .Net but I don't know how to work with it.
A Page or WebForm must have a URL so from other pages can be accessible. Who knows this?
Your problem is to generate your page without using aspx, am I right ?
If you want your page to be accessible from your WebServer, you will need the .aspx file, even if you completely generate your page by code.
To create your page, you create a class who inherits from the Page class, and you add your server controls to it.
public class TestCodePage : Page
{
public TestCodePage()
{
HtmlForm form = new HtmlForm();
LiteralControl l = new LiteralControl("I write a text in my form");
form.Controls.Add(l);
this.Controls.Add(form);
}
}
In order to have an url for this page, you just need to create an aspx page with this content :
<%# Page Inherits="MyWebApp.TestCodePage" %>

Resources