Ci risiamo con questo Medium Trust di register.
Ho costruito una dll in design mode per costruire una textbox con il calndario di jquery.
Posto il codice:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HTMC = System.Web.UI.HtmlControls;
namespace UserControl20
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:DatePicker runat=\"server\" Text=\"[Text]\"></{0}:DatePicker>")]
public class DatePicker : CompositeControl, IPostBackDataHandler
{
public event EventHandler OnTextChanged;
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("1")]
[Localizable(true)]
public int NoOfSpaces
{
get
{
if (ViewState["NoOfSpaces"] == null)
{
ViewState["NoOfSpaces"] = 0;
return 0;
}
int intSpaces = -1;
int.TryParse(ViewState["NoOfSpaces"].ToString(), out intSpaces);
if (intSpaces != -1) return intSpaces;
else
{
throw new InvalidOperationException("invalid value");
}
}
set
{
ViewState["NoOfSpaces"] = value;
}
}
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string Text
{
get
{
String s = ViewState["Text"] as string;
return ((s == null) ? "[Text]" : s);
}
set
{
if (ViewState["Text"] != null && (ViewState["Text"].ToString() != value))
if (this.Text != this._DatePicker.Text)
this._DatePicker.Text = value;
// OnTextChanged(this, EventArgs.Empty);
ViewState["Text"] = value;
}
}
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("True")]
[Localizable(true)]
public bool Enabled
{
get
{
String s = ViewState["Enabled"] as string;
return ((s == null) ? true : Convert.ToBoolean(s));
}
set
{
if (ViewState["Enabled"] != null && (Convert.ToBoolean(ViewState["Enabled"].ToString()) != value))
if (this.Enabled != this._DatePicker.Enabled)
this._DatePicker.Enabled =value;
// OnTextChanged(this, EventArgs.Empty);
ViewState["Enabled"] = value;
}
}
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
private string _ImageCalendar;
public string ImageCalendar
{
get { return _ImageCalendar; }
set { _ImageCalendar = value; }
}
private int _Larghezza;
public int Larghezza
{
get { return _Larghezza; }
set { _Larghezza = value; }
}
TextBox _DatePicker = new TextBox();
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
}
protected override void OnInit(EventArgs e)
{
InitControls();
base.OnInit(e);
}
protected override void CreateChildControls()
{
BuildControl();
base.CreateChildControls();
}
private void InitControls()
{
_DatePicker.EnableViewState = true;
_DatePicker.ID = "DatePicker";
}
private void BuildControl()
{
this.Controls.Clear();
_DatePicker.Text = this.Text;
if (_Larghezza > 0)
{
_DatePicker.Width =System.Web.UI.WebControls.Unit.Pixel(_Larghezza);
}
//Aggiungo un div
this.Controls.Add(this._DatePicker);
Literal hidControl = new Literal(); // to raise LoadPostData
hidControl.Text = String.Format("<input type=hidden name='{0}' value = '{0}'>", this.UniqueID);
this.Controls.Add(hidControl);
ClearChildViewState();
if (ImageCalendar == null) { ImageCalendar = ""; }
if (ImageCalendar.Trim().Length == 0) { ImageCalendar = "../image/calendar.gif"; }
if (this.Enabled == true)
{
string script = "";
script += " <script type=\"text/javascript\">";
script += "$(function() {";
script += "$(\"#" + _DatePicker.ClientID + "\").datepicker(";
script += " {";
script += " showOn: 'button',";
script += " dateFormat: 'dd/mm/yy',";
script += " dayNames: ['Domenica', 'Lunedì', 'Martedì', 'Mercoledì', 'Giovedì', 'Venerdì', 'Sabato'],";
script += " dayNamesShort: ['Dom', 'Lun', 'Mar', 'Mer', 'Gio', 'Ven', 'Sab'],";
script += " dayNamesMin: ['D', 'L', 'M', 'M', 'G', 'V', 'S'],";
script += " monthNames: ['Gennaio','Febbraio','Marzo','Aprile','Maggio','Giugno','Luglio','Agosto','Settembre','Ottobre','Novembre','Dicembre'],";
script += " monthNamesShort: ['Gen','Feb','Mar','Apr','Mag','Giu','Lug','Ago','Set','Ott','Nov','Dic'],";
script += " nextText: 'Successivo' ,";
script += " prevText: 'Precedente' ,";
script += " firstDay: 1,";
script += " changeMonth: true,";
script += " changeYear: true,";
script += " buttonImage: '" + ImageCalendar + "',";
script += " buttonImageOnly: true";
script += " }).closest(\"body\").find(\"#ui-datepicker-div\").wrap('<div class=\"datePicker\"></div>');";
script += "$(\"#" + _DatePicker.ClientID + "\").mask(\"99/99/9999\");";
script += "});";
script += "</script>";
Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "ScriptShowDatePicker" + _DatePicker.ClientID, script);
}
}
protected override void OnPreRender(EventArgs e)
{
ChildControlsCreated = false;
EnsureChildControls();
base.OnPreRender(e);
}
#region IPostBackDataHandler Members
public bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
{
if (postDataKey == this.UniqueID)
{
foreach (string key in postCollection.Keys)
{
if (key.ToLower().Contains(this._DatePicker.UniqueID.ToLower()))
{
this.Text = postCollection[key];
}
}
}
return false;
}
public void RaisePostDataChangedEvent()
{
return;
}
#endregion
}
}
Questo viene regsitrato nella pagina in questo modo:
<%@ Register Assembly="UserControl20, Version=0.0.0.0, Culture=neutral, PublicKeyToken=f93b385e790b3228" Namespace="UserControl20" TagPrefix="UC" %>
e viene utilizzato così:
<UC:DatePicker ID="txt_data" CssClass="textboxData" runat="server"
ImageCalendar="../image/calendar.gif" />
Su register da questo errore:
Server Error in '/' Application.
--------------------------------------------------------------------------------
Security Exception
Description: The application attempted to perform an operation not allowed by the security policy. To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file.
Exception Details: System.Security.SecurityException: That assembly does not allow partially trusted callers.
Source Error:
Line 285: </td>
Line 286: <td width="38%">
Line 287: <UC:DatePicker ID="txt_datada_cerca" CssClass="textboxData" runat="server" />
Line 288: </td>
Line 289: <td width="15%">
Source File: \\wagner\wwwroot$\imageprm.it\Keplero\Contenuti\GestArticoli.aspx Line: 287
Stack Trace:
[SecurityException: That assembly does not allow partially trusted callers.]
ASP.keplero_contenuti_gestarticoli_aspx.__BuildControltxt_datada_cerca() in \\wagner\wwwroot$\imageprm.it\Keplero\Contenuti\GestArticoli.aspx:287
ASP.keplero_contenuti_gestarticoli_aspx.__BuildControlaspPanelRicerca() in \\wagner\wwwroot$\imageprm.it\Keplero\Contenuti\GestArticoli.aspx:257
ASP.keplero_contenuti_gestarticoli_aspx.__BuildControl__control3(Control __ctrl) in \\wagner\wwwroot$\imageprm.it\Keplero\Contenuti\GestArticoli.aspx:253
System.Web.UI.CompiledTemplateBuilder.InstantiateIn(Control container) +12
ManagementCollapsiblePanel.CollapsiblePanelContainerControl.CreateChildControls() +560
System.Web.UI.Control.EnsureChildControls() +87
System.Web.UI.Control.PreRenderRecursiveInternal() +44
System.Web.UI.Control.PreRenderRecursiveInternal() +171
System.Web.UI.Control.PreRenderRecursiveInternal() +171
System.Web.UI.Control.PreRenderRecursiveInternal() +171
System.Web.UI.Control.PreRenderRecursiveInternal() +171
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +6785
System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +242
System.Web.UI.Page.ProcessRequest() +80
System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context) +21
System.Web.UI.Page.ProcessRequest(HttpContext context) +49
ASP.keplero_contenuti_gestarticoli_aspx.ProcessRequest(HttpContext context) in App_Web_foat7k5b.0.cs:0
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +181
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75
--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.4952; ASP.NET Version:2.0.50727.4927
Ho provato a compilare la dll mettendo nell'assemblyinfo questo:
[assembly:System.Security.AllowPartiallyTrustedCallers]
ma niente.
Ho provato a compilare la dll firmando l'assembly ma niente.
Ho provato a compilare il sito Markando l'assembly con AllowPartiallyTrustedCallers impostato su Signig nella pagina delle proprietà del Deployement Project.
Io non so più cosa fare...quelli di register mi continuano a rimandare a questa guida:
http://msdn.microsoft.com/it-us/library/ms998341.aspx#paght000020_developing
ma oltre a impostazioni sul web.config che io non posso fare non vedo altro...