C# 回发时,输入文本控件消失
我在代码中以这种方式创建了一些输入控件(文本),作为DynamicRadioButtonList的一部分(因此文本框位于radiobutton旁边):C# 回发时,输入文本控件消失,c#,asp.net,user-controls,updatepanel,postback,C#,Asp.net,User Controls,Updatepanel,Postback,我在代码中以这种方式创建了一些输入控件(文本),作为DynamicRadioButtonList的一部分(因此文本框位于radiobutton旁边): RadioButtonList radioOption=新建RadioButtonList(); radiobuttonlist.Items.Add(新的ListItem(dt.Rows[i][9].ToString()+“”) 所有控件都在UpdatePanel中 每次回发时,输入控件中的文本都会消失 如何保留输入的文本值 有什么想法吗?非常
RadioButtonList radioOption=新建RadioButtonList();
radiobuttonlist.Items.Add(新的ListItem(dt.Rows[i][9].ToString()+“”)
所有控件都在UpdatePanel中
每次回发时,输入控件中的文本都会消失
如何保留输入的文本值
有什么想法吗?非常感谢!必须在每次回发时重建控件树,包括部分回发-或者让它通过ControlState/ViewState重建。在这种情况下,在后续回发中,不会重建(或清除)Items集合,并且在渲染阶段为空 对于这样的情况,我的做法是:
1这应该行得通,但可能行不通。我通常不清楚哪些控件真正支持ViewState,以及在多大程度上支持ViewState,因为我觉得它的用法总是不一致。无论如何,如果禁用ViewState,它将不起作用-请记住禁用页面(或父控件)的ViewState一直禁用ViewState。此外,必须在适当的时间使用相同的控件路径/ID(通常为Init或Load)将控件加载到控件树中,以便它能够在请求ViewState时正确运行
#2的大致概念: 在包含的用户控件中保存视图状态(必须为此控件启用ViewState): 动态添加RadioButtonList(例如,在
控件中。OnLoad
):
如果数据绑定工作正常,那么应该可以禁用RadioButtonList的ViewState。但是有时候当ControlState应该使用时,会使用ViewState,所以请确保它按需要运行。非常感谢pst-我将尝试选项1,并让您知道它是如何运行的!我正在尝试选项2(选项1不起作用)。GenericDataSourceControl似乎非常复杂。是否有更简单的方法“将适当集合的ViewState存储在容器控件上,然后对消费控件进行数据绑定”?谢谢!!!@viv_#icious这就是(#2)我发现的最一致的方法-它还有一个额外的优点,就是将视图和模型分离(有些)。它只在最初几次比较复杂-然后是一个简单的模式。感谢pst-我真的是一个asp.net新手,所以所有这些都很难理解。只启用ViewState,将每个输入文本存储在ViewState中是否有效[“名称”]等?你有什么例子吗?只是很难在网上搜索输入控件的例子…我猜现在已经没有多少人使用它了!
RadioButtonList radioOption = new RadioButtonList();
radiobuttonlist.Items.Add(new ListItem(dt.Rows[i][9].ToString() + " <input id=\"" + name + "\" runat=\"server\" type=\"text\" value=\"Enter text\" />")
// ListItem is appropriately serializable and works well for
// automatic binding to various list controls.
List<ListItem> Names {
// May return null
get { return (List<ListItem>)ViewState["names"]; }
set { ViewState["names"] = value; }
}
void SelectEvent(sender e, GenericSelectArgs args) {
args.SetData(Names);
}
// Unless this NEEDS to be dynamic, move it into the declarative markup.
// The dynamic control must be added to the *same location* it was before the
// postback or there will be ugly invalid control tree creation exceptions.
var radioList = new RadioButtonList();
someControl.Controls.Add(radioList);
// To minimize problem with Control Tree creation this should be unique and
// consistent for dynamic controls.
radioList.ID = "radioList";
// Binding to the DS "declarative" usually works better I've found
radioList.DataSourceID = "idOfTheGDS";
// You -may- need to DataBind, depending upon a few factors - I will usually call
// DataBind in PreRender, but generally in the Load is OK/preferred.
// If it already binds, don't call it manually.
radioList.DataBind();