gpt4 book ai didi

c# - HiddenField.Value 在我的自定义控件内回发时是一个空字符串

转载 作者:行者123 更新时间:2023-11-30 22:41:43 25 4
gpt4 key购买 nike

我有一个带有隐藏字段的自定义控件。回发时我想获取存储在其中的值,但它始终是一个空字符串。有什么想法吗?

我正在对隐藏字段值执行客户端操作,并在发出回发之前在 firebug 中验证字段是否正确

这是设置:

public class DualListPanel : SWebControl, INamingContainer

{

protected IList<DlpItem> UnassignedList { get; set; }
protected IList<DlpItem> AssignedList { get; set; }
private HiddenField assignedItemsField, unassignedItemsField;

public DualListPanel()
{
CssClass = "DualListPanel";
EnableViewState = true;
}

#region ViewState

protected override void LoadViewState(object savedState)
{
var state = savedState as object[];
UnassignedList = state[0] as List<DlpItem>;
AssignedList = state[1] as List<DlpItem>;
base.LoadViewState(state[2]);
}

protected override object SaveViewState()
{
object[] state = new object[3];
state[0] = UnassignedList;
state[1] = AssignedList;
state[2] = base.SaveViewState();
return state;
}

#endregion

#region WebControl Overrides


protected override void OnInit(EventArgs e)
{
EnsureChildControls();
GetUnassignedList(); //omitted method
GetAssignedList(); //omitted method
base.OnInit(e);
}

protected override void CreateChildControls()
{
assignedItemsField = new HiddenField();
assignedItemsField.ID = "HiddenAssignedItems";
assignedItemsField.EnableViewState = true;

unassignedItemsField = new HiddenField();
unassignedItemsField.ID = "HiddenUnassignedItems";
unassignedItemsField.EnableViewState = true;

Controls.Add(assignedItemsField);
Controls.Add(unassignedItemsField);
base.CreateChildControls();
}

#endregion

#region Item Lists Retrieval

public string GetCommaDelimUnassignedItems()
{
return unassignedItemsField.Value;
}

public string GetCommaDelimAssignedItems()
{
return assignedItemsField.Value;
}

#endregion
}

最佳答案

我认为隐藏字段的值在回发期间不会丢失,每当您初始化隐藏字段时,将您的代码放在 Ispostback 中。

protected override void CreateChildControls()         
{
if(!ispostback){
assignedItemsField = new HiddenField();
assignedItemsField.ID = "HiddenAssignedItems";
assignedItemsField.EnableViewState = true;
unassignedItemsField = new HiddenField();
unassignedItemsField.ID = "HiddenUnassignedItems";
unassignedItemsField.EnableViewState = true;
Controls.Add(assignedItemsField);
Controls.Add(unassignedItemsField);
base.CreateChildControls();
}
}

关于c# - HiddenField.Value 在我的自定义控件内回发时是一个空字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4716146/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com