摘要:我有一个动态添加到页面的Web控件。该控件具有HiddenField子控件。我将控件的ClientID传递给jquery小部件,该小部件将值设置为字符串(json转换为字符串)。将表单发回服务器时,HiddenField的值为空字符串。我的值在Request.Form [ UniqueID ]对象中。值正在返回服务器。问题是,当我需要该值而不修改大量遗留代码时,我无法访问Request对象。
奇怪的是,selectmany控件是我遇到问题的控件,但它继承了正常工作的SelectOne。我认为我在selectmany类中做错了,因为即使我尝试将值推入SelectOneHiddenValue它也不起作用,但是当SelectOne使用它时它可以正常工作。
SelectOne:
[ToolboxData("<{0}:SelectOne runat=server></{0}:SelectOne>")]
public class SelectOne : Panel, IControl,ISelectOne
{
#region structure
private readonly Panel _selectOneTextboxContainer = new Panel();
protected readonly TextBox SelectOneTextbox = new TextBox();
protected readonly HiddenField SelectOneHiddenValue = new HiddenField();
private readonly Panel _selectOneDropdownImageContainer = new Panel();
private readonly Image _selectOneDropDownImage = new Image();
#endregion
private readonly IList<LookupItem> _selectedItems = new List<LookupItem>();
#region properties
public IList<LookupItem> SelectedItems { get { return _selectedItems; } }
public bool MultiSelect { get; set; }
public DisplayOption Display { get; set; }
public string Name { get; set; }
[DefaultValue(0)]
public int LookupValueOrgID
{
get
{
EnsureChildControls();
return Convert.ToInt32(String.IsNullOrEmpty(SelectOneHiddenValue.Value) ? "0" : SelectOneHiddenValue.Value);
}
set
{
EnsureChildControls();
SelectOneHiddenValue.Value = value.ToString();
}
}
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string Text
{
get
{
EnsureChildControls();
return SelectOneTextbox.Text;
}
set
{
EnsureChildControls();
SelectOneTextbox.Text = value;
}
}
/// <summary>
/// The minimum number of characters a user has to type before the autocompleter activates.
/// </summary>
[Bindable(true)]
[Category("Appearance")]
[DefaultValue(1)]
[Localizable(true)]
public int MinChars
{
get
{
int b = (ViewState["MinChars"] == null ? 1 : (int)ViewState["MinChars"]);
return b;
}
set
{
ViewState["MinChars"] = value;
}
}
/// <summary>
/// The number of backend query results to store in cache. If set to 1 (the current result), no caching will happen. Must be >= 1.
/// </summary>
[Bindable(true)]
[Category("Appearance")]
[DefaultValue(10)]
[Localizable(true)]
public int CacheLength
{
get
{
int b = (ViewState["CacheLength"] == null ? 10 : (int)ViewState["CacheLength"]);
return b;
}
set
{
ViewState["CacheLength"] = value;
}
}
public string OuterMarkupClientID
{
get { return "SelectOne_Container" + ClientID; }
}
/// <summary>
/// If true, target input text is appended to
/// If false, target input text is replaced
/// </summary>
public bool AppendSelectedTextToInput { get; set; }
public virtual string ContainerClass
{
get { return "SelectOneContainer"; }
}
#endregion
#region constructor
public SelectOne()
{
SetCssClasses();
}
#endregion
#region lifecycle overrides
protected override void CreateChildControls()
{
base.CreateChildControls();
AddChildrenToControlCollection();
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
SetupClientEvents();
ControlHelper.AddRequiredControl(this);
var resourceName = string.Empty;
var cs = Page.ClientScript;
resourceName = "UI.Controls.resources.images.DropDownButton.gif";
_selectOneDropDownImage.ImageUrl = cs.GetWebResourceUrl(GetType(), resourceName);
SelectOneTextbox.Attributes.Add("lookupOrgID", LookupOrgID.ToString());
SelectOneTextbox.Attributes.Add("cacheLength", CacheLength.ToString());
SelectOneTextbox.Attributes.Add("service", Service);
SelectOneTextbox.Attributes.Add("selectOneTextboxId", SelectOneTextbox.ClientID);
SelectOneTextbox.Attributes.Add("selectOneHiddenValueId",SelectOneHiddenValue.ClientID);
}
#endregion
#region private helpers
private void SetCssClasses()
{
this.CssClass = ContainerClass + " AutocompleteContainer";
_selectOneDropDownImage.CssClass = "SelectOneDropDownImage";
SelectOneTextbox.CssClass = "SelectOneTextbox QuantifiTextBox";
_selectOneTextboxContainer.CssClass = "SelectOneTextboxContainer";
_selectOneDropdownImageContainer.CssClass = "SelectOneDropDownImageContainer";
}
private void SetupClientEvents()
{
ControlHelper.RegisterAutoCompleteScript(this);
var resourceName = "UI.Controls.resources.scripts.SelectOne.js";
string js;
using (var sr = new StreamReader(GetType().Assembly.GetManifestResourceStream(resourceName)))
{
js = sr.ReadToEnd();
sr.Close();
}
ClientScriptProxy.Current.RegisterStartupScript(this, typeof(SelectOne), "SelectOneJS", js, true);
_selectOneDropDownImage.Attributes.Add("onclick", "SelectOne_ImageClick('" + SelectOneTextbox.ClientID + "');");
}
private void AddChildrenToControlCollection()
{
_selectOneTextboxContainer.Controls.Add(SelectOneTextbox);
this.Controls.Add(_selectOneTextboxContainer);
_selectOneDropdownImageContainer.Controls.Add(_selectOneDropDownImage);
this.Controls.Add(_selectOneDropdownImageContainer);
this.Controls.Add(SelectOneHiddenValue);
}
#endregion
}
选择多个:
[ToolboxData("<{0}:SelectMany runat=server></{0}:SelectMany>")]
public class SelectMany : SelectOne, ISelectMany
{
#region structure
private readonly Panel _selectedItemsPanel = new Panel();
private readonly HiddenField _selectManyHiddenField = new HiddenField();
public override string ContainerClass
{
get
{
return "SelectManyControlContainer";
}
}
#endregion
public SelectMany()
{
MultiSelect = true;
}
#region lifecycle overrides
protected override void CreateChildControls()
{
base.CreateChildControls();
_selectManyHiddenField.ID = "SelectedItemsHiddenValue";
this.Controls.Add(_selectedItemsPanel);
this.Controls.Add(_selectManyHiddenField);
SelectOneTextbox.Attributes.Add("data-multiselect", MultiSelect.ToString());
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
var resourceName = "jquery.SelectedItemCollection.js";
ClientScriptProxy.Current.RegisterClientScriptInclude(this, Page.ClientScript.GetWebResourceUrl(this.GetType(),resourceName));
var serializer = new JavaScriptSerializer();
var startupScript = String.Format("\n$('#{0}').each(function(){{" +
"var selectedPlugin = $(this).SelectedItemCollection(" +
" {{" +
" itemData: {1}," +
" deleteImageUrl: '{2}'," +
" selectedTextId: '{3}', " +
" hiddenTextFieldId: '{4}' " +
" }});}});\n",
this.ClientID,
serializer.Serialize(SelectedItems),
GetDeleteImageUrl(),
SelectOneTextbox.ClientID,
_selectManyHiddenField.ClientID
);
ClientScriptProxy.Current.RegisterStartupScript(this,this.GetType(),"SelectedItemsJs_" + this.ClientID,startupScript,true);
}
#endregion
#region public api
public void BindForm()
{
EnsureChildControls();
this.DataBind();
var jsonString = _selectManyHiddenField.Value;
SelectedItems.Clear();
if (!jsonString.IsNullOrEmpty())
{
Json.Decode<IEnumerable<LookupItem>>(jsonString).ForEach(li => SelectedItems.Add(li));
}
}
#endregion
private string GetDeleteImageUrl()
{
var cs = Page.ClientScript;
const string resourceName = "UI.Controls.resources.images.close.gif";
return cs.GetWebResourceUrl(this.GetType(), resourceName);
}
}
答案 0 :(得分:1)
为了纠正这个问题,我在基类和派生类的OnInit重写中添加了EnsureChildControls()。这样,我的所有控件都将被正确创建,ViewState将在创建后恢复。
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
EnsureChildControls();
}