如何在回发期间保存自定义用户控件视图状态?

发布于 2024-12-11 13:31:25 字数 365 浏览 5 评论 0原文

我编写了用户控件 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

习惯成性 2024-12-18 13:31:25

如果动态向页面添加控件,则必须在每次回发后重新创建它。
尝试删除 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:

  • add some public properties to your user control in order to get access to the values you need
    or
  • use TextBox txtBox = (TextBox)myCustomControlObject.FindControl("nestedTextBox") method to find (more information here: http://msdn.microsoft.com/en-us/library/486wc64h.aspx)
别念他 2024-12-18 13:31:25

您还可以使用 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文