如何在回发期间保存自定义用户控件视图状态?
我编写了用户控件 InputDetails,其中有一些文本框和一些单选框。
我在Page_Load期间动态添加它:
if(!Page.IsPostBack()){
InputDetails input = (InputDetails)Page.LoadControl("InputDetails.ascx");
PlaceHolder1.Controls.Add(input);
}
但是当我刷新页面时,控件消失了,所以我问,如何将用户控件保存在已添加的视图状态中,以便下次自动重新加载。更好的是,当页面回发时,如何读取用户控件文本框中输入的值?我需要能够在单个页面上添加多个 InputDetails,因此保存它会很有用。
I have written the user control InputDetails that has a few text boxes and a few radio boxes inside it.
I add it dynamically during Page_Load:
if(!Page.IsPostBack()){
InputDetails input = (InputDetails)Page.LoadControl("InputDetails.ascx");
PlaceHolder1.Controls.Add(input);
}
but when I refresh the page, the control is gone, so I'm asking, how do I save the user control in the viewstate that it has been added, so it automatically reloads it next time. Better yet, how do I read the values put in the text boxes of the user control when the page is posted back? I need to be able to add multiple InputDetails on a single page so saving it would be useful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果动态向页面添加控件,则必须在每次回发后重新创建它。
尝试删除 if (Page.IsPostBack()) 行并检查它是否有效:)。
对于您创建的每个控件,您还应该在每次创建时设置相同的 ID 值。
如果没有其他问题,ViewState 应该能够跨回发保存控件的状态。
为了读取这些值,您可以:
或
If you add a control to the page dynamically, you have to recreate it after each postback.
Try to remove the if (Page.IsPostBack()) line and check if it works :).
For each control you create, you should also set the same ID value each time it's created.
If there are no other issues, the ViewState should then be able to save state of the controls across postbacks.
In order to read the values, you can:
or
您还可以使用 AJAX 动态加载用户控件/服务器控件,并且视图状态需要控件 ID 才能正确存储视图状态。
您可以浏览此链接更多信息
You can load user controls / server control dynamically using AJAX also and viewstate requires controls ID to store the viewstate properly.
would you pls go through this link for more info