I cant use the following code in Master page for Globalization & Localization. It gives the error as commented in the code part "does not contain a defination for InitializeCulture"
protected override void InitializeCulture()
{
if (Request["Language"] != null)
{
//String selectedLanguage = Request["Language"];
// code wil go here
}
base.InitializeCulture();
//base.InitializeCulture gives error as mentioned in the next line
//does not contain a defination for InitializeCulture
}
When i add this code to other pages other than Master Page it works fine. is there any restriction on using this code in Master Page.
If i am able to define this code in Master Page then i dont need to write this code in every file.
Am i doing something wrong, I have include File for threading and Globalization, Still it doesn't work in Master Page
You have to do this (= override InitializeCulture) in your Page class. It doesn't work in the master page (MasterPage is derived from Control, not from page). I would suggest that you implement a base class which is derived from Page and derive every web form from this class, then you also have to write the code only once. It is always handy to have your own base class.
In Visual Studio you add a new class PageBase.cs:
public class FormBase : Page
{
protected override InitializeCulture()
{
if (Request.Form["lbCulture"] != null)
{
String selectedLanguage = Request.Form["lbCulture"];
UICulture = selectedLanguage;
Culture = selectedLanguage;
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(selectedLanguage);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(selectedLanguage);
}
base.InitializeCulture();
}
}
The current culture is either stored in some dropdown listbox, in the session or passed by query string. I used a listbox in the sample.
And then you derive your WebForm from this page like this:
public class Default : FormBase // instead of deriving from Page
Related
I have a master page with a placeholder control.
I have a page which inherits from master page.
I then have a custom control which is displayed on the page, this custom control has a cast to the masterpage so I can access the placeholder control to turn visibility on and off. Everything works as expected when I watch it in the debugger, but the placeholder control fails to turn it's visibility off.
I feel this has something to do in the order in which the events are firing. It appears any code in the custom control on the page is firing after the masterpage has already rendered.
Does anybody have any idea how I can affect the way this page is rendered so the custom control can turn the placeholder and on and off?
the code in the control looks like this;
protected override void Render(HtmlTextWriter writer)
{
var master = this.Page.Master as Site;
if (master != null) // cast failed, your master is a different type
{
master.NavBar.Visible = false;
}
// other stuff
}
Include the MasterType tag in your page, so that you dont need to typecase the page.Master, directly you can get the Master instance.
The solution to this problem was the order in which the control, masterpage and page were being fired, it was ignoring the setting on the custom control. The solution is to add this functionality to the custom control on the OnPreRender(EventArgs e) method.
protected override void OnPreRender(EventArgs e)
{
var master = this.Page.Master as Site;
if (master != null) // cast failed, your master is a different type
{
var progressShown = master.FindControl("ProgressShown");
if (progressShown != null)
{
master.NavBar.Attributes.Add("class", "test");
}
}
base.OnPreRender(e);
}
I wish to create a public method on my masterpage, that I can call from within every subpage.
I am trying to wrap my head around how this should be done.
On my subpages I've made this method to fill a panel with an errormessage.
protected void errorMessage (string errorText) {
HtmlGenericControl divTag = new HtmlGenericControl("div");
Panel_Name.Controls.Add(divTag);
divTag.InnerHtml = errorText;
}
Now if I were to make this function public on my masterpage, It wont recognize my Panel as it havent been showed yet. I'm guessing the answer involves FindControl
(Sorry for my rubbish code english)
How should I do this ?
To be fair, for your scenario, I would use a UserControl (.ascx) on your Pages (.aspx).
Then, in the UserControl, have your error message markup, such as:
Code front (ErrorMessage.ascx)
<asp:Panel runat="server" ID="PanelErrorMessage" /> // creates a <div>
Code behind (ErrorMessage.ascx.cs)
public string ErrorMessage
{
get {}
set
{
PanelErrorMessage.Text = value; // sets the panel text (<div>text</div>) to value when property is set
}
}
Use your UserControl on your Page (you'll need to define this as a control tag on your page too with the prefix/suffix):
<myControls:ErrorMessage runat="server" ID="MyErrorControl" />
You can also do this in many places on your page, if you require different errors.
Then, when you have an error, you'll simply do:
MyErrorControl.ErrorMessage = "This is my error message";
Job done!
First let me clear the air and post articles which already explain how to override the SaveButton:
How to override or customize the Sharepoint SaveButton?
How to override functionality of the Ribbon Save button
Override SharePoint Save Button to Provide Custom Functionality
I have read those and understood them, I just don't know how to fully implement it in my particular case:
I have a custom rendering template "CustomRender" which includes the "real form". The code for the real form looks something around these lines:
<%# Register TagPrefix="wssuc" TagName="ToolBar"
src="~/_controltemplates/ToolBar.ascx" %>
<%# Control Language="C#" AutoEventWireup="true"
CodeBehind="RealForm.ascx.cs" Inherits="CustomNameSpace.CustomForm" %>
<p>Test</p>
<wssuc:ToolBar runat="server" id="toolbar">
<TemplateButtons>
<SharePoint:SaveButton runat="server" />
</TemplateButtons>
</wssuc:ToolBar>
Now I want to override this save button. The sites above state that I just have to write another control which overrides the button. E.g.:
public class NewSaveButton: SaveButton
{
protected override bool SaveItem()
{
bool success = base.SaveItem();
RedirectUrl = String.Concat(List.ParentWeb.ServerRelativeUrl, "/",
List.Forms[PAGETYPE.PAGE_DISPLAYFORM].Url, #"?ID=",
ListItem.ID, #"&Source=", ListItem.ParentList.DefaultViewUrl);
return success;
}
}
Now I just don't know how to register this template inside my other template. Could I not just override the SaveButton in the Code behind of my template - how would I do that and reference it later on?
Option one: Code-Behind of the form (RealForm.ascx.cs) - can I just put the override method in there? How can I reference the button then in the form (how do I get <NewSaveButton>)?
Option two: Another template just for the button, e.g. SaveButton.ascx" - how do I reference that via <%# Register... %>, i.e. how do I know PublicKeyToken etc. when deployed via a Feature. And same thing here: My goal is to get some kind of "<NewSaveButton>" control for the form.
You're creating a new server control when you do this, so you'll need to register the new control on the page (or in this case, in the template .ascx file).
<%# Register TagPrefix="MyPrefix" Namespace="ControlNamespace" Assembly="MyFullyQualifiedAssembly" %>
In your code file you can to add the ToolboxDataAttribute to the class (this is only necessary if you are dragging&dropping the control from the toolbox in visual studio)
[ToolboxData("<{0}:NewSaveButton runat=\"server\"></{0}:NewSaveButton>")]
public class NewSaveButton : SaveButton {}
Now, you should be able to replace the save button on the form with the following:
<MyPrefix:NewSaveButton runat="server"></MyPrefix:NewSaveButton>
You're basically creating a new server control following the rules of asp.net (no sharepoint specific stuff is happening here).
For more information, take a look at this page: http://msdn.microsoft.com/en-us/library/yhzc935f(v=VS.85).aspx
On your page with SaveButton you could do the following trick (in my case save button is added in DataFormWebPart's XSL markup):
// On your page with SaveButton you could do the following trick
// (in my case save button is added in DataFormWebPart's XSL markup):
SPContext itemContext;
DataFormWebPart dataForm; // from designer's code behind
void Page_Init(object sender, EventArgs e)
{
// NOTE: by some reason ItemContexts of controls in DFWP are differ,
// so only SaveButton's OnSaveHandler is invoked
itemContext = dataForm.Controls.FindControlRecursive<SaveButton>().ItemContext;
}
void Page_Load(object sender, EventArgs e)
{
if (itemContext.FormContext.FormMode == SPControlMode.New ||
itemContext.FormContext.FormMode == SPControlMode.Edit)
{
itemContext.FormContext.OnSaveHandler += OnSaveHandler;
}
}
void OnSaveHandler(object sender, EventArgs eventArgs)
{
// TODO: Add your code before saving the item
SaveButton.SaveItem(saveButton.ItemContext, false, string.Empty);
// TODO: Add your code after saving the item
}
The FindControlRecursive() extension implementation is
public static class ControlExtensions
{
public static TControl FindControlRecursive<TControl>
(
this ControlCollection controls
) where TControl : Control
{
if (controls != null)
{
foreach (Control control in controls)
{
var foundControl = control as TControl
?? control.Controls.FindControlRecursive();
if (foundControl != null)
{
return foundControl;
}
}
}
return null;
}
}
Within my Website project, I have some aspx pages, javascript files, and a custom C# class I called MyCustomReport. I placed a Image box with an ID of Image1 inside SelectionReport.aspx. I need to get access to that Image1 inside MyCustomReport.cs so I can turn it on and off based on conditions. What code do I need to do this? Thanks everyone
You'll need to pass the instance of Image control to MyCustomReport. From there you'll be able to set it's Visible property to true or false.
Probably something like this
public partial class SelectionReport : Page
{
// your code here
protected void Page_Load( object sender, EventArgs e ){
MyCustomReport myCustomReport = new MyCustomReport();
myCustomReport.MyReport( Image1 );
}
}
public class MyCustomReport
{
public void MyReport( Image arg ){
// some more code
arg.Visible = false; // or true
}
}
EDIT derek is right, you won't need the entire page, just the image.
it sounds a bit odd to do it that way. You could pass the control to the class method using the ref keyword, then the class could modify it:
doSomething(data, MyUserControl);
I think a better implementation would be for your class to have a method or property that the page could query to turn the control on or off.
I'm currently converting a set of .aspx pages and the VB code behind them to .ascx and C#.
I'm most of the way through the project now but have become a bit stuck as I'm fairly new to ASP.net.
Basically the system I'm working with validates a shopping basket but with me changing the class the code inherits from I'm having issues working out what I should change it too.
I'm changing from System.Web.UI.Page to System.Web.UI.UserControl and am primarily having problems with the Validator.Add(v) element of the code below:
public override void Validate()
{
base.Validate();
if (Profile.ShoppingCart == null || Profile.ShoppingCart.Items.Count == 0)
{
CustomValidator v = new CustomValidator();
v.ErrorMessage = "You must have at least 1 course in your basket.";
v.IsValid = false;
Validator.Add(v);
}
}
So if anyone could provide assitance it would be appreciated.
Each user control contains a reference to the page that it is contained in.
Page.Validators.Add(v);